-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoHelper.js
54 lines (46 loc) · 1.29 KB
/
mongoHelper.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var Db = require('mongodb').Db,
Server = require('mongodb').Server;
var dbs = {};
var connectToDb = function(options, initcb) {
if (!options) {
initcb(new Error("missing options"));
return;
}
if (!options.mongodb) {
initcb(new Error("missing mongodb settings"));
return;
}
if (!options.mongodb.host) {
initcb(new Error("missing mongodb host"));
return;
}
if (!options.mongodb.port) {
initcb(new Error("missing mongodb port"));
return;
}
if (!dbs[options.mongodb.host]) {
dbs[options.mongodb.host] = {};
}
if (!dbs[options.mongodb.host][options.mongodb.port]) {
var dbo = new Db(
'parasoup',
new Server(
options.mongodb.host,
options.mongodb.port,
{ auto_reconnect: true, poolSize: 1 }
),
{ native_parser: false }
);
dbo.open(function(err, db) {
if (err) {
initcb(err);
} else {
dbs[options.mongodb.host][options.mongodb.port] = db;
initcb(null, db);
}
});
} else {
initcb(null, dbs[options.mongodb.host][options.mongodb.port]);
}
};
module.exports.open = connectToDb;