-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
35 lines (27 loc) · 746 Bytes
/
db.js
File metadata and controls
35 lines (27 loc) · 746 Bytes
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
const client = require("mongodb").MongoClient;
const _dbName = "Node-Auth";
const _User = "root";
const _Password = "housemd123";
const _Host = "127.0.0.1";
const url = `mongodb://${_User}:${_Password}@${_Host}/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false`;
let _db;
function initDb(callback) {
if (_db) {
console.warn("Trying to init DB again!");
return callback(null, _db);
}
client.connect(url, { useUnifiedTopology: true }, connected);
function connected(err, db) {
if (err) return callback(err);
console.log("DB connected");
_db = db.db(_dbName);
return callback(null, _db);
}
}
function getDb() {
return _db;
}
module.exports = {
getDb,
initDb,
};