-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdb.js
More file actions
35 lines (30 loc) · 674 Bytes
/
db.js
File metadata and controls
35 lines (30 loc) · 674 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
'use strict';
const MongoClient = require('mongodb').MongoClient;
const config = require('./config');
const state = {
db: null,
};
exports.connect = function (url, done) {
if (state.db) { return done(); }
MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
}, function (err, client) {
const db = client.db(config.DATABASE);
if (err) { return done(err); }
state.db = db;
done();
});
};
exports.get = function () {
return state.db;
};
exports.close = function (done) {
if (state.db) {
state.db.close(function (err, result) {
state.db = null;
state.mode = null;
done(err);
});
}
};