@atscript/db-mongo 0.1.179 → 0.1.181
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +122 -74
- package/dist/index.d.cts +39 -16
- package/dist/index.d.mts +39 -16
- package/dist/index.mjs +122 -74
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -134,6 +134,13 @@ function duplicateKeyViolation(error) {
|
|
|
134
134
|
//#endregion
|
|
135
135
|
//#region src/lib/collection-patcher.ts
|
|
136
136
|
/**
|
|
137
|
+
* `items` of an array operator as an aggregation operand. Always a
|
|
138
|
+
* `$literal`: the items are data, and an aggregation expression would read a
|
|
139
|
+
* `$`-prefixed string in them as a field path — storing another field's
|
|
140
|
+
* value, a secret one included, where the string was sent (since 0.1.180).
|
|
141
|
+
*/
|
|
142
|
+
const literal = (items) => ({ $literal: items });
|
|
143
|
+
/**
|
|
137
144
|
* True when a user value contains a `$`-prefixed string or object key — the
|
|
138
145
|
* shapes aggregation `$set` would evaluate as field paths / operators instead
|
|
139
146
|
* of treating as literal data. Caller must wrap such values in `{ $literal }`.
|
|
@@ -168,13 +175,16 @@ function incExpr(field, by) {
|
|
|
168
175
|
* Supported high‑level operations for *top‑level arrays* (see the attached
|
|
169
176
|
* spreadsheet in the chat):
|
|
170
177
|
*
|
|
171
|
-
* | Payload field |
|
|
172
|
-
*
|
|
173
|
-
* | `$replace` |
|
|
174
|
-
* | `$insert` | `$
|
|
175
|
-
* | `$upsert` |
|
|
176
|
-
* | `$update` | `$
|
|
177
|
-
* | `$remove` | `$
|
|
178
|
+
* | Payload field | Pipeline expression | Purpose |
|
|
179
|
+
* |---------------|----------------------------|------------------------------------------------------|
|
|
180
|
+
* | `$replace` | `$literal` | Replace the whole array (stands alone). |
|
|
181
|
+
* | `$insert` | `$concatArrays`, `$reduce` | Append; a stored key / unique value is skipped. |
|
|
182
|
+
* | `$upsert` | `$reduce` + `$map` | Change the element of a *key* in place, or append. |
|
|
183
|
+
* | `$update` | `$reduce` + `$map` | Change the elements matched by *key*. |
|
|
184
|
+
* | `$remove` | `$filter` | Remove by value or by *key*. |
|
|
185
|
+
*
|
|
186
|
+
* The result is the array the read-modify-write of the other adapters gives
|
|
187
|
+
* (`resolveArrayOps` in `@atscript/db`).
|
|
178
188
|
*
|
|
179
189
|
* The class walks through the incoming payload, detects which of the above
|
|
180
190
|
* operations applies to each top‑level array and builds the corresponding
|
|
@@ -293,6 +303,11 @@ var CollectionPatcher = class {
|
|
|
293
303
|
const childIsObject = typeof childValue === "object" && childValue !== null && !Array.isArray(childValue);
|
|
294
304
|
const propIsJson = propType.metadata?.has("db.json");
|
|
295
305
|
const propIsMerge = propType.metadata?.get("db.patch.strategy") === "merge";
|
|
306
|
+
const childFlatType = this.collection.flatMap.get(childKey);
|
|
307
|
+
if (childIsObject && !propIsJson && childFlatType?.metadata?.get("db.__topLevelArray")) {
|
|
308
|
+
this.parseArrayPatch(childKey, childValue, childFlatType);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
296
311
|
if (childIsObject && propType.type.kind === "object" && !propIsJson) {
|
|
297
312
|
if (propIsMerge) this.flattenPayload(childValue, childKey);
|
|
298
313
|
else this._flattenReplaceObject(childValue, childKey, propType);
|
|
@@ -317,11 +332,14 @@ var CollectionPatcher = class {
|
|
|
317
332
|
const toUpdate = value.$update;
|
|
318
333
|
const keyProps = flatType.type.kind === "array" ? (0, _atscript_db.getKeyProps)(flatType) : /* @__PURE__ */ new Set();
|
|
319
334
|
const keys = keyProps.size > 0 ? [...keyProps] : [];
|
|
335
|
+
if (toReplace !== void 0) {
|
|
336
|
+
this._replace(key, toReplace);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
320
339
|
this._remove(key, toRemove, keys, flatType);
|
|
321
|
-
this._replace(key, toReplace);
|
|
322
|
-
this._insert(key, toInsert, keys, flatType);
|
|
323
|
-
this._upsert(key, toUpsert, keys, flatType);
|
|
324
340
|
this._update(key, toUpdate, keys, flatType);
|
|
341
|
+
this._upsert(key, toUpsert, keys, flatType);
|
|
342
|
+
this._insert(key, toInsert, keys, flatType);
|
|
325
343
|
}
|
|
326
344
|
/**
|
|
327
345
|
* Build an *aggregation‐expression* that checks equality by **all** keys in
|
|
@@ -346,95 +364,111 @@ var CollectionPatcher = class {
|
|
|
346
364
|
* @private
|
|
347
365
|
*/
|
|
348
366
|
_replace(key, input) {
|
|
349
|
-
if (input) this._set(key, input);
|
|
367
|
+
if (input) this._set(key, literal(input));
|
|
350
368
|
}
|
|
351
369
|
/**
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
|
|
370
|
+
* `cond` whether an element of `$$value` (the array being built inside a
|
|
371
|
+
* `$reduce`) has the key(s) of `$$this` (the current item).
|
|
372
|
+
*/
|
|
373
|
+
_keyStored(keys) {
|
|
374
|
+
return { $anyElementTrue: { $map: {
|
|
375
|
+
input: "$$value",
|
|
376
|
+
as: "el",
|
|
377
|
+
in: this._keysEqual(keys, "$$el", "$$this")
|
|
378
|
+
} } };
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Items applied one after another: each is appended to the array built so
|
|
382
|
+
* far unless `stored` holds for it — then `whenStored` (default: the
|
|
383
|
+
* array as it is) is the result.
|
|
384
|
+
*/
|
|
385
|
+
_appendUnless(key, input, stored, whenStored = "$$value") {
|
|
386
|
+
this._set(key, { $reduce: {
|
|
387
|
+
input: literal(input),
|
|
388
|
+
initialValue: { $ifNull: [`$${key}`, []] },
|
|
389
|
+
in: { $cond: [
|
|
390
|
+
stored,
|
|
391
|
+
whenStored,
|
|
392
|
+
{ $concatArrays: ["$$value", ["$$this"]] }
|
|
393
|
+
] }
|
|
394
|
+
} });
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* `$insert` — what the read-modify-write of the other adapters does (since
|
|
398
|
+
* 0.1.180; before, a keyed or unique insert was an upsert / a set union):
|
|
399
|
+
* - keyed → append, skipping an item whose key is already stored
|
|
400
|
+
* - unique → append, skipping an item equal to a stored one
|
|
401
|
+
* - else → append every item
|
|
355
402
|
*/
|
|
356
403
|
_insert(key, input, keys, flatType) {
|
|
357
404
|
if (!input?.length) return;
|
|
358
|
-
if (
|
|
359
|
-
else this.
|
|
405
|
+
if (keys.length > 0) this._appendUnless(key, input, this._keyStored(keys));
|
|
406
|
+
else if (flatType.metadata?.has("expect.array.uniqueItems")) this._appendUnless(key, input, { $in: ["$$this", "$$value"] });
|
|
407
|
+
else this._set(key, { $concatArrays: [{ $ifNull: [`$${key}`, []] }, literal(input)] });
|
|
360
408
|
}
|
|
361
409
|
/**
|
|
362
|
-
* `$upsert`
|
|
363
|
-
*
|
|
364
|
-
*
|
|
410
|
+
* `$upsert` — as on the other adapters (since 0.1.180; before, a keyed
|
|
411
|
+
* upsert moved the element to the end and a key-less one was a set union,
|
|
412
|
+
* which also dropped duplicates already stored):
|
|
413
|
+
* - keyed → replace / merge into the element(s) of that key in place,
|
|
414
|
+
* or append when none has it
|
|
415
|
+
* - key-less → append unless an equal element is stored
|
|
365
416
|
*/
|
|
366
417
|
_upsert(key, input, keys, flatType) {
|
|
367
418
|
if (!input?.length) return;
|
|
368
|
-
if (keys.length
|
|
369
|
-
|
|
370
|
-
const vars = {
|
|
371
|
-
acc: "$$value",
|
|
372
|
-
cand: "$$this"
|
|
373
|
-
};
|
|
374
|
-
let appendExpr = "$$cand";
|
|
375
|
-
if (mergeStrategy) {
|
|
376
|
-
vars.existing = { $arrayElemAt: [{ $filter: {
|
|
377
|
-
input: "$$value",
|
|
378
|
-
as: "el",
|
|
379
|
-
cond: this._keysEqual(keys, "$$el", "$$this")
|
|
380
|
-
} }, 0] };
|
|
381
|
-
appendExpr = { $cond: [
|
|
382
|
-
{ $ifNull: ["$$existing", false] },
|
|
383
|
-
{ $mergeObjects: ["$$existing", "$$cand"] },
|
|
384
|
-
"$$cand"
|
|
385
|
-
] };
|
|
386
|
-
}
|
|
387
|
-
this._set(key, { $reduce: {
|
|
388
|
-
input,
|
|
389
|
-
initialValue: { $ifNull: [`$${key}`, []] },
|
|
390
|
-
in: { $let: {
|
|
391
|
-
vars,
|
|
392
|
-
in: { $concatArrays: [{ $filter: {
|
|
393
|
-
input: "$$acc",
|
|
394
|
-
as: "el",
|
|
395
|
-
cond: { $not: this._keysEqual(keys, "$$el", "$$cand") }
|
|
396
|
-
} }, [appendExpr]] }
|
|
397
|
-
} }
|
|
398
|
-
} });
|
|
419
|
+
if (keys.length === 0) {
|
|
420
|
+
this._appendUnless(key, input, { $in: ["$$this", "$$value"] });
|
|
399
421
|
return;
|
|
400
422
|
}
|
|
401
|
-
|
|
423
|
+
const mergeStrategy = flatType.metadata?.get("db.patch.strategy") === "merge";
|
|
424
|
+
this._appendUnless(key, input, this._keyStored(keys), { $map: {
|
|
425
|
+
input: "$$value",
|
|
426
|
+
as: "el",
|
|
427
|
+
in: { $cond: [
|
|
428
|
+
this._keysEqual(keys, "$$el", "$$this"),
|
|
429
|
+
mergeStrategy ? { $mergeObjects: ["$$el", "$$this"] } : "$$this",
|
|
430
|
+
"$$el"
|
|
431
|
+
] }
|
|
432
|
+
} });
|
|
402
433
|
}
|
|
403
434
|
/**
|
|
404
435
|
* `$update`
|
|
405
436
|
* - keyed → map array and merge / replace matching element(s)
|
|
406
|
-
* - non-keyed →
|
|
437
|
+
* - non-keyed → nothing: there is no key to find an element by (since
|
|
438
|
+
* 0.1.180, as on every other adapter; before, the items were added to
|
|
439
|
+
* the array as a set union)
|
|
407
440
|
*/
|
|
408
441
|
_update(key, input, keys, flatType) {
|
|
409
|
-
if (!input?.length) return;
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
} });
|
|
425
|
-
} else this._set(key, { $setUnion: [{ $ifNull: [`$${key}`, []] }, input] });
|
|
442
|
+
if (!input?.length || keys.length === 0) return;
|
|
443
|
+
const mergeStrategy = flatType.metadata?.get("db.patch.strategy") === "merge";
|
|
444
|
+
this._set(key, { $reduce: {
|
|
445
|
+
input: literal(input),
|
|
446
|
+
initialValue: { $ifNull: [`$${key}`, []] },
|
|
447
|
+
in: { $map: {
|
|
448
|
+
input: "$$value",
|
|
449
|
+
as: "el",
|
|
450
|
+
in: { $cond: [
|
|
451
|
+
this._keysEqual(keys, "$$el", "$$this"),
|
|
452
|
+
mergeStrategy ? { $mergeObjects: ["$$el", "$$this"] } : "$$this",
|
|
453
|
+
"$$el"
|
|
454
|
+
] }
|
|
455
|
+
} }
|
|
456
|
+
} });
|
|
426
457
|
}
|
|
427
458
|
/**
|
|
428
459
|
* `$remove`
|
|
429
|
-
* - keyed
|
|
430
|
-
* -
|
|
460
|
+
* - keyed → filter out any element whose key set matches a payload item
|
|
461
|
+
* - key-less → filter out every element equal to a payload item (since
|
|
462
|
+
* 0.1.180; before, a `$setDifference`, which also dropped
|
|
463
|
+
* duplicates of the elements that stay)
|
|
431
464
|
*/
|
|
432
465
|
_remove(key, input, keys, _flatType) {
|
|
433
466
|
if (!input?.length) return;
|
|
467
|
+
const stored = { $ifNull: [`$${key}`, []] };
|
|
434
468
|
if (keys.length > 0) this._set(key, { $let: {
|
|
435
|
-
vars: { rem: input },
|
|
469
|
+
vars: { rem: literal(input) },
|
|
436
470
|
in: { $filter: {
|
|
437
|
-
input:
|
|
471
|
+
input: stored,
|
|
438
472
|
as: "el",
|
|
439
473
|
cond: { $not: { $anyElementTrue: { $map: {
|
|
440
474
|
input: "$$rem",
|
|
@@ -443,7 +477,11 @@ var CollectionPatcher = class {
|
|
|
443
477
|
} } } }
|
|
444
478
|
} }
|
|
445
479
|
} });
|
|
446
|
-
else this._set(key, { $
|
|
480
|
+
else this._set(key, { $filter: {
|
|
481
|
+
input: stored,
|
|
482
|
+
as: "el",
|
|
483
|
+
cond: { $not: { $in: ["$$el", literal(input)] } }
|
|
484
|
+
} });
|
|
447
485
|
}
|
|
448
486
|
};
|
|
449
487
|
/**
|
|
@@ -2780,6 +2818,16 @@ var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
2780
2818
|
}
|
|
2781
2819
|
} : ops;
|
|
2782
2820
|
const { updateFilter, updateOptions } = new CollectionPatcher(this.getPatcherContext(), patch, effectiveOps).preparePatch();
|
|
2821
|
+
if (updateFilter.length === 0) {
|
|
2822
|
+
this._log("countDocuments (empty patch)", mongoFilter);
|
|
2823
|
+
return {
|
|
2824
|
+
matchedCount: await this.collection.countDocuments(mongoFilter, {
|
|
2825
|
+
limit: 1,
|
|
2826
|
+
...this._getSessionOpts()
|
|
2827
|
+
}),
|
|
2828
|
+
modifiedCount: 0
|
|
2829
|
+
};
|
|
2830
|
+
}
|
|
2783
2831
|
this._log("updateOne (patch)", mongoFilter, updateFilter);
|
|
2784
2832
|
return this._wrapUpdate(() => this.collection.updateOne(mongoFilter, updateFilter, {
|
|
2785
2833
|
...updateOptions,
|
package/dist/index.d.cts
CHANGED
|
@@ -24,13 +24,16 @@ declare function incExpr(field: string, by: number): Document;
|
|
|
24
24
|
* Supported high‑level operations for *top‑level arrays* (see the attached
|
|
25
25
|
* spreadsheet in the chat):
|
|
26
26
|
*
|
|
27
|
-
* | Payload field |
|
|
28
|
-
*
|
|
29
|
-
* | `$replace` |
|
|
30
|
-
* | `$insert` | `$
|
|
31
|
-
* | `$upsert` |
|
|
32
|
-
* | `$update` | `$
|
|
33
|
-
* | `$remove` | `$
|
|
27
|
+
* | Payload field | Pipeline expression | Purpose |
|
|
28
|
+
* |---------------|----------------------------|------------------------------------------------------|
|
|
29
|
+
* | `$replace` | `$literal` | Replace the whole array (stands alone). |
|
|
30
|
+
* | `$insert` | `$concatArrays`, `$reduce` | Append; a stored key / unique value is skipped. |
|
|
31
|
+
* | `$upsert` | `$reduce` + `$map` | Change the element of a *key* in place, or append. |
|
|
32
|
+
* | `$update` | `$reduce` + `$map` | Change the elements matched by *key*. |
|
|
33
|
+
* | `$remove` | `$filter` | Remove by value or by *key*. |
|
|
34
|
+
*
|
|
35
|
+
* The result is the array the read-modify-write of the other adapters gives
|
|
36
|
+
* (`resolveArrayOps` in `@atscript/db`).
|
|
34
37
|
*
|
|
35
38
|
* The class walks through the incoming payload, detects which of the above
|
|
36
39
|
* operations applies to each top‑level array and builds the corresponding
|
|
@@ -126,27 +129,47 @@ declare class CollectionPatcher {
|
|
|
126
129
|
*/
|
|
127
130
|
private _replace;
|
|
128
131
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
|
|
132
|
+
* `cond` whether an element of `$$value` (the array being built inside a
|
|
133
|
+
* `$reduce`) has the key(s) of `$$this` (the current item).
|
|
134
|
+
*/
|
|
135
|
+
private _keyStored;
|
|
136
|
+
/**
|
|
137
|
+
* Items applied one after another: each is appended to the array built so
|
|
138
|
+
* far unless `stored` holds for it — then `whenStored` (default: the
|
|
139
|
+
* array as it is) is the result.
|
|
140
|
+
*/
|
|
141
|
+
private _appendUnless;
|
|
142
|
+
/**
|
|
143
|
+
* `$insert` — what the read-modify-write of the other adapters does (since
|
|
144
|
+
* 0.1.180; before, a keyed or unique insert was an upsert / a set union):
|
|
145
|
+
* - keyed → append, skipping an item whose key is already stored
|
|
146
|
+
* - unique → append, skipping an item equal to a stored one
|
|
147
|
+
* - else → append every item
|
|
132
148
|
*/
|
|
133
149
|
private _insert;
|
|
134
150
|
/**
|
|
135
|
-
* `$upsert`
|
|
136
|
-
*
|
|
137
|
-
*
|
|
151
|
+
* `$upsert` — as on the other adapters (since 0.1.180; before, a keyed
|
|
152
|
+
* upsert moved the element to the end and a key-less one was a set union,
|
|
153
|
+
* which also dropped duplicates already stored):
|
|
154
|
+
* - keyed → replace / merge into the element(s) of that key in place,
|
|
155
|
+
* or append when none has it
|
|
156
|
+
* - key-less → append unless an equal element is stored
|
|
138
157
|
*/
|
|
139
158
|
private _upsert;
|
|
140
159
|
/**
|
|
141
160
|
* `$update`
|
|
142
161
|
* - keyed → map array and merge / replace matching element(s)
|
|
143
|
-
* - non-keyed →
|
|
162
|
+
* - non-keyed → nothing: there is no key to find an element by (since
|
|
163
|
+
* 0.1.180, as on every other adapter; before, the items were added to
|
|
164
|
+
* the array as a set union)
|
|
144
165
|
*/
|
|
145
166
|
private _update;
|
|
146
167
|
/**
|
|
147
168
|
* `$remove`
|
|
148
|
-
* - keyed
|
|
149
|
-
* -
|
|
169
|
+
* - keyed → filter out any element whose key set matches a payload item
|
|
170
|
+
* - key-less → filter out every element equal to a payload item (since
|
|
171
|
+
* 0.1.180; before, a `$setDifference`, which also dropped
|
|
172
|
+
* duplicates of the elements that stay)
|
|
150
173
|
*/
|
|
151
174
|
private _remove;
|
|
152
175
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -24,13 +24,16 @@ declare function incExpr(field: string, by: number): Document;
|
|
|
24
24
|
* Supported high‑level operations for *top‑level arrays* (see the attached
|
|
25
25
|
* spreadsheet in the chat):
|
|
26
26
|
*
|
|
27
|
-
* | Payload field |
|
|
28
|
-
*
|
|
29
|
-
* | `$replace` |
|
|
30
|
-
* | `$insert` | `$
|
|
31
|
-
* | `$upsert` |
|
|
32
|
-
* | `$update` | `$
|
|
33
|
-
* | `$remove` | `$
|
|
27
|
+
* | Payload field | Pipeline expression | Purpose |
|
|
28
|
+
* |---------------|----------------------------|------------------------------------------------------|
|
|
29
|
+
* | `$replace` | `$literal` | Replace the whole array (stands alone). |
|
|
30
|
+
* | `$insert` | `$concatArrays`, `$reduce` | Append; a stored key / unique value is skipped. |
|
|
31
|
+
* | `$upsert` | `$reduce` + `$map` | Change the element of a *key* in place, or append. |
|
|
32
|
+
* | `$update` | `$reduce` + `$map` | Change the elements matched by *key*. |
|
|
33
|
+
* | `$remove` | `$filter` | Remove by value or by *key*. |
|
|
34
|
+
*
|
|
35
|
+
* The result is the array the read-modify-write of the other adapters gives
|
|
36
|
+
* (`resolveArrayOps` in `@atscript/db`).
|
|
34
37
|
*
|
|
35
38
|
* The class walks through the incoming payload, detects which of the above
|
|
36
39
|
* operations applies to each top‑level array and builds the corresponding
|
|
@@ -126,27 +129,47 @@ declare class CollectionPatcher {
|
|
|
126
129
|
*/
|
|
127
130
|
private _replace;
|
|
128
131
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
|
|
132
|
+
* `cond` whether an element of `$$value` (the array being built inside a
|
|
133
|
+
* `$reduce`) has the key(s) of `$$this` (the current item).
|
|
134
|
+
*/
|
|
135
|
+
private _keyStored;
|
|
136
|
+
/**
|
|
137
|
+
* Items applied one after another: each is appended to the array built so
|
|
138
|
+
* far unless `stored` holds for it — then `whenStored` (default: the
|
|
139
|
+
* array as it is) is the result.
|
|
140
|
+
*/
|
|
141
|
+
private _appendUnless;
|
|
142
|
+
/**
|
|
143
|
+
* `$insert` — what the read-modify-write of the other adapters does (since
|
|
144
|
+
* 0.1.180; before, a keyed or unique insert was an upsert / a set union):
|
|
145
|
+
* - keyed → append, skipping an item whose key is already stored
|
|
146
|
+
* - unique → append, skipping an item equal to a stored one
|
|
147
|
+
* - else → append every item
|
|
132
148
|
*/
|
|
133
149
|
private _insert;
|
|
134
150
|
/**
|
|
135
|
-
* `$upsert`
|
|
136
|
-
*
|
|
137
|
-
*
|
|
151
|
+
* `$upsert` — as on the other adapters (since 0.1.180; before, a keyed
|
|
152
|
+
* upsert moved the element to the end and a key-less one was a set union,
|
|
153
|
+
* which also dropped duplicates already stored):
|
|
154
|
+
* - keyed → replace / merge into the element(s) of that key in place,
|
|
155
|
+
* or append when none has it
|
|
156
|
+
* - key-less → append unless an equal element is stored
|
|
138
157
|
*/
|
|
139
158
|
private _upsert;
|
|
140
159
|
/**
|
|
141
160
|
* `$update`
|
|
142
161
|
* - keyed → map array and merge / replace matching element(s)
|
|
143
|
-
* - non-keyed →
|
|
162
|
+
* - non-keyed → nothing: there is no key to find an element by (since
|
|
163
|
+
* 0.1.180, as on every other adapter; before, the items were added to
|
|
164
|
+
* the array as a set union)
|
|
144
165
|
*/
|
|
145
166
|
private _update;
|
|
146
167
|
/**
|
|
147
168
|
* `$remove`
|
|
148
|
-
* - keyed
|
|
149
|
-
* -
|
|
169
|
+
* - keyed → filter out any element whose key set matches a payload item
|
|
170
|
+
* - key-less → filter out every element equal to a payload item (since
|
|
171
|
+
* 0.1.180; before, a `$setDifference`, which also dropped
|
|
172
|
+
* duplicates of the elements that stay)
|
|
150
173
|
*/
|
|
151
174
|
private _remove;
|
|
152
175
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -133,6 +133,13 @@ function duplicateKeyViolation(error) {
|
|
|
133
133
|
//#endregion
|
|
134
134
|
//#region src/lib/collection-patcher.ts
|
|
135
135
|
/**
|
|
136
|
+
* `items` of an array operator as an aggregation operand. Always a
|
|
137
|
+
* `$literal`: the items are data, and an aggregation expression would read a
|
|
138
|
+
* `$`-prefixed string in them as a field path — storing another field's
|
|
139
|
+
* value, a secret one included, where the string was sent (since 0.1.180).
|
|
140
|
+
*/
|
|
141
|
+
const literal = (items) => ({ $literal: items });
|
|
142
|
+
/**
|
|
136
143
|
* True when a user value contains a `$`-prefixed string or object key — the
|
|
137
144
|
* shapes aggregation `$set` would evaluate as field paths / operators instead
|
|
138
145
|
* of treating as literal data. Caller must wrap such values in `{ $literal }`.
|
|
@@ -167,13 +174,16 @@ function incExpr(field, by) {
|
|
|
167
174
|
* Supported high‑level operations for *top‑level arrays* (see the attached
|
|
168
175
|
* spreadsheet in the chat):
|
|
169
176
|
*
|
|
170
|
-
* | Payload field |
|
|
171
|
-
*
|
|
172
|
-
* | `$replace` |
|
|
173
|
-
* | `$insert` | `$
|
|
174
|
-
* | `$upsert` |
|
|
175
|
-
* | `$update` | `$
|
|
176
|
-
* | `$remove` | `$
|
|
177
|
+
* | Payload field | Pipeline expression | Purpose |
|
|
178
|
+
* |---------------|----------------------------|------------------------------------------------------|
|
|
179
|
+
* | `$replace` | `$literal` | Replace the whole array (stands alone). |
|
|
180
|
+
* | `$insert` | `$concatArrays`, `$reduce` | Append; a stored key / unique value is skipped. |
|
|
181
|
+
* | `$upsert` | `$reduce` + `$map` | Change the element of a *key* in place, or append. |
|
|
182
|
+
* | `$update` | `$reduce` + `$map` | Change the elements matched by *key*. |
|
|
183
|
+
* | `$remove` | `$filter` | Remove by value or by *key*. |
|
|
184
|
+
*
|
|
185
|
+
* The result is the array the read-modify-write of the other adapters gives
|
|
186
|
+
* (`resolveArrayOps` in `@atscript/db`).
|
|
177
187
|
*
|
|
178
188
|
* The class walks through the incoming payload, detects which of the above
|
|
179
189
|
* operations applies to each top‑level array and builds the corresponding
|
|
@@ -292,6 +302,11 @@ var CollectionPatcher = class {
|
|
|
292
302
|
const childIsObject = typeof childValue === "object" && childValue !== null && !Array.isArray(childValue);
|
|
293
303
|
const propIsJson = propType.metadata?.has("db.json");
|
|
294
304
|
const propIsMerge = propType.metadata?.get("db.patch.strategy") === "merge";
|
|
305
|
+
const childFlatType = this.collection.flatMap.get(childKey);
|
|
306
|
+
if (childIsObject && !propIsJson && childFlatType?.metadata?.get("db.__topLevelArray")) {
|
|
307
|
+
this.parseArrayPatch(childKey, childValue, childFlatType);
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
295
310
|
if (childIsObject && propType.type.kind === "object" && !propIsJson) {
|
|
296
311
|
if (propIsMerge) this.flattenPayload(childValue, childKey);
|
|
297
312
|
else this._flattenReplaceObject(childValue, childKey, propType);
|
|
@@ -316,11 +331,14 @@ var CollectionPatcher = class {
|
|
|
316
331
|
const toUpdate = value.$update;
|
|
317
332
|
const keyProps = flatType.type.kind === "array" ? getKeyProps(flatType) : /* @__PURE__ */ new Set();
|
|
318
333
|
const keys = keyProps.size > 0 ? [...keyProps] : [];
|
|
334
|
+
if (toReplace !== void 0) {
|
|
335
|
+
this._replace(key, toReplace);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
319
338
|
this._remove(key, toRemove, keys, flatType);
|
|
320
|
-
this._replace(key, toReplace);
|
|
321
|
-
this._insert(key, toInsert, keys, flatType);
|
|
322
|
-
this._upsert(key, toUpsert, keys, flatType);
|
|
323
339
|
this._update(key, toUpdate, keys, flatType);
|
|
340
|
+
this._upsert(key, toUpsert, keys, flatType);
|
|
341
|
+
this._insert(key, toInsert, keys, flatType);
|
|
324
342
|
}
|
|
325
343
|
/**
|
|
326
344
|
* Build an *aggregation‐expression* that checks equality by **all** keys in
|
|
@@ -345,95 +363,111 @@ var CollectionPatcher = class {
|
|
|
345
363
|
* @private
|
|
346
364
|
*/
|
|
347
365
|
_replace(key, input) {
|
|
348
|
-
if (input) this._set(key, input);
|
|
366
|
+
if (input) this._set(key, literal(input));
|
|
349
367
|
}
|
|
350
368
|
/**
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
|
|
369
|
+
* `cond` whether an element of `$$value` (the array being built inside a
|
|
370
|
+
* `$reduce`) has the key(s) of `$$this` (the current item).
|
|
371
|
+
*/
|
|
372
|
+
_keyStored(keys) {
|
|
373
|
+
return { $anyElementTrue: { $map: {
|
|
374
|
+
input: "$$value",
|
|
375
|
+
as: "el",
|
|
376
|
+
in: this._keysEqual(keys, "$$el", "$$this")
|
|
377
|
+
} } };
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Items applied one after another: each is appended to the array built so
|
|
381
|
+
* far unless `stored` holds for it — then `whenStored` (default: the
|
|
382
|
+
* array as it is) is the result.
|
|
383
|
+
*/
|
|
384
|
+
_appendUnless(key, input, stored, whenStored = "$$value") {
|
|
385
|
+
this._set(key, { $reduce: {
|
|
386
|
+
input: literal(input),
|
|
387
|
+
initialValue: { $ifNull: [`$${key}`, []] },
|
|
388
|
+
in: { $cond: [
|
|
389
|
+
stored,
|
|
390
|
+
whenStored,
|
|
391
|
+
{ $concatArrays: ["$$value", ["$$this"]] }
|
|
392
|
+
] }
|
|
393
|
+
} });
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* `$insert` — what the read-modify-write of the other adapters does (since
|
|
397
|
+
* 0.1.180; before, a keyed or unique insert was an upsert / a set union):
|
|
398
|
+
* - keyed → append, skipping an item whose key is already stored
|
|
399
|
+
* - unique → append, skipping an item equal to a stored one
|
|
400
|
+
* - else → append every item
|
|
354
401
|
*/
|
|
355
402
|
_insert(key, input, keys, flatType) {
|
|
356
403
|
if (!input?.length) return;
|
|
357
|
-
if (
|
|
358
|
-
else this.
|
|
404
|
+
if (keys.length > 0) this._appendUnless(key, input, this._keyStored(keys));
|
|
405
|
+
else if (flatType.metadata?.has("expect.array.uniqueItems")) this._appendUnless(key, input, { $in: ["$$this", "$$value"] });
|
|
406
|
+
else this._set(key, { $concatArrays: [{ $ifNull: [`$${key}`, []] }, literal(input)] });
|
|
359
407
|
}
|
|
360
408
|
/**
|
|
361
|
-
* `$upsert`
|
|
362
|
-
*
|
|
363
|
-
*
|
|
409
|
+
* `$upsert` — as on the other adapters (since 0.1.180; before, a keyed
|
|
410
|
+
* upsert moved the element to the end and a key-less one was a set union,
|
|
411
|
+
* which also dropped duplicates already stored):
|
|
412
|
+
* - keyed → replace / merge into the element(s) of that key in place,
|
|
413
|
+
* or append when none has it
|
|
414
|
+
* - key-less → append unless an equal element is stored
|
|
364
415
|
*/
|
|
365
416
|
_upsert(key, input, keys, flatType) {
|
|
366
417
|
if (!input?.length) return;
|
|
367
|
-
if (keys.length
|
|
368
|
-
|
|
369
|
-
const vars = {
|
|
370
|
-
acc: "$$value",
|
|
371
|
-
cand: "$$this"
|
|
372
|
-
};
|
|
373
|
-
let appendExpr = "$$cand";
|
|
374
|
-
if (mergeStrategy) {
|
|
375
|
-
vars.existing = { $arrayElemAt: [{ $filter: {
|
|
376
|
-
input: "$$value",
|
|
377
|
-
as: "el",
|
|
378
|
-
cond: this._keysEqual(keys, "$$el", "$$this")
|
|
379
|
-
} }, 0] };
|
|
380
|
-
appendExpr = { $cond: [
|
|
381
|
-
{ $ifNull: ["$$existing", false] },
|
|
382
|
-
{ $mergeObjects: ["$$existing", "$$cand"] },
|
|
383
|
-
"$$cand"
|
|
384
|
-
] };
|
|
385
|
-
}
|
|
386
|
-
this._set(key, { $reduce: {
|
|
387
|
-
input,
|
|
388
|
-
initialValue: { $ifNull: [`$${key}`, []] },
|
|
389
|
-
in: { $let: {
|
|
390
|
-
vars,
|
|
391
|
-
in: { $concatArrays: [{ $filter: {
|
|
392
|
-
input: "$$acc",
|
|
393
|
-
as: "el",
|
|
394
|
-
cond: { $not: this._keysEqual(keys, "$$el", "$$cand") }
|
|
395
|
-
} }, [appendExpr]] }
|
|
396
|
-
} }
|
|
397
|
-
} });
|
|
418
|
+
if (keys.length === 0) {
|
|
419
|
+
this._appendUnless(key, input, { $in: ["$$this", "$$value"] });
|
|
398
420
|
return;
|
|
399
421
|
}
|
|
400
|
-
|
|
422
|
+
const mergeStrategy = flatType.metadata?.get("db.patch.strategy") === "merge";
|
|
423
|
+
this._appendUnless(key, input, this._keyStored(keys), { $map: {
|
|
424
|
+
input: "$$value",
|
|
425
|
+
as: "el",
|
|
426
|
+
in: { $cond: [
|
|
427
|
+
this._keysEqual(keys, "$$el", "$$this"),
|
|
428
|
+
mergeStrategy ? { $mergeObjects: ["$$el", "$$this"] } : "$$this",
|
|
429
|
+
"$$el"
|
|
430
|
+
] }
|
|
431
|
+
} });
|
|
401
432
|
}
|
|
402
433
|
/**
|
|
403
434
|
* `$update`
|
|
404
435
|
* - keyed → map array and merge / replace matching element(s)
|
|
405
|
-
* - non-keyed →
|
|
436
|
+
* - non-keyed → nothing: there is no key to find an element by (since
|
|
437
|
+
* 0.1.180, as on every other adapter; before, the items were added to
|
|
438
|
+
* the array as a set union)
|
|
406
439
|
*/
|
|
407
440
|
_update(key, input, keys, flatType) {
|
|
408
|
-
if (!input?.length) return;
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
} });
|
|
424
|
-
} else this._set(key, { $setUnion: [{ $ifNull: [`$${key}`, []] }, input] });
|
|
441
|
+
if (!input?.length || keys.length === 0) return;
|
|
442
|
+
const mergeStrategy = flatType.metadata?.get("db.patch.strategy") === "merge";
|
|
443
|
+
this._set(key, { $reduce: {
|
|
444
|
+
input: literal(input),
|
|
445
|
+
initialValue: { $ifNull: [`$${key}`, []] },
|
|
446
|
+
in: { $map: {
|
|
447
|
+
input: "$$value",
|
|
448
|
+
as: "el",
|
|
449
|
+
in: { $cond: [
|
|
450
|
+
this._keysEqual(keys, "$$el", "$$this"),
|
|
451
|
+
mergeStrategy ? { $mergeObjects: ["$$el", "$$this"] } : "$$this",
|
|
452
|
+
"$$el"
|
|
453
|
+
] }
|
|
454
|
+
} }
|
|
455
|
+
} });
|
|
425
456
|
}
|
|
426
457
|
/**
|
|
427
458
|
* `$remove`
|
|
428
|
-
* - keyed
|
|
429
|
-
* -
|
|
459
|
+
* - keyed → filter out any element whose key set matches a payload item
|
|
460
|
+
* - key-less → filter out every element equal to a payload item (since
|
|
461
|
+
* 0.1.180; before, a `$setDifference`, which also dropped
|
|
462
|
+
* duplicates of the elements that stay)
|
|
430
463
|
*/
|
|
431
464
|
_remove(key, input, keys, _flatType) {
|
|
432
465
|
if (!input?.length) return;
|
|
466
|
+
const stored = { $ifNull: [`$${key}`, []] };
|
|
433
467
|
if (keys.length > 0) this._set(key, { $let: {
|
|
434
|
-
vars: { rem: input },
|
|
468
|
+
vars: { rem: literal(input) },
|
|
435
469
|
in: { $filter: {
|
|
436
|
-
input:
|
|
470
|
+
input: stored,
|
|
437
471
|
as: "el",
|
|
438
472
|
cond: { $not: { $anyElementTrue: { $map: {
|
|
439
473
|
input: "$$rem",
|
|
@@ -442,7 +476,11 @@ var CollectionPatcher = class {
|
|
|
442
476
|
} } } }
|
|
443
477
|
} }
|
|
444
478
|
} });
|
|
445
|
-
else this._set(key, { $
|
|
479
|
+
else this._set(key, { $filter: {
|
|
480
|
+
input: stored,
|
|
481
|
+
as: "el",
|
|
482
|
+
cond: { $not: { $in: ["$$el", literal(input)] } }
|
|
483
|
+
} });
|
|
446
484
|
}
|
|
447
485
|
};
|
|
448
486
|
/**
|
|
@@ -2779,6 +2817,16 @@ var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
|
|
|
2779
2817
|
}
|
|
2780
2818
|
} : ops;
|
|
2781
2819
|
const { updateFilter, updateOptions } = new CollectionPatcher(this.getPatcherContext(), patch, effectiveOps).preparePatch();
|
|
2820
|
+
if (updateFilter.length === 0) {
|
|
2821
|
+
this._log("countDocuments (empty patch)", mongoFilter);
|
|
2822
|
+
return {
|
|
2823
|
+
matchedCount: await this.collection.countDocuments(mongoFilter, {
|
|
2824
|
+
limit: 1,
|
|
2825
|
+
...this._getSessionOpts()
|
|
2826
|
+
}),
|
|
2827
|
+
modifiedCount: 0
|
|
2828
|
+
};
|
|
2829
|
+
}
|
|
2782
2830
|
this._log("updateOne (patch)", mongoFilter, updateFilter);
|
|
2783
2831
|
return this._wrapUpdate(() => this.collection.updateOne(mongoFilter, updateFilter, {
|
|
2784
2832
|
...updateOptions,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-mongo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.181",
|
|
4
4
|
"description": "Mongodb plugin for atscript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"@atscript/typescript": "^0.2.12",
|
|
59
59
|
"@uniqu/core": "^0.2.2",
|
|
60
60
|
"mongodb": "^6.17.0",
|
|
61
|
-
"@atscript/db": "^0.1.
|
|
61
|
+
"@atscript/db": "^0.1.181"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "vp pack",
|