-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
52 lines (52 loc) · 1.99 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//import mongodb driver
var mc=require("mongodb").MongoClient;
//to hold db object
var dbo;
var userCollectionObj;
var adminCollectionObj;
var articleCollectionObj;
var categoryCollectionObj;
var recipeCollectionObj;
var recent_actions_collectionObj;
var likeArticlesCollectionObj;
var likeRecipesCollectionObj;
//database url
var dbUrl="mongodb+srv://project:[email protected]/test?retryWrites=true&w=majority";
//var dbUrl="mongodb+srv://keerthi:[email protected]/test?retryWrites=true&w=majority";
//function to initialise database
function initDb() {
mc.connect(dbUrl,{useNewUrlParser:true,useUnifiedTopology:true},(err,client)=>{
if(err) {
console.log("error in connecting in db");
}
console.log("connected to database");
dbo=client.db("userdb");
userCollectionObj = dbo.collection("usercollection");
adminCollectionObj = dbo.collection("admincollection");
articleCollectionObj = dbo.collection("articlecollection");
categoryCollectionObj = dbo.collection("categorycollection");
recipeCollectionObj=dbo.collection("recipecollection");
recent_actions_collectionObj=dbo.collection("recent_actions_collections")
likeArticlesCollectionObj=dbo.collection("likearticlescollection");
likeRecipesCollectionObj=dbo.collection("likerecipescollection");
})
}
//function to return db object
function getDb() {
// console.log(dbo,"db has not been initialised.please call init");
return {
userCollectionObj:userCollectionObj,
adminCollectionObj:adminCollectionObj,
articleCollectionObj:articleCollectionObj,
categoryCollectionObj:categoryCollectionObj,
recipeCollectionObj:recipeCollectionObj,
recent_actions_collectionObj:recent_actions_collectionObj,
likeArticlesCollectionObj:likeArticlesCollectionObj,
likeRecipesCollectionObj:likeRecipesCollectionObj
}
}
//export 2 functions
module.exports={
getDb,
initDb
}