-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmongo.js
More file actions
33 lines (28 loc) · 732 Bytes
/
Copy pathmongo.js
File metadata and controls
33 lines (28 loc) · 732 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
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var connected = false;
var db;
module.exports = {
getDb: function(){
if (!connected) {
throw new Error('You must connect to mongo before you can call getDb');
}
return db;
},
getCollection: function(collectionName){
if (!connected) {
throw new Error('You must connect to mongo before youc an call getCollection');
}
return db.collection(collectionName);
},
connect: function(url, callback){
MongoClient.connect(url, function(err, _db){
if (err) {
throw new Error('Failed to connect to mongo: '+err);
}
db = _db;
connected = true;
callback();
});
}
};