-
Notifications
You must be signed in to change notification settings - Fork 3
/
flatmatcher.js
272 lines (241 loc) · 10.4 KB
/
flatmatcher.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
var FlatMatcher = (function () {
/**
* Returns the correct MongoDB query syntax JSON for flat JSON object of properties/values to be used
* as Mongo query predicate
*
* @see FlatMatcher#buildMatcherLookup
* @param {Object} matchArgs JSON properties/values to use in a query matching documents of this Model's Schema
* @api public
*/
var getMatchObj = function (matchArgs) {
// Helpers
var isArray = function (value) {
return Object.prototype.toString.apply(value) === '[object Array]';
};
/**
* Internal helper supporting FlatMatcher#getMatcher.
*
* @see FlatMatcher#getMatcher
* @param {String} pathString dot-notation prefix for current traversal of properties in the Schema
* @param {Object} tree the Schema#tree property for the Schema
* @param {Object} matcherLookup the return value mapping flat property names to dot-notation paths, value casting functions and isArray flags
* @param {Number} depth depth of recursion of call
* @api private
*/
var buildMatcherLookup = function (pathString, tree, matcherLookup, depth) {
// Guard against arbitrarily deep recursion for properties that are
// object/Schema types with properties that are other object/Schema types forming a cycle
if (depth > maxDepth) {
return matcherLookup;
}
depth += 1;
// Helpers
var MatcherLookupEntry = function (p, c, a) {
return {pathString : p, caster : c, isArray : a};
};
// Guard against overwriting existing entries. As documented, this function will only behave as expected
// if all properties in Schema at all levels have unique names. So the function creates an entry only for
// the first instance of any name encountered without qualifying it with its full dot notation path (the
// entire point of using the Matcher.) But it will create dot-notation qualified entries if there is a
// clash, so these can be accessed by full path.
var makeLookupEntry = function (prop, pathString, caster, isArrayVal) {
var entry = MatcherLookupEntry(pathString, caster, isArrayVal);
if (! matcherLookup.hasOwnProperty(prop)) {
matcherLookup[prop] = entry;
}
else {
matcherLookup[pathString] = entry;
}
};
var isArray = function (value) {
return Object.prototype.toString.apply(value) === '[object Array]';
};
var isFunction = function (value) {
return typeof value === 'function';
};
var isObject = function (value, literal) {
var type = typeof value, test = !!value && type === 'object';
return test && literal ? value.constructor === Object : test;
};
var isMongooseSchema = function (v) {
return (isObject(v) && v.hasOwnProperty('paths') && v.hasOwnProperty('tree'));
};
var mongooseSchemaTreeEquals = function (s1, s2) {
var objLen = function (o) {
var l = 0;
for (prop in o) {
l += 1;
}
return l;
};
var isSamePropertyVal = function (pv1, pv2) {
if ( (isArray(pv1) && isArray(pv2)) || (isFunction(pv1) && isFunction(pv2)) ||
(isObject(pv1) && isObject(pv2)) || (isMongooseSchema(pv1) && isMongooseSchema(pv2)) ) {
return true;
}
return false;
};
// Compare the tree property of each Schema object for same length
var l1, l2;
if ((l1 = objLen(s1)) !== (l2 = objLen(s2))) {
return false;
}
// Iterate each tree, insuring same property names and Schema data types
for (prop in s1) {
if (! prop in s2) {
return false;
}
else if (! isSamePropertyVal(s1[prop], s2[prop])) {
return false;
}
}
return true;
};
var noOpCaster = function (value) {
return value;
};
// /Helpers
var propVal = null;
var curPathString = pathString;
var isArrayVal = false;
var literal = 1;
for (prop in tree) {
pathString = curPathString;
// Build dot-notation path
if (pathString.length > 0) {
pathString += '.';
}
pathString += prop;
// NOTE: Depends on internal Schema property 'tree' property implementation.
// tree is an object with a structure mirroring the Schema definition.
// Properties are fields in the Schema. Their value is:
// - a casting function for the type of data the field holds, if the field is a simple type
// i.e. string, number, boolean or date
// - a nested object if the field contains nested objects
// - a nested MongooseSchema (a specific object type) if the field holds nested Schemas
// - an array if the field holds any type of value in an embedded array
propVal = tree[prop];
if (isArrayVal = isArray(propVal)) {
// Arrays can be empty in Mongoose, contain a compound type (object or Schema) or a simple type
// Arrays are empty or homogenous (have values of only one type). So if the tree value is an array:
// - if the array in the tree contains a function, then the function is the caster
// - if the array in the tree contains objects or Schema objects, create two entries:
// - one is recurse to build dot notation for matching single objects in arrays
// - one is an entry at the array level without dot notation into the object, passing literal through
// because matching more than one object using $in requires matching on an array of literal values
// - if the array in the tree is empty (which is legal in Mongoose), then we can't know what type the
// array may contain, so create an entry that passes through literal values to match exactly
if (propVal.length === 0)
{
makeLookupEntry(prop, pathString, noOpCaster, isArrayVal);
continue;
}
else {
propVal = propVal[0];
}
}
if (isFunction(propVal, literal)) {
makeLookupEntry(prop, pathString, propVal, isArrayVal);
}
else if(isMongooseSchema(propVal)) {
// Embedded object in array, create entry for literal match as per comment above
if (isArrayVal) {
makeLookupEntry(prop, pathString, noOpCaster, isArrayVal);
}
// If embedded Schema is a a child property of the same Schema, we know we have endless recursion,
/// so just support one level of recursion from here
if (mongooseSchemaTreeEquals(tree, propVal.tree) && depth < maxDepth) {
depth = maxDepth;
}
// Recurse
matcherLookup = buildMatcherLookup(pathString, propVal.tree, matcherLookup, depth);
}
else if (isObject(propVal)) {
if (isArrayVal) {
makeLookupEntry(prop, pathString, noOpCaster, isArrayVal);
}
// Always recurse on embedded plain objects (not Schemas), because these aren't as much "types"
// as Schemas so there isn't as clear a case of identifying self-reference children and cycles. So
// just allow and guard against stack overflow
matcherLookup = buildMatcherLookup(pathString, propVal, matcherLookup, depth);
}
}
return matcherLookup;
};
// /Helpers
// The JSON set of document properties/values to return
var pathString = '';
var matcherLookup = {};
var depth = 0;
matcherLookup = buildMatcherLookup(pathString, this.tree, matcherLookup, depth);
// JSON in valid MongoDB dot-notation and "$in" syntax to be match pred in find(), update() and delete() Mongo calls
var matchObj = {};
var propVal = null;
var mlEntry = null;
var j = 0;
for (prop in matchArgs) {
if (matcherLookup.hasOwnProperty(prop)) {
propVal = matchArgs[prop];
mlEntry = matcherLookup[prop];
// Handle embedded array cases
if (mlEntry.isArray) {
// Client can pass in single values or array of values to match against array fields
// Single values match with standard dot notation, as MongoDB transparently searches the embedded array for
// all objects matching the predicate value
// Multiple values work like SQL "IN", meaning MongoDB searches the embedded array for all objects matching *any*
// of the values in the predicate. Multiple values require "$in" operator in matcher.
if (isArray(propVal)) {
// Loop over all values, cast each one
for (j = 0; j < propVal.length; j += 1) {
propVal[j] = mlEntry.caster(propVal[j]);
}
matchObj[mlEntry.pathString] = {"$in" : propVal};
}
else {
matchObj[mlEntry.pathString] = mlEntry.caster(propVal);
}
}
// Matching single value field
else {
matchObj[mlEntry.pathString] = mlEntry.caster(propVal);
}
}
}
return matchObj;
};
/**
* Default maximum recursion depth for building flat matcher from Schema.tree. Protects against cycles.
* @api private
*/
var maxDepth = 5;
/**
* Gets the maximum depth the FlatMatcher will recurse on Schema to build matcher from args
* @api public
*/
var getMaxDepth = function () {
return maxDepth;
};
/**
* Sets the maximum depth the FlatMatcher will recurse on Schema to build matcher from args. Default is 5.
* Note: protects against cycles which will never exit recursion, so should be set to some reasonable value.
*
* @param {Number} depth value for maximum recursion depth
* @api public
*/
var setMaxDepth = function (depth) {
maxDepth = depth;
};
return {
plugin : function (schema, opts) {
schema['getMatchObj'] = getMatchObj;
// Store schema tree as child prop of method, so it's a closure
// that includes this property of the schema being decorated.
// Must do this because 'this' context of getMatchObj() calls are its
// own scope and schema.tree wouldn't otherwise be available.
schema.getMatchObj.tree = schema.tree;
schema['setMaxDepth'] = setMaxDepth;
schema['getMaxDepth'] = getMaxDepth;
}
};
}());
exports.plugin = FlatMatcher.plugin;