forked from jfromaniello/mongo-getdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 1.72 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const MongoClient = require("mongodb").MongoClient;
const async = require('async');
const configs = {};
const MemoizedConnect = async.memoize(function (alias, callback) {
if (!(alias in configs)) {
throw new Error('unknown ' + alias + ' config');
}
MongoClient.connect.apply(MongoClient, configs[alias].concat([callback]));
});
const isMongoUrl = (str) => str.indexOf('mongodb://') === 0 || str.indexOf('mongodb+srv://') === 0;
const getDb = module.exports = function(alias, callback) {
if ( !(alias in configs) && typeof alias == 'string' && isMongoUrl(alias) ) {
//directly using a connection string as alias.
configs[alias] = [alias];
}
if (typeof alias === 'function') {
callback = alias;
alias = 'default';
}
const done = function (err, client) {
let db;
if(client && client.constructor == MongoClient) {
db = client.db();
db.client = client;
} else {
db = client;
}
if (callback.length === 1) {
if (err) {
console.error('Error connecting to the db, exiting: \n', err);
return process.exit(1);
} else {
return callback(db);
}
} else {
callback(err, db);
}
};
MemoizedConnect(alias, function (err, db) {
if (err) {
return done(err);
}
done(null, db);
});
};
getDb.init = function () {
var args = [].slice.call(arguments, 0);
var alias = 'default';
if (args.length === 0) {
alias = 'default';
args = [process.env.DB];
}
if (typeof arguments[0] === 'string' && typeof arguments[1] === 'string'){
alias = args[0];
args = args.slice(1);
}
delete MemoizedConnect.memo[alias];
configs[alias] = args;
};
getDb.legacyHapi = require('./legacy-hapi')(getDb);