This repository was archived by the owner on Jul 16, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathQuery.js
460 lines (404 loc) · 14.1 KB
/
Query.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
const { Error: GABError } = require("../Internals/Errors");
const Schema = require("./Schema");
const mpath = require("mpath");
module.exports = class Query {
/**
* An object with methods to interact with a Document
* @constructor
* @param {Document} doc The Document this Query object interacts with
*/
constructor (doc, path) {
/**
* The Document this Query object interacts with
* @type {Document}
* @private
*/
this._doc = doc;
/**
* The mpath string pointing to the current selected object
* @type {string}
*/
this.parsed = path || "";
/**
* The current value being interacted with
* @type {Document.Object|Object}
*/
this._current = path ? mpath.get(path, this._doc._doc) : this._doc._doc;
this._definition = this._doc._model.schema;
if (path) this._shiftSchema(path, true, true);
}
/**
* Change the current selection to a property of said selected value
* @param {string} path The path of the property to select
* @returns {module.Query}
*/
prop (path) {
try {
if (path === "..") {
const index = this.parsed.lastIndexOf(".");
this.parsed = this.parsed.substring(0, index);
this._current = this.parsed === "" ? this._doc._doc : mpath.get(this.parsed, this._doc._doc);
this._shiftSchema(this.parsed, true);
return this;
}
this.parsed += this._parseForString(path);
this._current = mpath.get(this.parsed, this._doc._doc);
this._shiftSchema(path);
return this;
} catch (err) {
throw new GABError("GADRIVER_ERROR", { err }, `Could not parse Query: ${err.message}`);
}
}
/**
* Get a (nested) property from the current selection using an mpath string
* @param {string} key The mpath string pointing to the current value, using template variable labels in the form of "$label"
* @param {object|array} [data] An Object or Array mapping variable labels to strings or ID's. All strings are ran through Query._findById first to check for ID matches.
* @returns {*}
*/
get (key, data) {
try {
let found = true;
if (!data || (data.constructor !== Object && data.constructor !== Array)) return mpath.get(this.parsed + this._parseForString(key), this._doc._doc);
const splitted = key.split(".");
let parsed = "";
splitted.forEach(piece => {
const val = data[piece.replace("$", "")];
if (!val || !piece.startsWith("$")) {
parsed += this._parseForString(piece, parsed);
} else {
const index = this._findById(val, mpath.get(this.parsed + this._parseForString(parsed), this._doc._doc));
if (index === null) found = false;
parsed += this._parseForString(String(index || val), parsed);
}
});
return found ? mpath.get(this.parsed + this._parseForString(parsed), this._doc._doc) : undefined;
} catch (err) {
throw new GABError("GADRIVER_ERROR", { err }, `Could not parse Query: ${err.message}`);
}
}
/**
* Selects a subdocument of the current array or map by _id
* @param {string} path
* @param {string} id The _id value of the subdocument to select
* @returns {module.Query}
*/
id (path, id) {
try {
if (id !== undefined) this.prop(path);
else id = path;
if (!this._canId()) return this.prop(id);
const index = this._definition.isMap ? id : this._findById(id);
this.parsed += this._parseForString(index);
this._current = index === null ? undefined : mpath.get(this.parsed, this._doc._doc);
return this;
} catch (err) {
throw new GABError("GADRIVER_ERROR", { err }, `Could not parse Query: ${err.message}`);
}
}
/**
* Set the value of a path
* @param {string} path
* @param {*} value
* @returns {module.Query}
*/
set (path, value) {
try {
if (value !== undefined) {
const parsed = this.parsed + this._parseForString(path);
const definition = this._shiftSchema(path, false, false);
this._validate(definition, parsed, value);
this._writeValue(parsed, definition.cast(value));
} else {
this._validate(this._definition, this.parsed, path, !this._definition.isArray || isNaN(this.parsed.substring(this.parsed.lastIndexOf(".") + 1)));
this._writeValue(this.parsed, this._definition.cast(path));
}
return this;
} catch (err) {
if (err.constructor === Schema.ValidationError) throw err;
throw new GABError("GADRIVER_ERROR", { err }, `Could not set atomics or parse Query: ${err.message}`);
}
}
/**
* Gets a subdocument of the currently selected array by _id
* @param {string} id The _id value of the subdocument to get
* @returns {*}
*/
getById (id) {
if (!this._canId()) return this.prop(id);
return this.get("$0", [this._findById(id)]);
}
/**
* Pushes a value to the currently selected array or map
* @param {string|*} path The path of the array to be pushed to, or the value if the selected array should be used
* @param {*} value The value to be pushed to the currently selected array or map
* @returns {module.Query}
*/
push (path, value) {
let parsedPath = this.parsed;
let definition = this._definition;
if (value === undefined) {
value = path;
} else {
parsedPath = this.parsed + this._parseForString(path);
definition = this._shiftSchema(path, false, false);
}
const targetObject = mpath.get(parsedPath, this._doc._doc);
if (!this._canId(targetObject)) return this;
if (definition.isMap && value && !value._id) throw new Schema.ValidationError({ type: "required", value: value._id, path: `${parsedPath}._id`, definition });
if (value && (definition.isArray ? this._findById(value._id, targetObject) : targetObject[value._id])) {
throw new Schema.ValidationError({ type: "duplicate", value: value._id, path: `${parsedPath}._id`, definition });
}
const obj = definition.type.key === "schema" ? definition.type.schema.build(value) : value;
this._validate(definition, parsedPath, obj, false);
if (definition.isMap) {
const ID = obj._id;
this._writeValue(`${parsedPath}${this._parseForString(ID, parsedPath)}`, obj);
} else if (definition.isArray) {
this._push(parsedPath, obj);
}
return this;
}
/**
* Pulls a value from an array or map
* @param {string} path
* @param {string|number|object} idOrObject
* @returns {module.Query}
*/
pull (path, idOrObject) {
let parsedPath = this.parsed;
let definition = this._definition;
if (idOrObject === undefined) {
idOrObject = path;
} else {
parsedPath = this.parsed + this._parseForString(path);
definition = this._shiftSchema(path, false, false);
}
if (idOrObject === undefined || idOrObject === null) return this;
const targetObject = mpath.get(parsedPath, this._doc._doc);
if (!this._canId(targetObject)) return this;
switch (definition.type.key) {
case "string":
case "number": {
this._pullAll(parsedPath, idOrObject);
break;
}
case "schema":
case "object":
if (idOrObject._id !== undefined) idOrObject = idOrObject._id;
if (definition.isArray) {
this._pull(parsedPath, idOrObject);
} else if (definition.isMap) {
this._unset(parsedPath + this._parseForString(String(idOrObject), parsedPath));
}
break;
default:
this._unset(parsedPath + this._parseForString(String(idOrObject), parsedPath));
}
return this;
}
/**
* Removes a value from the document
* @param {string} [path]
* @returns {module.Query}
*/
remove (path) {
let parsedPath = this.parsed;
if (path !== undefined) parsedPath = this.parsed + this._parseForString(path);
const parent = mpath.get(parsedPath.substring(0, parsedPath.lastIndexOf(".")), this._doc._doc);
if (path === undefined && Array.isArray(parent)) {
this._pull(parsedPath.substring(0, parsedPath.lastIndexOf(".")), this.val._id);
} else {
this._unset(parsedPath);
}
return this;
}
/**
* Increments the value at the given path by a given amount
* @param {string|number} path
* @param {number} [amount]
* @returns {module.Query}
*/
inc (path, amount) {
if (amount === undefined && typeof path === "string") amount = 1;
if (path === undefined && amount === undefined) path = 1;
if (amount !== undefined) {
const parsed = this.parsed + this._parseForString(path);
const value = mpath.get(parsed, this._doc) + amount;
if (isNaN(value)) return this;
const definition = this._shiftSchema(path, false, false);
this._validate(definition, parsed, value);
this._inc(parsed, amount);
} else {
const value = mpath.get(this.parsed, this._doc) + path;
if (isNaN(value)) return this;
this._validate(this._definition, this.parsed, value);
this._inc(this.parsed, path);
}
return this;
}
get clone () {
return new Query(this._doc, this.parsed);
}
/**
* The current raw value this Query has selected
* @returns {*}
* @readonly
*/
get val () {
return this._current === undefined ? undefined : mpath.get(this.parsed, this._doc._doc);
}
/**
* Parse a mpath piece to be suffixed to the current mpath
* @param {string} str The piece to be parsed
* @param {string} [obj=module.Query.parsed] The mpath root the piece is to be suffixed onto
* @returns {string}
* @private
*/
_parseForString (str, obj = this.parsed) {
return obj === "" ? str : `.${str}`;
}
/**
* Check if the current value or obj can be used to find a subdocument by ID
* @param {object} [obj=model.Query._current] The object to be checked
* @returns {boolean}
* @private
*/
_canId (obj = this._current) {
return obj && (Array.isArray(obj) || obj.constructor === Object);
}
/**
* Internal function to find an array value by its _id property, returns null if value is not found
* @param {string} id The ID to test the array values against
* @param {object} [obj=model.Query._current] The array that holds the value to be found
* @returns {*}
* @private
*/
_findById (id, obj = this._current) {
if (this._canId(obj)) {
const index = obj.findIndex(prop => prop._id === id);
return index === -1 ? null : index;
} else {
return null;
}
}
/**
* Internal function to write a value to a specified path
* @param {string} path The mpath string of the value to be set
* @param {*} val The value to be set
* @private
*/
_writeValue (path, val) {
val = this._definition.cast ? this._definition.cast(val) : val;
mpath.set(path, val, this._doc._doc);
mpath.set(path, val, this._doc);
this._doc._setAtomic(path, val, "$set");
}
/**
* Internal function to push a value to an array at the specified path
* @param {string} path The mpath string of the array the value is to be pushed to
* @param {*} val The value to push to the array
* @private
*/
_push (path, val) {
val = this._definition.cast ? this._definition.cast(val) : val;
mpath.get(path, this._doc._doc).push(val);
this._doc._setAtomic(path, val, "$push");
}
/**
* Internal function to pull a value with a specified ID from an array at the specified path
* @param {string} path The mpath string of the array to pull the value from
* @param {string} id The ID of the value to pull from the array
* @private
*/
_pull (path, id) {
const array = mpath.get(path, this._doc._doc);
const indexOfID = array.findIndex(a => a._id === id);
if (indexOfID === -1) return;
array.splice(indexOfID, 1);
this._doc._setAtomic(path, id, "$pull");
}
_pullAll (path, value) {
const array = mpath.get(path, this._doc._doc);
if (!Array.isArray(array)) return;
const indexOfValue = array.indexOf(value);
if (indexOfValue === -1) return;
array.splice(indexOfValue, 1);
this._doc._setAtomic(path, value, "$pullAll");
}
/**
* Internal function to remove a value from an object
* @param {string} path
* @private
*/
_unset (path) {
const childPathSeparatorIndex = path.lastIndexOf(".");
const parentPath = path.substring(0, childPathSeparatorIndex);
const childPath = path.substring(childPathSeparatorIndex + 1);
const parent = mpath.get(parentPath, this._doc._doc);
if (Array.isArray(parent)) {
parent.splice(childPath, 1);
this._doc._setAtomic(path, "", "$unset");
} else {
delete parent[childPath];
delete mpath.get(parentPath, this._doc)[childPath];
this._doc._setAtomic(path, "", "$unset");
}
}
/**
* Internal function to increment a value at the specified path
* @param {string} path
* @param {number} amount
* @private
*/
_inc (path, amount) {
const value = mpath.get(path, this._doc._doc);
mpath.set(path, value + amount, this._doc._doc);
mpath.set(path, value + amount, this._doc._doc);
this._doc._setAtomic(path, amount, "$inc");
}
/**
* Shift the currently selected Definition to the desired path
* @param {string} paths The mpath of the to-be selected Definition
* @param {boolean} absolute If set to true, the paths will be applied from the root schema
* @param {boolean} mutate If set to true, the current definition will be shifted
* @returns {Definition|null}
* @private
*/
_shiftSchema (paths, absolute, mutate = true) {
let definition = this._definition;
let outofscope = false;
if (absolute) definition = this._doc._model.schema;
const parsedArray = paths.split(".");
parsedArray.forEach(path => {
if (definition && definition.type && (definition.type.key === "object" || definition.type.key === "mixed")) {
outofscope = true;
return;
}
if (definition instanceof Schema) definition = definition._definitions.get(path);
else if (definition && definition.type.key === "schema" && definition.type.schema._definitions.has(path)) definition = definition.type.schema._definitions.get(path);
else if (!definition || (!definition.isMap && !definition.isArray)) definition = null;
});
if (mutate) this._definition = definition;
if (outofscope) definition.outofscopeflag = true;
return definition;
}
/**
* Validates a value against a given definition, prepares the ValidationError (if created) to be thrown, returns null otherwise
* @param {Definition} definition
* @param {string} path
* @param {*} value
* @param {boolean} [absolute=true]
* @returns {null}
* @private
*/
_validate (definition, path, value, absolute = true) {
if (definition.outofscopeflag) return;
const error = definition.validate(value, absolute);
if (error && (!Array.isArray(error) || error.length)) {
error.path = path;
throw new Schema.ValidationError(error, this._doc);
} else {
return null;
}
}
};