forked from mrvautin/adminMongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
287 lines (268 loc) · 10.6 KB
/
common.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
// checks for the password in the /config/app.json file if it's set
exports.checkLogin = function(req, res, next){
var passwordConf = req.nconf.app.get('app');
// only check for login if a password is specified in the /config/app.json file
if(passwordConf && passwordConf.hasOwnProperty('password')){
// dont require login session for login route
if(req.path === '/app/login' || req.path === '/app/logout' || req.path === '/app/login_action'){
next();
}else{
// if the session exists we continue, else renter login page
if(req.session.loggedIn){
next(); // allow the next route to run
}else{
res.redirect(req.app_context + '/app/login');
}
}
}else{
// no password is set so we continue
next();
}
};
// gets some db stats
exports.get_db_status = function (mongo_db, cb){
var adminDb = mongo_db.admin();
adminDb.serverStatus(function (err, status){
if(err){
cb('Error', null);
}else{
cb(null, status);
}
});
};
// gets the backup dirs
exports.get_backups = function(cb){
var junk = require('junk');
var backupPath = path.join(__dirname, '../backups');
fs.readdir(backupPath, function (err, files){
cb(null, files.filter(junk.not));
});
};
// gets the db stats
exports.get_db_stats = function (mongo_db, db_name, cb){
var async = require('async');
var db_obj = {};
// if at connection level we loop db's and collections
if(db_name == null){
var adminDb = mongo_db.admin();
adminDb.listDatabases(function (err, db_list){
if(err){
cb('User is not authorised', null);
return;
}
if(db_list !== undefined){
async.forEachOf(exports.order_object(db_list.databases), function (value, key, callback){
exports.order_object(db_list.databases);
var skipped_dbs = ['null', 'admin', 'local'];
if(skipped_dbs.indexOf(value.name) === -1){
var tempDBName = value.name;
mongo_db.db(tempDBName).listCollections().toArray(function (err, coll_list){
var coll_obj = {};
async.forEachOf(exports.cleanCollections(coll_list), function (value, key, callback){
mongo_db.db(tempDBName).collection(value).stats(function (err, coll_stat){
coll_obj[value] = {Storage: coll_stat.size, Documents: coll_stat.count};
callback();
});
}, function (err){
if(err) console.error(err.message);
// add the collection object to the DB object with the DB as key
db_obj[value.name] = exports.order_object(coll_obj);
callback();
});
});
}else{
callback();
}
}, function (err){
if(err) console.error(err.message);
// wrap this whole thing up
cb(null, exports.order_object(db_obj));
});
}else{
// if doesnt have the access to get all DB's
cb(null, null);
}
});
// if at DB level, we just grab the collections below
}else{
mongo_db.db(db_name).listCollections().toArray(function (err, coll_list){
var coll_obj = {};
async.forEachOf(exports.cleanCollections(coll_list), function (value, key, callback){
mongo_db.db(db_name).collection(value).stats(function (err, coll_stat){
coll_obj[value] = {
Storage: coll_stat ? coll_stat.size : 0,
Documents: coll_stat ? coll_stat.count : 0
};
callback();
});
}, function (err){
if(err) console.error(err.message);
db_obj[db_name] = exports.order_object(coll_obj);
cb(null, db_obj);
});
});
}
};
// gets the Databases
exports.get_db_list = function (uri, mongo_db, cb){
var async = require('async');
var adminDb = mongo_db.admin();
var db_arr = [];
// if a DB is not specified in the Conn string we try get a list
if(uri.database === undefined || uri.database === null){
// try go all admin and get the list of DB's
adminDb.listDatabases(function (err, db_list){
if(db_list !== undefined){
async.forEachOf(db_list.databases, function (value, key, callback){
var skipped_dbs = ['null', 'admin', 'local'];
if(skipped_dbs.indexOf(value.name) === -1){
db_arr.push(value.name);
}
callback();
}, function (err){
if(err) console.error(err.message);
exports.order_array(db_arr);
cb(null, db_arr);
});
}else{
cb(null, null);
}
});
}else{
cb(null, null);
}
};
// Normally you would know how your ID's are stored in your DB. As the _id value which is used to handle
// all document viewing in adminMongo is a parameter we dont know if it is an ObjectId, string or integer. We can check if
// the _id string is a valid MongoDb ObjectId but this does not guarantee it is stored as an ObjectId in the DB. It's most likely
// the value will be an ObjectId (hopefully) so we try that first then go from there
exports.get_id_type = function (mongo, collection, doc_id, cb){
if(doc_id){
var ObjectID = require('mongodb').ObjectID;
// if a valid ObjectId we try that, then then try as a string
if(ObjectID.isValid(doc_id)){
mongo.collection(collection).findOne({_id: ObjectID(doc_id)}, function (err, doc){
if(doc){
// doc_id is an ObjectId
cb(null, {'doc_id_type': ObjectID(doc_id), 'doc': doc});
}else{
mongo.collection(collection).findOne({_id: doc_id}, function (err, doc){
if(doc){
// doc_id is string
cb(null, {'doc_id_type': doc_id, 'doc': doc});
}else{
cb('Document not found', {'doc_id_type': null, 'doc': null});
}
});
}
});
}else{
// if the value is not a valid ObjectId value we try as an integer then as a last resort, a string.
mongo.collection(collection).findOne({_id: parseInt(doc_id)}, function (err, doc){
if(doc){
// doc_id is integer
cb(null, {'doc_id_type': parseInt(doc_id), 'doc': doc});
return;
}else{
mongo.collection(collection).findOne({_id: doc_id}, function (err, doc){
if(doc){
// doc_id is string
cb(null, {'doc_id_type': doc_id, 'doc': doc});
}else{
cb('Document not found', {'doc_id_type': null, 'doc': null});
}
});
}
});
}
}else{
cb(null, {'doc_id_type': null, 'doc': null});
}
};
// gets the Databases and collections
exports.get_sidebar_list = function (mongo_db, db_name, cb){
var async = require('async');
var db_obj = {};
// if no DB is specified, we get all DBs and collections
if(db_name == null){
var adminDb = mongo_db.admin();
adminDb.listDatabases(function (err, db_list){
if(db_list){
async.forEachOf(db_list.databases, function (value, key, callback){
var skipped_dbs = ['null', 'admin', 'local'];
if(skipped_dbs.indexOf(value.name) === -1){
mongo_db.db(value.name).listCollections().toArray(function (err, collections){
collections = exports.cleanCollections(collections);
exports.order_array(collections);
db_obj[value.name] = collections;
callback();
});
}else{
callback();
}
}, function (err){
if(err) console.error(err.message);
cb(null, exports.order_object(db_obj));
});
}else{
cb(null, exports.order_object(db_obj));
}
});
}else{
mongo_db.db(db_name).listCollections().toArray(function (err, collections){
collections = exports.cleanCollections(collections);
exports.order_array(collections);
db_obj[db_name] = collections;
cb(null, db_obj);
});
}
};
// order the object by alpha key
exports.order_object = function(unordered){
if(unordered !== undefined){
var ordered = {};
var keys = Object.keys(unordered);
exports.order_array(keys);
keys.forEach(function (key){
ordered[key] = unordered[key];
});
}
return ordered;
};
exports.order_array = function(array){
if(array){
array.sort(function (a, b){
a = a.toLowerCase();
b = b.toLowerCase();
if(a === b)return 0;
if(a > b)return 1;
return-1;
});
}
return array;
};
// render the error page
exports.render_error = function(res, req, err, conn){
var connection_list = req.nconf.connections.get('connections');
var conn_string = '';
if(connection_list[conn] !== undefined){
conn_string = connection_list[conn].connection_string;
}
res.render('error', {
message: err,
conn: conn,
conn_string: conn_string,
connection_list: exports.order_object(connection_list),
helpers: req.handlebars.helpers
});
};
exports.cleanCollections = function(collection_list){
var list = [];
_.each(collection_list, function (item){
list.push(item.name);
});
return list;
};