-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
39 lines (31 loc) · 761 Bytes
/
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
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
require('dotenv').config()
const url = process.env.DB_URL_CLOUD;
const dbName = 'restfulblog';
const mongoOptions = {userNewUrlParser : true}
//console.log(process.env);
const state = {
db : null
};
const connect = (cb) => {
if(state.db)
cb();
else{
MongoClient.connect(url, mongoOptions, (err, client)=>{
if(err)
cb(err);
else{
state.db = client.db(dbName);
cb();
}
});
}
}
const getPrimaryKey = (_id) => {
return ObjectID(_id);
}
const getDB = () => {
return state.db;
}
module.exports = {getDB, connect, getPrimaryKey};