@prisma/orm-target-mongo 8.0.0-rc.24-dev.14 → 8.0.0-rc.24-dev.16
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/{control-BlMckWwK.mjs → control-ADZk3Q1S.mjs} +3059 -307
- package/dist/control-ADZk3Q1S.mjs.map +1 -0
- package/dist/{control-DjXy3TCE.mjs → control-C9gGCopP.mjs} +2 -2
- package/dist/{control-DjXy3TCE.mjs.map → control-C9gGCopP.mjs.map} +1 -1
- package/dist/{control-OiC131Dv.d.mts → control-Dc2P_CN-.d.mts} +15 -6
- package/dist/control-Dc2P_CN-.d.mts.map +1 -0
- package/dist/driver.mjs +1 -1
- package/dist/driver__control.mjs +1 -1
- package/dist/driver__runtime.mjs +1 -1
- package/dist/driver__runtime.mjs.map +1 -1
- package/dist/{migration-DASk9QNA.d.mts → migration-D78n7EUy.d.mts} +1 -1
- package/dist/{migration-factories-C5ktmt8c-BP8k9SnZ.mjs → migration-factories-RJZq8NhE-CigCh286.mjs} +621 -13
- package/dist/migration-factories-RJZq8NhE-CigCh286.mjs.map +1 -0
- package/dist/{mongo-driver-B9PP6J-f-401ZvDKV.mjs → mongo-driver-BV3a7kcA-BJrV90Qw.mjs} +3 -3
- package/dist/{mongo-driver-B9PP6J-f-401ZvDKV.mjs.map → mongo-driver-BV3a7kcA-BJrV90Qw.mjs.map} +1 -1
- package/dist/{op-factory-call-BCgwbVJe-DhcJRxAb.d.mts → op-factory-call-vacb7XmE-sHhC1Hit.d.mts} +151 -5
- package/dist/op-factory-call-vacb7XmE-sHhC1Hit.d.mts.map +1 -0
- package/dist/target.d.mts +4 -4
- package/dist/target.mjs +3 -3
- package/dist/target__control.d.mts +2 -2
- package/dist/target__control.mjs +1 -1
- package/dist/target__migration.d.mts +3 -3
- package/dist/target__migration.mjs +2 -2
- package/package.json +10 -10
- package/dist/control-BlMckWwK.mjs.map +0 -1
- package/dist/control-OiC131Dv.d.mts.map +0 -1
- package/dist/migration-factories-C5ktmt8c-BP8k9SnZ.mjs.map +0 -1
- package/dist/op-factory-call-BCgwbVJe-DhcJRxAb.d.mts.map +0 -1
package/dist/{migration-factories-C5ktmt8c-BP8k9SnZ.mjs → migration-factories-RJZq8NhE-CigCh286.mjs}
RENAMED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { t as mongoTargetError } from "./mongo-target-errors-Btf3ML1w-QOtEG7EX.mjs";
|
|
2
2
|
import { ifDefined } from "@prisma/orm-framework/utils/defined";
|
|
3
3
|
import { blindCast } from "@prisma/orm-framework/utils/casts";
|
|
4
|
+
import { notOk, ok } from "@prisma/orm-framework/utils/result";
|
|
4
5
|
import { MONGO_INDEX_OPTION_KEYS, defaultMongoIndexName } from "@prisma/orm-family-mongo/contract";
|
|
5
6
|
import { CollModCommand, DropCollectionCommand, DropIndexCommand, ListCollectionsCommand, ListIndexesCommand, MongoAndExpr, MongoExistsExpr, MongoFieldFilter, MongoNotExpr, RenameCollectionCommand, buildIndexOpId, keysToKeySpec } from "@prisma/orm-family-mongo/query-ast/control";
|
|
6
|
-
import { RawAggregateCommand, RawUpdateManyCommand } from "@prisma/orm-family-mongo/query-ast/execution";
|
|
7
|
+
import { RawAggregateCommand, RawDeleteManyCommand, RawUpdateManyCommand } from "@prisma/orm-family-mongo/query-ast/execution";
|
|
7
8
|
import { createFieldAccessor } from "@prisma/orm-family-mongo/query-builder";
|
|
8
9
|
import { collection } from "@prisma/orm-family-mongo/query-builder/contract-free";
|
|
9
|
-
//#region ../../../3-targets/mongo/1-target/dist/migration-factories-
|
|
10
|
+
//#region ../../../3-targets/mongo/1-target/dist/migration-factories-RJZq8NhE.mjs
|
|
10
11
|
const NUMBERS = [
|
|
11
12
|
"double",
|
|
12
13
|
"int",
|
|
@@ -63,6 +64,257 @@ function castsFrom(from, to) {
|
|
|
63
64
|
function convertKeepsEveryValue(from, to) {
|
|
64
65
|
return from === to || (KEEPS_EVERY_VALUE[from]?.has(to) ?? false);
|
|
65
66
|
}
|
|
67
|
+
const BLANK = /* @__PURE__ */ new Set([
|
|
68
|
+
" ",
|
|
69
|
+
" ",
|
|
70
|
+
"\n",
|
|
71
|
+
"\r"
|
|
72
|
+
]);
|
|
73
|
+
const ESCAPES = {
|
|
74
|
+
b: "\b",
|
|
75
|
+
f: "\f",
|
|
76
|
+
n: "\n",
|
|
77
|
+
r: "\r",
|
|
78
|
+
t: " ",
|
|
79
|
+
"/": "/",
|
|
80
|
+
"\\": "\\"
|
|
81
|
+
};
|
|
82
|
+
function isNameFirst(char) {
|
|
83
|
+
return /^[A-Za-z_]$/.test(char) || (char.codePointAt(0) ?? 0) >= 128;
|
|
84
|
+
}
|
|
85
|
+
function isNameChar(char) {
|
|
86
|
+
return isNameFirst(char) || /^[0-9]$/.test(char);
|
|
87
|
+
}
|
|
88
|
+
var Reader = class {
|
|
89
|
+
text;
|
|
90
|
+
position = 0;
|
|
91
|
+
constructor(text) {
|
|
92
|
+
this.text = text;
|
|
93
|
+
}
|
|
94
|
+
get done() {
|
|
95
|
+
return this.position >= this.text.length;
|
|
96
|
+
}
|
|
97
|
+
peek(offset = 0) {
|
|
98
|
+
return this.text[this.position + offset] ?? "";
|
|
99
|
+
}
|
|
100
|
+
next() {
|
|
101
|
+
const char = this.peek();
|
|
102
|
+
this.position += 1;
|
|
103
|
+
return char;
|
|
104
|
+
}
|
|
105
|
+
skipBlank() {
|
|
106
|
+
while (BLANK.has(this.peek())) this.position += 1;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
function readShorthandName(reader) {
|
|
110
|
+
if (!isNameFirst(reader.peek())) return void 0;
|
|
111
|
+
let name = "";
|
|
112
|
+
while (!reader.done && isNameChar(reader.peek())) name += reader.next();
|
|
113
|
+
return name;
|
|
114
|
+
}
|
|
115
|
+
function readStringLiteral(reader) {
|
|
116
|
+
const quote = reader.next();
|
|
117
|
+
let value = "";
|
|
118
|
+
while (!reader.done) {
|
|
119
|
+
const char = reader.next();
|
|
120
|
+
if (char === quote) return value;
|
|
121
|
+
if (char !== "\\") {
|
|
122
|
+
if ((char.codePointAt(0) ?? 0) < 32) return void 0;
|
|
123
|
+
value += char;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const escaped = reader.next();
|
|
127
|
+
if (escaped === quote) value += quote;
|
|
128
|
+
else if (escaped === "u") {
|
|
129
|
+
const hex = reader.text.slice(reader.position, reader.position + 4);
|
|
130
|
+
if (!/^[0-9A-Fa-f]{4}$/.test(hex)) return void 0;
|
|
131
|
+
reader.position += 4;
|
|
132
|
+
value += String.fromCharCode(Number.parseInt(hex, 16));
|
|
133
|
+
} else {
|
|
134
|
+
const replacement = ESCAPES[escaped];
|
|
135
|
+
if (replacement === void 0) return void 0;
|
|
136
|
+
value += replacement;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function readBracket(reader) {
|
|
141
|
+
reader.next();
|
|
142
|
+
reader.skipBlank();
|
|
143
|
+
let step;
|
|
144
|
+
const first = reader.peek();
|
|
145
|
+
if (first === "*") {
|
|
146
|
+
reader.next();
|
|
147
|
+
step = { kind: "wildcard" };
|
|
148
|
+
} else if (first === "'" || first === "\"") {
|
|
149
|
+
const name = readStringLiteral(reader);
|
|
150
|
+
step = name === void 0 ? void 0 : {
|
|
151
|
+
kind: "member",
|
|
152
|
+
name
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
reader.skipBlank();
|
|
156
|
+
return step !== void 0 && reader.next() === "]" ? step : void 0;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Parses an RFC 9535 JSONPath that uses only member name selectors (`.street`, `['street']`) and
|
|
160
|
+
* the wildcard selector (`[*]`, `.*`), the grammar SQL's stored-key operations take. Any other
|
|
161
|
+
* JSONPath feature is refused with the reason.
|
|
162
|
+
*/
|
|
163
|
+
function parseJsonPath(text) {
|
|
164
|
+
const unsupported = notOk(`path ${JSON.stringify(text)} is not a JSONPath of member names (.name or ['name']) and wildcards ([*] or .*) from $`);
|
|
165
|
+
const reader = new Reader(text);
|
|
166
|
+
if (reader.next() !== "$") return unsupported;
|
|
167
|
+
const steps = [];
|
|
168
|
+
while (!reader.done) {
|
|
169
|
+
reader.skipBlank();
|
|
170
|
+
const char = reader.peek();
|
|
171
|
+
let step;
|
|
172
|
+
if (char === "." && reader.peek(1) === "*") {
|
|
173
|
+
reader.position += 2;
|
|
174
|
+
step = { kind: "wildcard" };
|
|
175
|
+
} else if (char === ".") {
|
|
176
|
+
reader.next();
|
|
177
|
+
const name = readShorthandName(reader);
|
|
178
|
+
step = name === void 0 ? void 0 : {
|
|
179
|
+
kind: "member",
|
|
180
|
+
name
|
|
181
|
+
};
|
|
182
|
+
} else if (char === "[") step = readBracket(reader);
|
|
183
|
+
if (step === void 0) return unsupported;
|
|
184
|
+
steps.push(step);
|
|
185
|
+
}
|
|
186
|
+
return ok({
|
|
187
|
+
text,
|
|
188
|
+
steps
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/** The steps as a JSONPath: a member name in shorthand when it is an identifier, else in brackets. */
|
|
192
|
+
function jsonPathText(steps) {
|
|
193
|
+
return `$${steps.map((step) => {
|
|
194
|
+
if (step.kind === "wildcard") return "[*]";
|
|
195
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(step.name) ? `.${step.name}` : `[${JSON.stringify(step.name)}]`;
|
|
196
|
+
}).join("")}`;
|
|
197
|
+
}
|
|
198
|
+
/** The longest start of `path`'s text that is a path of `steps` selectors, without trailing blanks. */
|
|
199
|
+
function writtenPrefix(path, steps) {
|
|
200
|
+
for (let end = path.text.length - 1; end > 0; end -= 1) {
|
|
201
|
+
const prefix = path.text.slice(0, end).trimEnd();
|
|
202
|
+
const parsed = parseJsonPath(prefix);
|
|
203
|
+
if (parsed.ok && parsed.value.steps.length === steps) return prefix;
|
|
204
|
+
}
|
|
205
|
+
return jsonPathText(path.steps.slice(0, steps));
|
|
206
|
+
}
|
|
207
|
+
/** The key path of `path` inside root field `field`, when the path ends at a member name. */
|
|
208
|
+
function storedKeyPath(field, path) {
|
|
209
|
+
const parsed = parseJsonPath(path);
|
|
210
|
+
if (!parsed.ok) return parsed;
|
|
211
|
+
const last = parsed.value.steps.at(-1);
|
|
212
|
+
if (last?.kind !== "member") return notOk(`path ${JSON.stringify(path)} does not end at a key`);
|
|
213
|
+
return ok({
|
|
214
|
+
field,
|
|
215
|
+
path: parsed.value,
|
|
216
|
+
objects: parsed.value.steps.slice(0, -1),
|
|
217
|
+
objectsText: writtenPrefix(parsed.value, parsed.value.steps.length - 1),
|
|
218
|
+
key: last.name
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/** The names of the keys a key path steps through and ends at. */
|
|
222
|
+
function keysOf(keyPath) {
|
|
223
|
+
return keyPath.path.steps.flatMap((step) => step.kind === "member" ? [step.name] : []);
|
|
224
|
+
}
|
|
225
|
+
/** A key's value in an object, read with `$getField` so the key is never read as a path or an operator. */
|
|
226
|
+
function keyOf(object, key) {
|
|
227
|
+
return { $getField: {
|
|
228
|
+
field: { $literal: key },
|
|
229
|
+
input: object
|
|
230
|
+
} };
|
|
231
|
+
}
|
|
232
|
+
/** Whether the object holds the key, `null` included. */
|
|
233
|
+
function holdsKey(object, key) {
|
|
234
|
+
return { $ne: [{ $type: keyOf(object, key) }, "missing"] };
|
|
235
|
+
}
|
|
236
|
+
/** Whether the key is missing from the object or holds `null`. */
|
|
237
|
+
function lacksKey(object, key) {
|
|
238
|
+
return { $in: [{ $type: keyOf(object, key) }, ["missing", "null"]] };
|
|
239
|
+
}
|
|
240
|
+
function isOfType(value, type) {
|
|
241
|
+
return { $eq: [{ $type: value }, type] };
|
|
242
|
+
}
|
|
243
|
+
/** Whether a value has the type the next step reads: a list for a wildcard, an object for a key. */
|
|
244
|
+
function walkable(value, steps) {
|
|
245
|
+
return isOfType(value, steps[0]?.kind === "wildcard" ? "array" : "object");
|
|
246
|
+
}
|
|
247
|
+
function elementVariable(depth) {
|
|
248
|
+
return `element${depth}`;
|
|
249
|
+
}
|
|
250
|
+
/** The value of `value`, known to be walkable by `steps`, with each object at the end of `steps` rewritten. */
|
|
251
|
+
function rewritten(value, steps, rewrite, depth) {
|
|
252
|
+
const [step, ...rest] = steps;
|
|
253
|
+
if (step === void 0) return rewrite(value);
|
|
254
|
+
const guarded = (child, childDepth) => ({ $cond: [
|
|
255
|
+
walkable(child, rest),
|
|
256
|
+
rewritten(child, rest, rewrite, childDepth),
|
|
257
|
+
child
|
|
258
|
+
] });
|
|
259
|
+
if (step.kind === "wildcard") {
|
|
260
|
+
const as = elementVariable(depth);
|
|
261
|
+
return { $map: {
|
|
262
|
+
input: value,
|
|
263
|
+
as,
|
|
264
|
+
in: guarded(`$$${as}`, depth + 1)
|
|
265
|
+
} };
|
|
266
|
+
}
|
|
267
|
+
const child = keyOf(value, step.name);
|
|
268
|
+
return { $cond: [
|
|
269
|
+
walkable(child, rest),
|
|
270
|
+
{ $setField: {
|
|
271
|
+
field: { $literal: step.name },
|
|
272
|
+
input: value,
|
|
273
|
+
value: rewritten(child, rest, rewrite, depth)
|
|
274
|
+
} },
|
|
275
|
+
value
|
|
276
|
+
] };
|
|
277
|
+
}
|
|
278
|
+
/** Whether some object at the end of `steps`, inside `value` known to be walkable by them, satisfies `condition`. */
|
|
279
|
+
function anyObject(value, steps, condition, depth) {
|
|
280
|
+
const [step, ...rest] = steps;
|
|
281
|
+
if (step === void 0) return condition(value);
|
|
282
|
+
const guarded = (child, childDepth) => ({ $cond: [
|
|
283
|
+
walkable(child, rest),
|
|
284
|
+
anyObject(child, rest, condition, childDepth),
|
|
285
|
+
false
|
|
286
|
+
] });
|
|
287
|
+
if (step.kind === "wildcard") {
|
|
288
|
+
const as = elementVariable(depth);
|
|
289
|
+
return { $anyElementTrue: [{ $map: {
|
|
290
|
+
input: value,
|
|
291
|
+
as,
|
|
292
|
+
in: guarded(`$$${as}`, depth + 1)
|
|
293
|
+
} }] };
|
|
294
|
+
}
|
|
295
|
+
return guarded(keyOf(value, step.name), depth);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* The update pipeline that rewrites each object at the key path's objects with `rewrite`. A missing
|
|
299
|
+
* or `null` value, or a value of another type than the step expects, is left as it is.
|
|
300
|
+
*/
|
|
301
|
+
function objectsRewritePipeline(keyPath, rewrite) {
|
|
302
|
+
const root = `$${keyPath.field}`;
|
|
303
|
+
return [{ $set: { [keyPath.field]: { $cond: [
|
|
304
|
+
walkable(root, keyPath.objects),
|
|
305
|
+
rewritten(root, keyPath.objects, rewrite, 0),
|
|
306
|
+
root
|
|
307
|
+
] } } }];
|
|
308
|
+
}
|
|
309
|
+
/** The filter that matches the documents where some object at the key path's objects satisfies `condition`. */
|
|
310
|
+
function documentsWithObject(keyPath, condition) {
|
|
311
|
+
const root = `$${keyPath.field}`;
|
|
312
|
+
return { $expr: { $cond: [
|
|
313
|
+
walkable(root, keyPath.objects),
|
|
314
|
+
anyObject(root, keyPath.objects, condition, 0),
|
|
315
|
+
false
|
|
316
|
+
] } };
|
|
317
|
+
}
|
|
66
318
|
function isBuildable(value) {
|
|
67
319
|
if (typeof value !== "object" || value === null || !("build" in value)) return false;
|
|
68
320
|
return typeof Reflect.get(value, "build") === "function";
|
|
@@ -123,27 +375,27 @@ function documentsHolding(fields, filter) {
|
|
|
123
375
|
const [only] = conditions;
|
|
124
376
|
return conditions.length === 1 && only !== void 0 ? only : { $and: conditions };
|
|
125
377
|
}
|
|
126
|
-
const
|
|
378
|
+
const UNREWRITABLE_KEYS = [
|
|
127
379
|
{
|
|
128
|
-
matches: (
|
|
380
|
+
matches: (key) => key === "",
|
|
129
381
|
reason: "it is empty"
|
|
130
382
|
},
|
|
131
383
|
{
|
|
132
|
-
matches: (
|
|
133
|
-
reason: "MongoDB does not let a document change its _id"
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
matches: (field) => field.includes("."),
|
|
384
|
+
matches: (key) => key.includes("."),
|
|
137
385
|
reason: "MongoDB reads a name containing \".\" as a path into embedded documents"
|
|
138
386
|
},
|
|
139
387
|
{
|
|
140
|
-
matches: (
|
|
388
|
+
matches: (key) => key.startsWith("$"),
|
|
141
389
|
reason: "MongoDB reads a name starting with \"$\" as an operator"
|
|
142
390
|
}
|
|
143
391
|
];
|
|
392
|
+
/** Why a key of a document or of an object inside one cannot be rewritten, if it cannot. */
|
|
393
|
+
function unrewritableKeyReason(key) {
|
|
394
|
+
return UNREWRITABLE_KEYS.find((rule) => rule.matches(key))?.reason;
|
|
395
|
+
}
|
|
144
396
|
/** Why `renameField` and `unsetField` cannot rewrite a field of this name, if they cannot. */
|
|
145
397
|
function unrewritableFieldNameReason(field) {
|
|
146
|
-
return
|
|
398
|
+
return field === "_id" ? "MongoDB does not let a document change its _id" : unrewritableKeyReason(field);
|
|
147
399
|
}
|
|
148
400
|
/** The plan meta of the raw queries a migration's operations run and check with. */
|
|
149
401
|
const RAW_PLAN_META = {
|
|
@@ -364,6 +616,362 @@ function fillField(options) {
|
|
|
364
616
|
}]
|
|
365
617
|
};
|
|
366
618
|
}
|
|
619
|
+
/** Where the objects that hold a key are: the path to them, and the root field and collection. */
|
|
620
|
+
function placeAt(options, path) {
|
|
621
|
+
return `at path ${JSON.stringify(path)} in field ${options.field} of collection ${options.collection}`;
|
|
622
|
+
}
|
|
623
|
+
function keyPlace(options, keyPath) {
|
|
624
|
+
return placeAt(options, keyPath.objectsText);
|
|
625
|
+
}
|
|
626
|
+
function assertRewritableKey(options, keyPath, key) {
|
|
627
|
+
const reason = unrewritableKeyReason(key);
|
|
628
|
+
if (reason === void 0) return;
|
|
629
|
+
throw mongoTargetError("MIGRATION.FIELD_NAME_UNSUPPORTED", `Key ${JSON.stringify(key)} ${keyPlace(options, keyPath)} cannot be rewritten: ${reason}.`, { meta: {
|
|
630
|
+
collection: options.collection,
|
|
631
|
+
field: options.field,
|
|
632
|
+
path: options.path,
|
|
633
|
+
key,
|
|
634
|
+
reason
|
|
635
|
+
} });
|
|
636
|
+
}
|
|
637
|
+
/** The key path of a stored key operation, refusing a root field or a key MongoDB cannot rewrite. */
|
|
638
|
+
function rewritableKeyPath(factory, options) {
|
|
639
|
+
assertRewritableField(options.collection, options.field);
|
|
640
|
+
const keyPath = storedKeyPath(options.field, options.path);
|
|
641
|
+
if (!keyPath.ok) throw mongoTargetError("MIGRATION.INVALID_OPERATION_ENTRY", `${factory} cannot act ${placeAt(options, options.path)}: ${keyPath.failure}.`, { meta: {
|
|
642
|
+
context: factory,
|
|
643
|
+
collection: options.collection,
|
|
644
|
+
field: options.field,
|
|
645
|
+
path: options.path,
|
|
646
|
+
reason: keyPath.failure
|
|
647
|
+
} });
|
|
648
|
+
for (const key of keysOf(keyPath.value)) assertRewritableKey(options, keyPath.value, key);
|
|
649
|
+
return keyPath.value;
|
|
650
|
+
}
|
|
651
|
+
function withFilter(filter, condition) {
|
|
652
|
+
return filter === void 0 ? condition : { $and: [filter, condition] };
|
|
653
|
+
}
|
|
654
|
+
function storedKeyId(options, action) {
|
|
655
|
+
return `storedKey.${options.collection}.${options.field}.${options.path}.${action}${idSuffix(options.filter)}`;
|
|
656
|
+
}
|
|
657
|
+
function noDocumentMatches(collectionName, match, description) {
|
|
658
|
+
return {
|
|
659
|
+
description,
|
|
660
|
+
source: rawAggregate(collectionName, [{ $match: match }, { $limit: 1 }]),
|
|
661
|
+
filter: MATCH_ALL_FILTER,
|
|
662
|
+
expect: "notExists"
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
function storedKeyRewrite(options, toRewrite, keyPath, rewrite) {
|
|
666
|
+
return rawUpdateMany(options.collection, toRewrite, objectsRewritePipeline(keyPath, rewrite), { bypassDocumentValidation: true });
|
|
667
|
+
}
|
|
668
|
+
function literal(key) {
|
|
669
|
+
return { $literal: key };
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Renames the last key of `path` to `to` in every object at the path inside root field `field`,
|
|
673
|
+
* skipping document validation. The precheck stops when an object holds both names.
|
|
674
|
+
*/
|
|
675
|
+
function renameStoredKey(options) {
|
|
676
|
+
const keyPath = rewritableKeyPath("renameStoredKey", options);
|
|
677
|
+
const { collection: collectionName, field, path, to, filter } = options;
|
|
678
|
+
assertRewritableKey(options, keyPath, to);
|
|
679
|
+
const { key } = keyPath;
|
|
680
|
+
if (key === to) throw mongoTargetError("MIGRATION.INVALID_OPERATION_ENTRY", `renameStoredKey renames key ${JSON.stringify(key)} ${keyPlace(options, keyPath)} to its own name.`, { meta: {
|
|
681
|
+
context: "renameStoredKey",
|
|
682
|
+
collection: collectionName,
|
|
683
|
+
field,
|
|
684
|
+
path,
|
|
685
|
+
reason: "the new name is the old name"
|
|
686
|
+
} });
|
|
687
|
+
const place = keyPlace(options, keyPath);
|
|
688
|
+
const holdingKey = withFilter(filter, documentsWithObject(keyPath, (object) => holdsKey(object, key)));
|
|
689
|
+
const holdingBoth = withFilter(filter, documentsWithObject(keyPath, (object) => ({ $and: [holdsKey(object, key), holdsKey(object, to)] })));
|
|
690
|
+
const id = storedKeyId(options, `rename(${to})`);
|
|
691
|
+
return {
|
|
692
|
+
id,
|
|
693
|
+
meta: { rewrite: {
|
|
694
|
+
kind: "renameStoredKey",
|
|
695
|
+
collection: collectionName,
|
|
696
|
+
field,
|
|
697
|
+
path,
|
|
698
|
+
to
|
|
699
|
+
} },
|
|
700
|
+
label: `Rename key ${JSON.stringify(key)} to ${JSON.stringify(to)} ${place}${labelSuffix(filter)}`,
|
|
701
|
+
operationClass: "widening",
|
|
702
|
+
name: id,
|
|
703
|
+
precheck: [noDocumentMatches(collectionName, holdingBoth, `no object ${place} holds both key ${JSON.stringify(key)} and key ${JSON.stringify(to)}; remove one of the two keys from those objects`)],
|
|
704
|
+
run: [storedKeyRewrite(options, holdingKey, keyPath, (object) => ({ $cond: [
|
|
705
|
+
holdsKey(object, key),
|
|
706
|
+
{ $setField: {
|
|
707
|
+
field: literal(to),
|
|
708
|
+
input: { $unsetField: {
|
|
709
|
+
field: literal(key),
|
|
710
|
+
input: object
|
|
711
|
+
} },
|
|
712
|
+
value: keyOf(object, key)
|
|
713
|
+
} },
|
|
714
|
+
object
|
|
715
|
+
] }))],
|
|
716
|
+
postcheck: [noDocumentMatches(collectionName, holdingKey, `no object ${place} holds key ${JSON.stringify(key)}`)]
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
/** Removes the last key of `path` from every object at the path inside root field `field`, skipping document validation. */
|
|
720
|
+
function removeStoredKey(options) {
|
|
721
|
+
const keyPath = rewritableKeyPath("removeStoredKey", options);
|
|
722
|
+
const { collection: collectionName, field, path, filter } = options;
|
|
723
|
+
const { key } = keyPath;
|
|
724
|
+
const holdingKey = withFilter(filter, documentsWithObject(keyPath, (object) => holdsKey(object, key)));
|
|
725
|
+
const id = storedKeyId(options, "remove");
|
|
726
|
+
return {
|
|
727
|
+
id,
|
|
728
|
+
meta: { rewrite: {
|
|
729
|
+
kind: "removeStoredKey",
|
|
730
|
+
collection: collectionName,
|
|
731
|
+
field,
|
|
732
|
+
path
|
|
733
|
+
} },
|
|
734
|
+
label: `Remove key ${JSON.stringify(key)} ${keyPlace(options, keyPath)}${labelSuffix(filter)}`,
|
|
735
|
+
operationClass: "destructive",
|
|
736
|
+
name: id,
|
|
737
|
+
precheck: [],
|
|
738
|
+
run: [storedKeyRewrite(options, holdingKey, keyPath, (object) => ({ $unsetField: {
|
|
739
|
+
field: literal(key),
|
|
740
|
+
input: object
|
|
741
|
+
} }))],
|
|
742
|
+
postcheck: [noDocumentMatches(collectionName, holdingKey, `no object ${keyPlace(options, keyPath)} holds key ${JSON.stringify(key)}`)]
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Sets the last key of `path` to `value` in every object at the path inside root field `field`
|
|
747
|
+
* where it is missing or `null`, skipping document validation.
|
|
748
|
+
*/
|
|
749
|
+
function fillStoredKey(options) {
|
|
750
|
+
const keyPath = rewritableKeyPath("fillStoredKey", options);
|
|
751
|
+
const { collection: collectionName, field, path, value, filter } = options;
|
|
752
|
+
const { key } = keyPath;
|
|
753
|
+
const lackingKey = withFilter(filter, documentsWithObject(keyPath, (object) => lacksKey(object, key)));
|
|
754
|
+
const id = storedKeyId(options, "fill");
|
|
755
|
+
return {
|
|
756
|
+
id,
|
|
757
|
+
meta: { rewrite: {
|
|
758
|
+
kind: "fillStoredKey",
|
|
759
|
+
collection: collectionName,
|
|
760
|
+
field,
|
|
761
|
+
path
|
|
762
|
+
} },
|
|
763
|
+
label: `Fill key ${JSON.stringify(key)} ${keyPlace(options, keyPath)} where it is missing or null${labelSuffix(filter)}`,
|
|
764
|
+
operationClass: "widening",
|
|
765
|
+
name: id,
|
|
766
|
+
precheck: [],
|
|
767
|
+
run: [storedKeyRewrite(options, lackingKey, keyPath, (object) => ({ $cond: [
|
|
768
|
+
lacksKey(object, key),
|
|
769
|
+
{ $setField: {
|
|
770
|
+
field: literal(key),
|
|
771
|
+
input: object,
|
|
772
|
+
value
|
|
773
|
+
} },
|
|
774
|
+
object
|
|
775
|
+
] }))],
|
|
776
|
+
postcheck: [noDocumentMatches(collectionName, lackingKey, `no object ${keyPlace(options, keyPath)} lacks key ${JSON.stringify(key)} or holds null there`)]
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
function isStoredValue(value) {
|
|
780
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
781
|
+
}
|
|
782
|
+
function storedValuePlace(factory, options, value) {
|
|
783
|
+
const { collection: collectionName, field, path } = options;
|
|
784
|
+
if (!isStoredValue(value)) throw mongoTargetError("MIGRATION.INVALID_OPERATION_ENTRY", `${factory} cannot match value ${JSON.stringify(value)} in field ${field} of collection ${collectionName}: it compares only strings, numbers and booleans.`, { meta: {
|
|
785
|
+
context: factory,
|
|
786
|
+
collection: collectionName,
|
|
787
|
+
field,
|
|
788
|
+
reason: "the value is not a string, a number or a boolean"
|
|
789
|
+
} });
|
|
790
|
+
if (path === void 0) {
|
|
791
|
+
assertRewritableField(collectionName, field);
|
|
792
|
+
return {
|
|
793
|
+
keyPath: void 0,
|
|
794
|
+
text: `in field ${field} of collection ${collectionName}`
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
const at = {
|
|
798
|
+
collection: collectionName,
|
|
799
|
+
field,
|
|
800
|
+
path
|
|
801
|
+
};
|
|
802
|
+
return {
|
|
803
|
+
keyPath: rewritableKeyPath(factory, at),
|
|
804
|
+
text: placeAt(at, path)
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
function equalsValue(held, value) {
|
|
808
|
+
return { $eq: [held, { $literal: value }] };
|
|
809
|
+
}
|
|
810
|
+
/** Whether a value is `value`, or a list with an element equal to it. */
|
|
811
|
+
function holdsValue(held, value) {
|
|
812
|
+
return { $cond: [
|
|
813
|
+
{ $isArray: held },
|
|
814
|
+
{ $in: [{ $literal: value }, held] },
|
|
815
|
+
equalsValue(held, value)
|
|
816
|
+
] };
|
|
817
|
+
}
|
|
818
|
+
/** The documents in which the value at the place satisfies `condition`. */
|
|
819
|
+
function documentsWhereValue(options, place, condition) {
|
|
820
|
+
const { keyPath } = place;
|
|
821
|
+
const match = keyPath === void 0 ? { $expr: condition(`$${options.field}`) } : documentsWithObject(keyPath, (object) => condition(keyOf(object, keyPath.key)));
|
|
822
|
+
return withFilter(options.filter, match);
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* The update pipeline that rewrites the value at the place: a list with `inList`, and a value equal
|
|
826
|
+
* to `matched` with `replacement`, or, when it is `undefined`, by removing the field or key.
|
|
827
|
+
*/
|
|
828
|
+
function storedValuePipeline(options, place, matched, inList, replacement) {
|
|
829
|
+
const { keyPath } = place;
|
|
830
|
+
if (keyPath === void 0) {
|
|
831
|
+
const held = `$${options.field}`;
|
|
832
|
+
return [{ $set: { [options.field]: { $cond: [
|
|
833
|
+
{ $isArray: held },
|
|
834
|
+
inList(held),
|
|
835
|
+
{ $cond: [
|
|
836
|
+
equalsValue(held, matched),
|
|
837
|
+
replacement === void 0 ? "$$REMOVE" : replacement,
|
|
838
|
+
held
|
|
839
|
+
] }
|
|
840
|
+
] } } }];
|
|
841
|
+
}
|
|
842
|
+
const key = literal(keyPath.key);
|
|
843
|
+
return objectsRewritePipeline(keyPath, (object) => {
|
|
844
|
+
const held = keyOf(object, keyPath.key);
|
|
845
|
+
const set = (value) => ({ $setField: {
|
|
846
|
+
field: key,
|
|
847
|
+
input: object,
|
|
848
|
+
value
|
|
849
|
+
} });
|
|
850
|
+
return { $cond: [
|
|
851
|
+
{ $isArray: held },
|
|
852
|
+
set(inList(held)),
|
|
853
|
+
{ $cond: [
|
|
854
|
+
equalsValue(held, matched),
|
|
855
|
+
replacement === void 0 ? { $unsetField: {
|
|
856
|
+
field: key,
|
|
857
|
+
input: object
|
|
858
|
+
} } : set(replacement),
|
|
859
|
+
object
|
|
860
|
+
] }
|
|
861
|
+
] };
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
function storedValueId(options, action) {
|
|
865
|
+
const path = options.path === void 0 ? "" : `.${options.path}`;
|
|
866
|
+
return `storedValue.${options.collection}.${options.field}${path}.${action}${idSuffix(options.filter)}`;
|
|
867
|
+
}
|
|
868
|
+
const TYPED_CONSTANT = /* @__PURE__ */ new Set([
|
|
869
|
+
"$literal",
|
|
870
|
+
"$toDouble",
|
|
871
|
+
"$toLong",
|
|
872
|
+
"$toInt",
|
|
873
|
+
"$toDecimal"
|
|
874
|
+
]);
|
|
875
|
+
/** The constant an expression writes, as a label shows it: unwrapped from `$literal` or a cast. */
|
|
876
|
+
function expressionText(value) {
|
|
877
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
878
|
+
const entries = Object.entries(value);
|
|
879
|
+
const [only] = entries;
|
|
880
|
+
if (entries.length === 1 && only !== void 0 && TYPED_CONSTANT.has(only[0])) return expressionText(only[1] ?? null);
|
|
881
|
+
}
|
|
882
|
+
return JSON.stringify(value);
|
|
883
|
+
}
|
|
884
|
+
function holdingDescription(options, value) {
|
|
885
|
+
const at = options.path === void 0 ? "" : ` at path ${JSON.stringify(options.path)}`;
|
|
886
|
+
return `no document in ${options.collection} holds value ${JSON.stringify(value)}${at} in field ${options.field}`;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Replaces the stored value `from` with `to` where the field, or the key at `path` inside it, holds
|
|
890
|
+
* it: the value itself, or each element of a list equal to it. Skips document validation.
|
|
891
|
+
*/
|
|
892
|
+
function replaceStoredValue(options) {
|
|
893
|
+
const { collection: collectionName, field, path, from, to, filter } = options;
|
|
894
|
+
const place = storedValuePlace("replaceStoredValue", options, from);
|
|
895
|
+
const holding = documentsWhereValue(options, place, (held) => holdsValue(held, from));
|
|
896
|
+
const id = storedValueId(options, `replace(${JSON.stringify(from)})`);
|
|
897
|
+
return {
|
|
898
|
+
id,
|
|
899
|
+
meta: { rewrite: {
|
|
900
|
+
kind: "replaceStoredValue",
|
|
901
|
+
collection: collectionName,
|
|
902
|
+
field,
|
|
903
|
+
...ifDefined("path", path)
|
|
904
|
+
} },
|
|
905
|
+
label: `Replace value ${JSON.stringify(from)} with ${expressionText(to)} ${place.text}${labelSuffix(filter)}`,
|
|
906
|
+
operationClass: "widening",
|
|
907
|
+
name: id,
|
|
908
|
+
precheck: [],
|
|
909
|
+
run: [rawUpdateMany(collectionName, holding, storedValuePipeline(options, place, from, (list) => ({ $map: {
|
|
910
|
+
input: list,
|
|
911
|
+
as: "value",
|
|
912
|
+
in: { $cond: [
|
|
913
|
+
equalsValue("$$value", from),
|
|
914
|
+
to,
|
|
915
|
+
"$$value"
|
|
916
|
+
] }
|
|
917
|
+
} }), to), { bypassDocumentValidation: true })],
|
|
918
|
+
postcheck: [noDocumentMatches(collectionName, holding, holdingDescription(options, from))]
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* Removes the stored value `value` where the field, or the key at `path` inside it, holds it: the
|
|
923
|
+
* field or key goes where it equals the value, and the value is pulled from a list. With
|
|
924
|
+
* `required`, the operation first checks that no document holds the value there outside a list.
|
|
925
|
+
* Skips document validation.
|
|
926
|
+
*/
|
|
927
|
+
function removeStoredValue(options) {
|
|
928
|
+
const { collection: collectionName, field, path, value, filter, required, enumValue } = options;
|
|
929
|
+
const place = storedValuePlace("removeStoredValue", options, value);
|
|
930
|
+
const holding = documentsWhereValue(options, place, (held) => holdsValue(held, value));
|
|
931
|
+
const id = storedValueId(options, `remove(${JSON.stringify(value)})`);
|
|
932
|
+
return {
|
|
933
|
+
id,
|
|
934
|
+
meta: { rewrite: {
|
|
935
|
+
kind: "removeStoredValue",
|
|
936
|
+
collection: collectionName,
|
|
937
|
+
field,
|
|
938
|
+
...ifDefined("path", path),
|
|
939
|
+
value,
|
|
940
|
+
...ifDefined("required", required),
|
|
941
|
+
...ifDefined("filter", filter),
|
|
942
|
+
...ifDefined("enumValue", enumValue)
|
|
943
|
+
} },
|
|
944
|
+
label: `Remove value ${JSON.stringify(value)} ${path === void 0 ? `from field ${field} of collection ${collectionName}` : place.text}${labelSuffix(filter)}`,
|
|
945
|
+
operationClass: "destructive",
|
|
946
|
+
name: id,
|
|
947
|
+
precheck: required === true ? [noDocumentMatches(collectionName, documentsWhereValue(options, place, (held) => equalsValue(held, value)), `${holdingDescription(options, value)}, which is required`)] : [],
|
|
948
|
+
run: [rawUpdateMany(collectionName, holding, storedValuePipeline(options, place, value, (list) => ({ $filter: {
|
|
949
|
+
input: list,
|
|
950
|
+
as: "value",
|
|
951
|
+
cond: { $not: [equalsValue("$$value", value)] }
|
|
952
|
+
} }), void 0), { bypassDocumentValidation: true })],
|
|
953
|
+
postcheck: [noDocumentMatches(collectionName, holding, holdingDescription(options, value))]
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
/** The documents a filter selects, in words where the planner wrote it, for an operation's label. */
|
|
957
|
+
function selectedText(filter) {
|
|
958
|
+
const words = Object.entries(filter).map(([field, condition]) => conditionInWords(field, condition));
|
|
959
|
+
return words.length > 0 && words.every((word) => word !== void 0) ? `whose ${words.join(" and ")}` : `matching ${JSON.stringify(filter)}`;
|
|
960
|
+
}
|
|
961
|
+
/** Deletes the documents of a collection that `filter` selects, as a variant's documents are deleted. */
|
|
962
|
+
function deleteDocuments(options) {
|
|
963
|
+
const { collection: collectionName, filter } = options;
|
|
964
|
+
const id = `documents.${collectionName}.delete${idSuffix(filter)}`;
|
|
965
|
+
return {
|
|
966
|
+
id,
|
|
967
|
+
label: `Delete the documents of collection ${collectionName} ${selectedText(filter)}`,
|
|
968
|
+
operationClass: "destructive",
|
|
969
|
+
name: id,
|
|
970
|
+
precheck: [],
|
|
971
|
+
run: [rewritePlan(collectionName, new RawDeleteManyCommand(collectionName, filter))],
|
|
972
|
+
postcheck: [noDocumentMatches(collectionName, filter, `no document in ${collectionName} matches ${JSON.stringify(filter)}`)]
|
|
973
|
+
};
|
|
974
|
+
}
|
|
367
975
|
function formatKeys(keys) {
|
|
368
976
|
return keys.map((k) => `${k.field}:${k.direction}`).join(", ");
|
|
369
977
|
}
|
|
@@ -647,6 +1255,6 @@ function validatedCollection(name, schema, indexes) {
|
|
|
647
1255
|
}), ...indexes.map((idx) => createIndex(name, idx.keys, idx.unique !== void 0 ? { unique: idx.unique } : void 0))];
|
|
648
1256
|
}
|
|
649
1257
|
//#endregion
|
|
650
|
-
export { setValidation as _, createIndex as a,
|
|
1258
|
+
export { unrewritableKeyReason as A, renameCollection as C, setValidation as D, replaceStoredValue as E, validatedCollection as M, storedKeyPath as O, removeStoredValue as S, renameStoredKey as T, keysOf as _, createIndex as a, rawUpdateMany as b, documentsMissingOrNull as c, dropCollection as d, dropIndex as f, jsonPathText as g, isConvertTarget as h, createCollection as i, unsetField as j, unrewritableFieldNameReason as k, documentsNotOfType as l, fillStoredKey as m, castsFrom as n, dataTransform as o, fillField as p, collMod as r, deleteDocuments as s, castField as t, documentsWithObject as u, lacksKey as v, renameField as w, removeStoredKey as x, rawAggregate as y };
|
|
651
1259
|
|
|
652
|
-
//# sourceMappingURL=migration-factories-
|
|
1260
|
+
//# sourceMappingURL=migration-factories-RJZq8NhE-CigCh286.mjs.map
|