forked from mongo-express/mongo-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
156 lines (127 loc) · 4.08 KB
/
db.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var
async = require('async')
, mongodb = require('mongodb')
;
var utils = require('./utils');
var connect = function(config) {
// set up database stuff
var host = config.mongodb.server || 'localhost';
var port = config.mongodb.port || mongodb.Connection.DEFAULT_PORT;
var dbOptions = {
auto_reconnect: config.mongodb.autoReconnect,
poolSize: config.mongodb.poolSize
};
var db = new mongodb.Db('local', new mongodb.Server(host, port, dbOptions), {safe:true});
var connections = {};
var databases = [];
var collections = {};
//get admin instance
var adminDb = db.admin();
var mainConn; // main db connection
// update the collections list
var updateCollections = function(db, dbName, callback) {
db.listCollections().toArray(function (err, result) {
var names = [];
for (var r in result) {
names.push(result[r].name);
}
collections[dbName] = names.sort();
if (callback) {
callback(err);
}
});
};
// update database list
var updateDatabases = function(admin) {
admin.listDatabases(function(err, dbs) {
if (err) {
//TODO: handle error
console.error(err);
}
for (var key in dbs.databases) {
var dbName = dbs.databases[key]['name'];
//'local' is special database, ignore it
if (dbName == 'local') {
continue;
}
if (config.mongodb.whitelist.length != 0) {
if (!_.include(config.mongodb.whitelist, dbName)) {
continue;
}
}
if (config.mongodb.blacklist.length != 0) {
if (_.include(config.mongodb.blacklist, dbName)) {
continue;
}
}
connections[dbName] = mainConn.db(dbName);
databases.push(dbName);
updateCollections(connections[dbName], dbName);
}
//Sort database names
databases = databases.sort();
});
};
// connect to mongodb database
db.open(function(err, db) {
if (err) {
throw err;
}
console.log('Database connected!');
mainConn = db;
//Check if admin features are on
if (config.mongodb.admin === true) {
if (config.mongodb.adminUsername.length == 0) {
console.log('Admin Database connected');
updateDatabases(adminDb);
} else {
//auth details were supplied, authenticate admin account with them
adminDb.authenticate(config.mongodb.adminUsername, config.mongodb.adminPassword, function(err, result) {
if (err) {
//TODO: handle error
console.error(err);
}
console.log('Admin Database connected');
updateDatabases(adminDb);
});
}
} else {
//Regular user authentication
if (typeof config.mongodb.auth == "undefined" || config.mongodb.auth.length == 0) {
throw new Error('Add auth details to config or turn on admin!');
}
async.forEachSeries(config.mongodb.auth, function(auth, callback) {
console.log("Connecting to " + auth.database + "...");
connections[auth.database] = mainConn.db(auth.database);
databases.push(auth.database);
if (typeof auth.username != "undefined" && auth.username.length != 0) {
connections[auth.database].authenticate(auth.username, auth.password, function(err, success) {
if (err) {
//TODO: handle error
console.error(err);
}
if (!success) {
console.error('Could not authenticate to database "' + auth.database + '"');
}
updateCollections(connections[auth.database], auth.database);
console.log('Connected!');
callback();
});
} else {
updateCollections(connections[auth.database], auth.database);
console.log('Connected!');
callback();
}
});
}
});
return {
updateCollections: updateCollections,
connections: connections,
databases: databases,
collections: collections,
adminDb: adminDb,
mainConn: mainConn
}
};
module.exports = connect;