-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb.js
376 lines (362 loc) · 12.3 KB
/
mongodb.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* MongoDB data store. Depends on
* http://github.com/christkv/node-mongodb-native
*/
//
// N.B. for the latest RQL parser for mongo please refer to https://github.com/dvv/underscore.query
//
var convertNodeAsyncFunction = require('promised-io/promise').convertNodeAsyncFunction,
//Connection = require("mongodb/connection").Connection,
mongo = require('mongodb'),
ObjectID = require('bson/lib/bson/objectid').ObjectID,
Server = mongo.Server,
defer = require("promised-io/promise").defer,
when = require("promised-io/promise").when,
jsArray = require("rql/js-array"),
PreconditionFailed = require("perstore/errors").PreconditionFailed,
DuplicateEntryError = require('perstore/errors').DuplicateEntryError;;
var RQ = require("rql/parser");
//RQ.converters["default"] = exports.converters.auto;
function parse(query, directives){
//dir('MONGO:', query);//, directives);
// parse string to parsed terms
if(typeof query === "string"){
// handle $-parameters
// TODO: consider security issues
//// N.B. considered, treated as evil, bump
//throw new URIError("Sorry, we don't allow raw querystrings. Please, provide the parsed terms instead");
if (directives && directives.parameters) {
query = query.replace(/\$[1-9]/g, function(param){
return directives.parameters[param.substring(1) - 1];
});
}
// poorman regexp? *foo, bar*
/***v = (v.charAt(0) != '*') ? '^' + v : v.substring(1);
v = (v.slice(-1) != '*') ? v + '$' : v.substring(0, v.length-1);***/
query = RQ.parseQuery(query);
}
var options = {
skip: 0,
limit: +Infinity,
lastSkip: 0,
lastLimit: +Infinity
};
var search = {};
// var needBulkFetch = directives && directives.postprocess; // whether to fetch whole dataset to process it here
//if (!needBulkFetch) {
function walk(name, terms) {
// valid funcs
var valid_funcs = ['lt','lte','gt','gte','ne','in','nin','not','mod','all','size','exists','type','elemMatch'];
// funcs which definitely require array arguments
var requires_array = ['in','nin','all','mod'];
// funcs acting as operators
var valid_operators = ['or', 'and'];//, 'xor'];
// compiled search conditions
var search = {};
// iterate over terms
terms.forEach(function(term){
var func = term.name;
var args = term.args;
// ignore bad terms
// N.B. this filters quirky terms such as for ?or(1,2) -- term here is a plain value
if (!func || !args) return;
//dir(['W:', func, args]);
// process well-known functions
// http://www.mongodb.org/display/DOCS/Querying
if (func == 'sort' && args.length > 0) {
options.sort = args.map(function(sortAttribute){
var firstChar = sortAttribute.charAt(0);
var orderDir = 'ascending';
if (firstChar == '-' || firstChar == '+') {
if (firstChar == '-') {
orderDir = 'descending';
}
sortAttribute = sortAttribute.substring(1);
}
return [sortAttribute, orderDir];
});
} else if (func == 'select') {
options.fields = args;
} else if (func == 'values') {
options.unhash = true;
options.fields = args;
// N.B. mongo has $slice but so far we don't allow it
/*} else if (func == 'slice') {
//options[args.shift()] = {'$slice': args.length > 1 ? args : args[0]};*/
} else if (func == 'limit') {
// we calculate limit(s) combination
options.lastSkip = options.skip;
options.lastLimit = options.limit;
// TODO: validate args, negative args
var l = args[0] || Infinity, s = args[1] || 0;
// N.B: so far the last seen limit() contains Infinity
options.totalCount = args[2];
if (l <= 0) l = 0;
if (s > 0) options.skip += s, options.limit -= s;
if (l < options.limit) options.limit = l;
//dir('LIMIT', options);
// grouping
} else if (func == 'group') {
// TODO:
// nested terms? -> recurse
} else if (args[0] && typeof args[0] === 'object') {
if (valid_operators.indexOf(func) > -1)
search['$'+func] = walk(func, args);
// N.B. here we encountered a custom function
// ...
// structured query syntax
// http://www.mongodb.org/display/DOCS/Advanced+Queries
} else {
//dir(['F:', func, args]);
// mongo specialty
if (func == 'le') func = 'lte';
else if (func == 'ge') func = 'gte';
// the args[0] is the name of the property
var key = args.shift();
// the rest args are parameters to func()
if (requires_array.indexOf(func) >= 0) {
args = args[0];
} else {
// FIXME: do we really need to .join()?!
args = args.length == 1 ? args[0] : args.join();
}
// regexps:
if (typeof args === 'string' && args.indexOf('re:') === 0)
args = new RegExp(args.substr(3), 'i');
// regexp inequality means negation of equality
if (func == 'ne' && args instanceof RegExp) {
func = 'not';
}
// TODO: contains() can be used as poorman regexp
// E.g. contains(prop,a,bb,ccc) means prop.indexOf('a') >= 0 || prop.indexOf('bb') >= 0 || prop.indexOf('ccc') >= 0
//if (func == 'contains') {
// // ...
//}
// valid functions are prepended with $
if (valid_funcs.indexOf(func) > -1) {
func = '$'+func;
}
// $or requires an array of conditions
// N.B. $or is said available for mongodb >= 1.5.1
if (name == 'or') {
if (!(search instanceof Array))
search = [];
var x = {};
x[func == 'eq' ? key : func] = args;
search.push(x);
// other functions pack conditions into object
} else {
// several conditions on the same property is merged into one object condition
if (search[key] === undefined)
search[key] = {};
if (search[key] instanceof Object && !(search[key] instanceof Array))
search[key][func] = args;
// equality cancels all other conditions
if (func == 'eq')
search[key] = args;
}
}
// TODO: add support for query expressions as Javascript
});
return search;
}
//dir(['Q:',query]);
search = walk(query.name, query.args);
//dir(['S:',search]);
return [options, search];
}
// this will return a data store
module.exports = function(options){
var ready = defer();
var collection, schema;
function getCollection(db){
db.collection(options.collection, function(err, coll){
if(err){
console.log("Failed to load mongo database collection " + dbOptions.name + " collection " + options.collection + " error " + err.message);
ready.reject(err);
}else{
collection = coll;
ready.resolve(coll);
}
});
}
var dbOptions = require("perstore/util/settings").database;
var url = options.url || dbOptions.url || process.env.MONGOLAB_URI || process.env.MONGOHQ_URL;
if(url){
console.log(url);
mongo.connect(url, function(err, db){
if(err){
console.log('Failed to connect to mongo database ' + url + ' - error: ' + err.message);
ready.reject(err);
}
else {
getCollection(db);
}
});
}
else {
var database = options.database || new mongo.Db(dbOptions.name,
new Server(dbOptions.host, dbOptions.port, {}), {});
database.open(function(err, db){
if(err){
console.log("Failed to load mongo database " + dbOptions.name + " error " + err.message);
ready.reject(err);
}
else{
getCollection(db);
}
});
}
// async helper
function callAsync(method, args){
return convertNodeAsyncFunction(method, true).apply(collection, args);
}
// interface
return {
ready: function(){
return ready;
},
setSchema: function(arg){
schema = arg;
},
get: function(id){
var deferred = defer();
collection.findOne({id: id}, function(err, obj){
if (err) return deferred.reject(err);
if (obj) delete obj._id;
if(obj === null){
obj = undefined;
}
//dir('GOT:', id, obj, query);
//if (???.queryString) {
// var query = ???.queryString;
// if (query)
// obj = jsArray.executeQuery(query, {}, [obj])[0];
//}
deferred.resolve(obj);
});
return deferred;
},
put: function(object, directives){
var deferred = defer();
// N.B. id may come from directives (the primary valid source),
// and from object.id
directives = directives || {};
var id = directives.id || object.id;
if (!object.id) object.id = id;
var search = {id: id};
//dir('PUT:', object, directives.overwrite === false, !id);
if (directives.overwrite === false || !id) {// === undefined) {
// do an insert, and check to make sure no id matches first
collection.findOne(search, function(err, found){
if (err) return deferred.reject(err);
if (found === null) {
if (!object.id) object.id = ObjectID.createPk().toJSON();
collection.insert(object, function(err, obj){
if (err) return deferred.reject(err);
// .insert() returns array, we need the first element
obj = obj && obj[0];
if (obj) delete obj._id;
deferred.resolve(obj.id);
});
} else {
deferred.reject(deferred.reject(new DuplicateEntryError(id + " exists, and can't be overwritten")));
}
});
} else {
collection.update(search, object, {upsert: directives.overwrite}, function(err, obj){
if (err) return deferred.reject(err);
if (obj) delete obj._id;
deferred.resolve(id);
});
}
return deferred;
},
"delete": function(id, directives){
var deferred = defer();
// compose search conditions
//if (id === undefined) id = '?' + (this.req.queryString || '');
//if (id.charAt(0) === '?') {
// var x = parse(id.substring(1), metadata);
// var options = x[0], search = x[1];
//} else {
var search = {id: id};
//}
// remove matching documents
collection.remove(search, function(err, result){
if (err) return deferred.reject(err);
deferred.resolve(undefined);
});
return deferred;
},
query: function(query, directives){
//dir('QRY:', query);
var deferred = defer();
// compose search conditions
var x = parse(query, directives);
var meta = x[0], search = x[1];
// range of non-positive length is trivially empty
//if (options.limit > options.totalCount)
// options.limit = options.totalCount;
if (meta.limit <= 0) {
var results = [];
results.totalCount = 0;
return results;
}
// request full recordset length
//dir('RANGE', options, directives.limit);
// N.B. due to collection.count doesn't respect meta.skip and meta.limit
// we have to correct returned totalCount manually.
// totalCount will be the minimum of unlimited query length and the limit itself
var totalCountPromise = (meta.totalCount) ?
when(callAsync(collection.count, [search]), function(totalCount){
totalCount -= meta.lastSkip;
if (totalCount < 0)
totalCount = 0;
if (meta.lastLimit < totalCount)
totalCount = meta.lastLimit;
// N.B. just like in rql/js-array
return Math.min(totalCount, typeof meta.totalCount === "number" ? meta.totalCount : Infinity);
}) : undefined;
//}
// request filtered recordset
//dir('QRY:', search);
collection.find(search, meta, function(err, cursor){
if (err) return deferred.reject(err);
cursor.toArray(function(err, results){
if (err) return deferred.reject(err);
// N.B. results here can be [{$err: 'err-message'}]
// the only way I see to distinguish from quite valid result [{_id:..., $err: ...}] is to check for absense of _id
if (results && results[0] && results[0].$err !== undefined && results[0]._id === undefined) {
return deferred.reject(results[0].$err);
}
var fields = meta.fields;
var len = results.length;
// damn ObjectIDs!
for (var i = 0; i < len; i++) {
delete results[i]._id;
}
// kick out unneeded fields
if (fields) {
// unhash objects to arrays
if (meta.unhash) {
results = jsArray.executeQuery('values('+fields+')', directives, results);
}
}
// total count
when(totalCountPromise, function(result){
results.count = results.length;
results.start = meta.skip;
results.end = meta.skip + results.count;
results.schema = schema;
results.totalCount = result;
//dir('RESULTS:', results.slice(0,0));
deferred.resolve(results);
});
});
});
return deferred;
}
}
}
module.exports.MongoDB = module.exports;