-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.js
199 lines (199 loc) · 6.27 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
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
// Database gateway (db)
// built for use by multiple projects, each with their own db.
var myname = "db: ";
// --
module.exports = function(settings, port, dbCollections){
var exports = {};
var db = exports;
var logHelp = require("./log.js");
var _ = require("underscore");
// --
if(!settings) return log9(myname+"No settings object?");
if(!port) return log9(myname+"No port specified?");
if(!dbCollections) return log9(myname+"No dbCollections?");
// ----------------------------
// IMPORTANT APP-SPECIFIC VARS.
var MONGO_HOST = settings.mongo_host || "127.0.0.1";
var MONGO_PORT = settings.mongo_port || 27017;
var MONGO_USER = settings.mongo_user || "ProjUser";
var MONGO_PASS = settings.mongo_pass || "Password1234";
var MONGO_DBNAME = settings.mongo_dbname || "Project";
// --
if(port === "NO_PORT") port = "";
MONGO_USER += port;
MONGO_PASS += port;
MONGO_DBNAME += port;
// ----------------------------
var MAX_QUERY_RESULTS = 100; // >= 2
var MAX_QUERY_LOOP_COUNT = 100;
var ENABLE_DB_PROFILING = false;
var UPDATE_DB_INDEXES_AFTER_CONNECTION = false; // only need to do this once.
var DB_PROFILING_PRFILE = 1;
var DB_PROFILING_SLOW_MS = 50;
// -- MongoDB!
var mongo = require("mongodb");
var Db = mongo.Db;
var Server = mongo.Server;
// --
var dbServer = new Server(MONGO_HOST, MONGO_PORT, {auto_reconnect: true});
var dbDB = new Db(MONGO_DBNAME, dbServer, {native_parser:false, safe:true});
// --
var dbRoot = ""; // root database
var dbReady = false;
var dbReadyCb = null;
// --
dbDB.open(function(err, db) {
if(err) return logErr(err, myname+"could not open db");
dbRoot = db;
db.authenticate(MONGO_USER, MONGO_PASS, function(err, dbauth){
if(err) return logErr(err, myname+"could not authenticate with db");
// --
if(ENABLE_DB_PROFILING){
db.command({
profile: DB_PROFILING_PRFILE,
slowms : DB_PROFILING_SLOW_MS
}, function(err){
if(err) return logErr(myname+"Unable to setup Db profiling.");
if(DB_PROFILING_PRFILE > 0){
log2(myname+"Db profiling enabled.");
}else{
log2(myname+"profiling disabled.");
}
});
}
// --
var total = _.size(dbCollections);
var sofar = 0;
_.each(dbCollections, function(val, key){
db.collection(key, function(err, collection){
if(err) return logErr(err, myname+"could not open collection: "+key);
dbCollections[key] = collection;
//console.log(myname+"opened db collection: "+key);
sofar++;
if(sofar === total){
//console.log(myname+"all collections ready.");
if(UPDATE_DB_INDEXES_AFTER_CONNECTION){
createDBObjectIndexes();
}
dbReady = true;
if(dbReadyCb){
dbReadyCb();
}
}
});
});
});
});
exports.ready = function(cb){
dbReadyCb = cb;
if(dbReady) return cb();
};
// --
function createDBObjectIndexes(){
console.log(myname+"ensuring db index...");
// dbCollections.SessionEvents.ensureIndex('_session', {safe:true}, function(err, indexName) {
// if(err) logErr(err, myname+"unable to update object index.");
// log1(myname+"(+) index "+indexName);
// });
// dbCollections.SessionInfo.ensureIndex('_urlname', {safe:true}, function(err, indexName) {
// if(err) logErr(err, myname+"unable to update object index.");
// log1(myname+"(+) index "+indexName);
// });
}
// --
exports.query = function(dbObj, query, fields, skip, limit, orderby, callback){
limit = Math.max(0, Math.min(limit, MAX_QUERY_RESULTS));
orderby = orderby||{_id: -1};
var q2 = {$query: query, $orderby: orderby};
dbObj.find(q2, fields||{}, skip, limit, function (err, cursor) {
if(err){
return callback("query failed.");
}
// cursor.count(function(err,count){
// console.log("err?", err);
// console.log("count?", count);
// });
cursor.toArray(function(err, docs) {
return callback(err, docs);
});
});
};
exports.queryAndIterate = function(dbObj, query, fields, skip, limit, orderby, callback){
if(!dbReady) return logErrCB(myname+"Not ready.", cb);
var loopCount = 0;
limit = limit||0;
skip = skip ||0;
function fetch(){
if(limit && skip >= limit) return callback(null, null);
query(dbObj, query, fields, skip, 2, orderby, function(err, docs){
if(err || !docs) return logErrCB(err, cb);
skip++;
if(docs.length > 0){
callback(null, docs[0], fetch);
}else{
callback(null, null);
}
});
}
fetch();
};
exports.count = function(dbObj, query, callback){
if(!dbReady) return logErrCB(myname+"Not ready.", cb);
dbObj.count(query, function (err, info) {
if(err){
return callback("count failed.");
}
return callback(null, info);
});
};
exports.distinct = function(dbObj, distinct, query, sortby, sortdir, maxormin, skip, limit, callback){
if(!dbReady) return logErrCB(myname+"Not ready.", cb);
limit = Math.max(0, Math.min(limit, MAX_QUERY_RESULTS));
var grp = {_id: "$"+distinct};
grp["max_"+sortby] = {$max: "$"+sortby};
grp["min_"+sortby] = {$min: "$"+sortby};
var srt = {};
srt[maxormin+"_"+sortby] = sortdir;
dbObj.aggregate([
{$match: query},
{$group : grp},
{$sort: srt},
{$skip: skip},
{$limit: limit}
], function(err, result) {
if(err) logErr(err, "db aggregation error");
callback(err, result);
});
};
exports.findByID = function(dbObj, id, cb){
if(!dbReady) return logErrCB(myname+"Not ready.", cb);
id = db.asMongoObjectID(id);
// var idStr = id+"";
// if(idStr && idStr.length === 24){
// try{
// var mID = mongo.ObjectID(idStr);
// id = mID;
// }catch(ex){
// console.log("looks like mongoID, but couldn't parse it.");
// console.log(ex);
// }
// }
dbObj.findOne({_id: id}, {}, cb);
};
// --
exports.asMongoObjectID = function(id){
var idStr = id+"";
if(idStr && idStr.length === 24){
try{
var mID = mongo.ObjectID(idStr);
id = mID;
}catch(ex){
console.log("looks like mongoID, but couldn't parse it.");
console.log(ex);
}
}
return id;
};
// --
return exports;
};