forked from amitinsan/requestslog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
42 lines (35 loc) · 890 Bytes
/
db.js
File metadata and controls
42 lines (35 loc) · 890 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
36
37
38
39
40
41
42
var MongoClient = require('mongodb').MongoClient;
const config = require('./config.js');
var state = {
db: null,
collection: null,
}
exports.connect = function(done) {
if (state.db) return done()
MongoClient.connect(config.database.path, function(err, database) {
if (err) return done(err)
state.db = database.db(config.database.name);
state.collection = state.db.collection(config.database.collection);
console.log('connection done');
done();
})
}
exports.get = function() {
return state.db;
}
exports.collection = function() {
return state.collection;
}
exports.otherCollection = function (collection) {
return state.db.collection(collection);
}
exports.close = function(done) {
if (state.db) {
state.db.close(function(err, result) {
state.db = null;
state.collection = null;
state.mode = null;
done(err);
})
}
}