-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
35 lines (31 loc) · 846 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
// code example from mongoDB
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const dbname = 'foolance';
const mongoUri = 'mongodb+srv://foolancer:[email protected]/foolance?retryWrites=true&w=majority';
const mongoOptions = { useNewUrlParser: true, useUnifiedTopology: true };
const state = {
db: null,
};
// connect to db
const connect = (cb) => {
if (state.db) cb();
else
MongoClient.connect(mongoUri, mongoOptions, (err, client) => {
if (err) cb(err);
else {
state.db = client.db(dbname);
cb();
}
});
};
// get primary key of
const getPrimaryKey = (_id) => {
return ObjectID(_id);
};
// get db
const getDB = () => {
return state.db;
};
// export all functions
module.exports = {getDB, connect, getPrimaryKey};