-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
39 lines (32 loc) · 952 Bytes
/
database.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
/**
* Global adviz database object. Gets initialized after the server is started. Can be used by any logger or router.
* @type {MongoClient}
*/
const MongoClient = require('mongodb').MongoClient;
// global state for mongodb
let state = {
database: undefined
};
exports.connectDB = function(url, done) {
if (state.database) return done()
MongoClient.connect(url, function(err, db) {
if (err) return done(err)
state.database = db.db("advizDB");
console.log("Successfully connected to AdViz Database!")
done()
})
}
exports.getDatabase = function() {
return state.database
}
exports.closeConnection = function(done) {
if (state.database) {
state.database.close(function(err, result) {
state.database = null
state.mode = null
console.log("Closing connection to Adviz Database...")
done(err)
})
}
}
module.exports = exports;