-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
28 lines (25 loc) · 792 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
/* Malay Bhavsar (Leo-Malay) */
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectID;
const OId = require("mongodb").ObjectId.createFromHexString;
const config = require("config");
const mongoOption = { useNewUrlParser: !0, useUnifiedTopology: !0 };
const url = config.get("DB.URL");
const dbname = config.get("DB.NAME");
const state = { db: null };
const connect = (cb) => {
state.db
? cb()
: MongoClient.connect(url, mongoOption, (err, client) => {
err ? cb(err) : ((state.db = client.db(dbname)), cb());
});
};
const getID = (_id) => String(ObjectId(_id));
const getOID = (_id) => OId(String(_id));
const getDB = () => state.db;
module.exports = {
getDB,
connect,
getID,
getOID,
};