-
Notifications
You must be signed in to change notification settings - Fork 20
/
mongoParse.js
315 lines (278 loc) · 9.67 KB
/
mongoParse.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
var mapValues = require("./mapValues")
var matches = require("./matches")
var DotNotationPointers = exports.DotNotationPointers = require("./DotNotationPointers")
// routerDefinition should be a function that gets a Route object as its `this` context
var Parse = function(mongoQuery) {
this.parts = parseQuery(mongoQuery)
}
Parse.prototype = {}
// instance methods
Parse.prototype.map = function(callback) {
return map(this.parts, callback)
}
Parse.prototype.mapValues = function(callback) {
return compressQuery(mapValues(this.parts, callback))
}
Parse.prototype.matches = function(document, validate) {
return matches(this.parts, document, validate)
}
exports.parse = function(mongoQuery) {
return new Parse(mongoQuery)
}
exports.inclusive = function(mongoProjection) {
return isInclusive(mongoProjection)
}
exports.search = function(documents, query, sort, validate) {
var parsedQuery = new Parse(query)
return documents.filter(function(doc) {
return parsedQuery.matches(doc, validate)
}).sort(function(a,b) {
for(var k in sort) {
var result = sortCompare(a,b,k)
if(result !== 0) {
if(sort[k]<0)
result = -result
return result
}
}
return 0 // if it got here, they're the same
})
}
var complexFieldIndependantOperators = {$and:1, $or:1, $nor:1}
var simpleFieldIndependantOperators = {$text:1, $comment:1}
// compares two documents by a single sort property
function sortCompare(a,b,sortProperty) {
var aVal = DotNotationPointers(a, sortProperty)[0].val // todo: figure out what mongo does with multiple matching sort properties
var bVal = DotNotationPointers(b, sortProperty)[0].val
if(aVal > bVal) {
return 1
} else if(aVal < bVal) {
return -1
} else {
return 0
}
}
function isInclusive(projection) {
for(var k in projection) {
if(!projection[k]) {
if(k !== '_id') {
return false
}
} else if(k === '$meta') {
return true
} else if(projection[k]) {
if(projection[k] instanceof Object && ('$elemMatch' in projection[k] || '$slice' in projection[k])) {
// ignore
} else {
return true
}
}
}
}
function parseQuery(query) {
if(query instanceof Function || typeof(query) === 'string') {
if(query instanceof Function) {
query = "("+query+").call(obj)"
}
var normalizedFunction = new Function("return function(){var obj=this; return "+query+"}")()
return [new Part(undefined, '$where', normalizedFunction)]
}
// else
var parts = []
for(var key in query) {
if(key in complexFieldIndependantOperators) { // a field-independant operator
var operator = key
var operands = query[key]
var innerParts = []
operands.forEach(function(operand) {
innerParts.push(new Part(undefined, '$and', [operand], parseQuery(operand)))
})
parts.push(new Part(undefined, operator, query[key], innerParts))
} else if(key in simpleFieldIndependantOperators) {
parts.push(new Part(undefined, key, query[key]))
} else { // a field
var field = key
if(isObject(query[key]) && fieldOperand(query[key])) {
for(var innerOperator in query[key]) {
var innerOperand = query[key][innerOperator]
parts.push(parseFieldOperator(field, innerOperator, innerOperand))
}
} else { // just a value, shorthand for $eq
parts.push(new Part(field, '$eq', query[key]))
}
}
}
return parts
}
function map(parts, callback) {
var result = {}
parts.forEach(function(part) {
if(part.operator === '$and') {
var mappedResult = map(part.parts, callback)
} else if(part.operator in complexFieldIndependantOperators) {
var mappedParts = part.parts.map(function(part) {
return map(part.parts, callback)
})
var mappedResult = {$or: mappedParts}
} else {
var value = {}; value[part.operator] = part.operand
var cbResult = callback(part.field, value)
var mappedResult = processMappedResult(part, cbResult)
}
mergeQueries(result, mappedResult)
})
compressQuery(result)
return result
function processMappedResult(part, mappedResult) {
if(mappedResult === undefined) {
var result = {}
if(part.field === undefined) {
result[part.operator] = part.operand
} else {
var operation = {}
operation[part.operator] = part.operand
result[part.field] = operation
}
return result
} else if(mappedResult === null) {
return {}
} else {
return mappedResult
}
}
}
// merges query b into query a, resolving conflicts by using $and (or other techniques)
function mergeQueries(a,b) {
for(var k in b) {
if(k in a) {
if(k === '$and') {
a[k] = a[k].concat(b[k])
} else {
var andOperandA = {}; andOperandA[k] = a[k]
var andOperandB = {}; andOperandB[k] = b[k]
var and = {$and:[andOperandA,andOperandB]}
delete a[k]
mergeQueries(a,and)
}
} else {
a[k] = b[k]
}
}
}
// decanonicalizes the query to remove any $and or $eq that can be merged up with its parent object
// compresses in place (mutates)
var compressQuery = exports.compressQuery = function (x) {
for(var operator in complexFieldIndependantOperators) {
if(operator in x) {
x[operator].forEach(function(query){
compressQuery(query)
})
}
}
if('$and' in x) {
x.$and.forEach(function(andOperand) {
for(var k in andOperand) {
if(k in x) {
if(!(x[k] instanceof Array) && typeof(x[k]) === 'object' && k[0] !== '$') {
for(var operator in andOperand[k]) {
if(!(operator in x[k])) {
x[k][operator] = andOperand[k][operator]
delete andOperand[k][operator]
if(Object.keys(andOperand[k]).length === 0)
delete andOperand[k]
}
}
}
} else {
x[k] = andOperand[k]
delete andOperand[k]
}
}
})
x.$and = filterEmpties(x.$and)
if(x.$and.length === 0) {
delete x.$and
}
}
if('$or' in x) {
x.$or = filterEmpties(x.$or)
if(x.$or.length === 0) {
delete x.$or
} else if(x.$or.length === 1) {
var orOperand = x.$or[0]
delete x.$or
mergeQueries(x,orOperand)
}
}
for(var k in x) {
if(x[k] && x[k].$eq !== undefined && Object.keys(x[k]).length === 1) {
x[k] = x[k].$eq
}
if(x[k] && x[k].$elemMatch !== undefined) {
compressQuery(x[k].$elemMatch)
}
}
return x
function filterEmpties(a) {
return a.filter(function(operand) {
if(Object.keys(operand).length === 0)
return false
else
return true
})
}
}
// returns a Part object
function parseFieldOperator(field, operator, operand) {
if(operator === '$elemMatch') {
var elemMatchInfo = parseElemMatch(operand)
var innerParts = elemMatchInfo.parts
var implicitField = elemMatchInfo.implicitField
} else if(operator === '$not') {
var innerParts = parseNot(field, operand)
} else {
var innerParts = []
}
return new Part(field, operator, operand, innerParts, implicitField)
}
// takes in the operand of the $elemMatch operator
// returns the parts that operand parses to, and the implicitField value
function parseElemMatch(operand) {
if(fieldOperand(operand)) {
var parts = []
for(var operator in operand) {
var innerOperand = operand[operator]
parts.push(parseFieldOperator(undefined, operator, innerOperand))
}
return {parts: parts, implicitField: true}
} else { // non-field operators ( stuff like {a:5} or {$and:[...]} )
return {parts: parseQuery(operand), implicitField: false}
}
}
function parseNot(field, operand) {
var parts = []
for(var operator in operand) {
var subOperand = operand[operator]
parts.push(parseFieldOperator(field, operator, subOperand))
}
return parts
}
// returns true for objects like {$gt:5}, {$elemMatch:{...}}
// returns false for objects like {x:4} and {$or:[...]}
function fieldOperand(obj) {
for(var key in obj) {
return key[0] === '$' && !(key in complexFieldIndependantOperators) // yes i know this won't actually loop
}
}
// returns true if the value is an object and *not* an array
function isObject(value) {
return value instanceof Object && !(value instanceof Array)
}
var Part = function(field, operator, operand, parts, implicitField) {
if(parts === undefined) parts = []
this.field = field
this.operator = operator
this.operand = operand
this.parts = parts
this.implicitField = implicitField // only used for a certain type of $elemMatch part
}