@web-ts-toolkit/moo 0.42.2 → 0.47.1
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/README.md +122 -7
- package/index.d.mts +2 -2
- package/index.d.ts +2 -2
- package/index.js +386 -57
- package/index.mjs +386 -57
- package/is.d.mts +2 -2
- package/is.d.ts +2 -2
- package/is.js +8 -7
- package/is.mjs +8 -7
- package/llms.txt +33 -5
- package/package.json +3 -3
- package/plugins/cascade-delete.d.mts +151 -4
- package/plugins/cascade-delete.d.ts +151 -4
- package/plugins/cascade-delete.js +354 -33
- package/plugins/cascade-delete.mjs +354 -33
- package/plugins/index.d.mts +1 -1
- package/plugins/index.d.ts +1 -1
- package/plugins/index.js +354 -33
- package/plugins/index.mjs +354 -33
- package/plugins/keycloak-user-sync.d.mts +32 -4
- package/plugins/keycloak-user-sync.d.ts +32 -4
- package/plugins/keycloak-user-sync.js +208 -45
- package/plugins/keycloak-user-sync.mjs +208 -45
- package/plugins/model-function.d.mts +46 -0
- package/plugins/model-function.d.ts +46 -0
- package/plugins/new-document.d.mts +21 -2
- package/plugins/new-document.d.ts +21 -2
- package/schema.d.mts +34 -5
- package/schema.d.ts +34 -5
- package/schema.js +24 -17
- package/schema.mjs +24 -17
|
@@ -62,6 +62,121 @@ var resolveFilter = (value, document) => {
|
|
|
62
62
|
}
|
|
63
63
|
return result;
|
|
64
64
|
};
|
|
65
|
+
var isNonEmptyString = (value) => typeof value === "string" && value.trim().length > 0;
|
|
66
|
+
var isEmptyFilterObject = (value) => Object.keys(value).length === 0;
|
|
67
|
+
var EMPTY_FULL_FILTER_MESSAGE = "cascadeDeletePlugin rejects empty foreignFilter by default because {} would match every document in the dependent collection; provide a non-empty filter object. Broad deletes are not implicitly enabled.";
|
|
68
|
+
var DEFAULT_MAX_CONCURRENCY = 8;
|
|
69
|
+
var DEFAULT_BATCH_SIZE = 100;
|
|
70
|
+
var assertPositiveIntOption = (value, name, modelName) => {
|
|
71
|
+
if (value === void 0) return;
|
|
72
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`cascadeDeletePlugin for model "${modelName}" requires \`${name}\` to be an integer >= 1 when provided`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
var assertPluginOptions = (options) => {
|
|
79
|
+
if (!(0, import_utils2.isPlainObject)(options)) {
|
|
80
|
+
throw new Error("cascadeDeletePlugin requires options with a non-empty `model` name");
|
|
81
|
+
}
|
|
82
|
+
const { model, localField, foreignField, foreignFilter, extraForeignFilter } = options;
|
|
83
|
+
if (!isNonEmptyString(model)) {
|
|
84
|
+
throw new Error("cascadeDeletePlugin requires a non-empty `model` name");
|
|
85
|
+
}
|
|
86
|
+
if (localField !== void 0 && !isNonEmptyString(localField)) {
|
|
87
|
+
throw new Error("cascadeDeletePlugin requires `localField` to be a non-empty string when provided");
|
|
88
|
+
}
|
|
89
|
+
assertPositiveIntOption(options.maxConcurrency, "maxConcurrency", model);
|
|
90
|
+
assertPositiveIntOption(options.batchSize, "batchSize", model);
|
|
91
|
+
if (foreignField !== void 0 && !isNonEmptyString(foreignField)) {
|
|
92
|
+
throw new Error("cascadeDeletePlugin requires `foreignField` to be a non-empty string when provided");
|
|
93
|
+
}
|
|
94
|
+
if (foreignFilter !== void 0) {
|
|
95
|
+
if (!(0, import_utils2.isFunction)(foreignFilter) && !(0, import_utils2.isPlainObject)(foreignFilter)) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`cascadeDeletePlugin invalid \`foreignFilter\` for model "${model}": expected a plain filter object or a resolver function`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if ((0, import_utils2.isPlainObject)(foreignFilter) && isEmptyFilterObject(foreignFilter)) {
|
|
101
|
+
throw new Error(`cascadeDeletePlugin ${EMPTY_FULL_FILTER_MESSAGE}`);
|
|
102
|
+
}
|
|
103
|
+
} else if (!isNonEmptyString(localField) || !isNonEmptyString(foreignField)) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`cascadeDeletePlugin for model "${model}" requires either \`foreignFilter\` or both \`localField\` and \`foreignField\``
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (extraForeignFilter !== void 0) {
|
|
109
|
+
if (!(0, import_utils2.isFunction)(extraForeignFilter) && !(0, import_utils2.isPlainObject)(extraForeignFilter)) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`cascadeDeletePlugin invalid \`extraForeignFilter\` for model "${model}": expected a plain filter object or a resolver function`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var assertFullFilterNotEmpty = (filter, modelName) => {
|
|
117
|
+
if (isEmptyFilterObject(filter)) {
|
|
118
|
+
throw new Error(`cascadeDeletePlugin for model "${modelName}" ${EMPTY_FULL_FILTER_MESSAGE}`);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
var isMissingScalarKey = (value) => {
|
|
122
|
+
if (value === void 0 || value === null) return true;
|
|
123
|
+
if (typeof value === "string" && value.trim().length === 0) return true;
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
var unwrapKeyValue = (value) => {
|
|
127
|
+
if ((0, import_utils2.isPlainObject)(value)) {
|
|
128
|
+
const candidate = value._id;
|
|
129
|
+
if (candidate !== void 0 && candidate !== null) return candidate;
|
|
130
|
+
}
|
|
131
|
+
if ((0, import_utils2.isObject)(value)) {
|
|
132
|
+
try {
|
|
133
|
+
const candidate = value._id;
|
|
134
|
+
if (candidate !== void 0 && candidate !== null) return candidate;
|
|
135
|
+
} catch {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
};
|
|
140
|
+
var normalizeKeyList = (localValue) => {
|
|
141
|
+
if (Array.isArray(localValue)) {
|
|
142
|
+
const keys = [];
|
|
143
|
+
for (const entry of localValue) {
|
|
144
|
+
const unwrapped2 = unwrapKeyValue(entry);
|
|
145
|
+
if (!isMissingScalarKey(unwrapped2)) keys.push(unwrapped2);
|
|
146
|
+
}
|
|
147
|
+
return keys.length > 0 ? keys : null;
|
|
148
|
+
}
|
|
149
|
+
const unwrapped = unwrapKeyValue(localValue);
|
|
150
|
+
if (isMissingScalarKey(unwrapped)) return null;
|
|
151
|
+
return [unwrapped];
|
|
152
|
+
};
|
|
153
|
+
var isPathDeselected = (owner, path) => {
|
|
154
|
+
try {
|
|
155
|
+
const candidate = owner;
|
|
156
|
+
if (typeof candidate.$isSelected === "function") {
|
|
157
|
+
const result = candidate.$isSelected(path);
|
|
158
|
+
if (typeof result === "boolean") return result === false;
|
|
159
|
+
}
|
|
160
|
+
if (typeof candidate.isSelected === "function") {
|
|
161
|
+
const result = candidate.isSelected(path);
|
|
162
|
+
if (typeof result === "boolean") return result === false;
|
|
163
|
+
}
|
|
164
|
+
} catch {
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
};
|
|
168
|
+
var buildRelationshipPredicate = (foreignField, localValue) => {
|
|
169
|
+
const keys = normalizeKeyList(localValue);
|
|
170
|
+
if (!keys) return null;
|
|
171
|
+
if (Array.isArray(localValue)) {
|
|
172
|
+
return { [foreignField]: { $in: keys } };
|
|
173
|
+
}
|
|
174
|
+
return { [foreignField]: keys[0] };
|
|
175
|
+
};
|
|
176
|
+
var combineWithRelationship = (relationship, extra) => {
|
|
177
|
+
if (!extra || isEmptyFilterObject(extra)) return relationship;
|
|
178
|
+
return { $and: [relationship, extra] };
|
|
179
|
+
};
|
|
65
180
|
var applyQueryOptions = (query, options) => {
|
|
66
181
|
let builder = query;
|
|
67
182
|
if (options.select) {
|
|
@@ -76,8 +191,81 @@ var applyQueryOptions = (query, options) => {
|
|
|
76
191
|
if (options.lean) {
|
|
77
192
|
builder = builder.lean();
|
|
78
193
|
}
|
|
194
|
+
if (options.session) {
|
|
195
|
+
builder = builder.session(options.session);
|
|
196
|
+
}
|
|
79
197
|
return builder;
|
|
80
198
|
};
|
|
199
|
+
var getOwningConnection = (owner) => {
|
|
200
|
+
const candidate = owner?.collection?.conn ?? owner?.constructor?.collection?.conn ?? null;
|
|
201
|
+
if (candidate && typeof candidate.model === "function") {
|
|
202
|
+
return candidate;
|
|
203
|
+
}
|
|
204
|
+
return import_mongoose2.default;
|
|
205
|
+
};
|
|
206
|
+
var getBoundSession = (owner) => {
|
|
207
|
+
try {
|
|
208
|
+
const session = owner?.$session?.() ?? null;
|
|
209
|
+
return session ?? null;
|
|
210
|
+
} catch {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
var resolveExecutionContext = (owner, targetModelName, explicitSession) => {
|
|
215
|
+
const session = explicitSession ?? getBoundSession(owner) ?? null;
|
|
216
|
+
const connection = getOwningConnection(owner);
|
|
217
|
+
const Target = connection.model(targetModelName);
|
|
218
|
+
return { connection, Target, session };
|
|
219
|
+
};
|
|
220
|
+
var isInTransaction = (session) => {
|
|
221
|
+
try {
|
|
222
|
+
return typeof session?.inTransaction === "function" ? session.inTransaction() : false;
|
|
223
|
+
} catch {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var getSessionClient = (session) => {
|
|
228
|
+
if (!session) return null;
|
|
229
|
+
return session.client ?? null;
|
|
230
|
+
};
|
|
231
|
+
var getConnectionClient = (connection) => {
|
|
232
|
+
try {
|
|
233
|
+
if (connection === import_mongoose2.default) {
|
|
234
|
+
return import_mongoose2.default.connection.getClient?.() ?? null;
|
|
235
|
+
}
|
|
236
|
+
return connection.getClient?.() ?? null;
|
|
237
|
+
} catch {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var getTopologyType = (client) => {
|
|
242
|
+
try {
|
|
243
|
+
const topology = client?.topology;
|
|
244
|
+
const type = topology?.description?.type;
|
|
245
|
+
return typeof type === "string" ? type : null;
|
|
246
|
+
} catch {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var assertTransactionSupported = (context) => {
|
|
251
|
+
const { connection, session } = context;
|
|
252
|
+
if (!session) return;
|
|
253
|
+
const sessionClient = getSessionClient(session);
|
|
254
|
+
const connectionClient = getConnectionClient(connection);
|
|
255
|
+
if (sessionClient && connectionClient && sessionClient !== connectionClient) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
"cascadeDeletePlugin does not support sessions bound to a different connection/client; pass a session from the owning model connection"
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
if (isInTransaction(session)) {
|
|
261
|
+
const topologyType = getTopologyType(sessionClient);
|
|
262
|
+
if (topologyType === "Single") {
|
|
263
|
+
throw new Error(
|
|
264
|
+
"cascadeDeletePlugin does not support transactions on a standalone MongoDB deployment; use a replica set or sharded cluster, or delete without a transaction"
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
81
269
|
var supportsDeleteOne = (value) => (0, import_utils2.isObject)(value) && typeof value.deleteOne === "function";
|
|
82
270
|
var supportsRemove = (value) => (0, import_utils2.isObject)(value) && typeof value.remove === "function";
|
|
83
271
|
var mergeResults = (previousResults, modelName, currentResults) => {
|
|
@@ -90,56 +278,189 @@ var mergeResults = (previousResults, modelName, currentResults) => {
|
|
|
90
278
|
[modelName]: currentResults
|
|
91
279
|
};
|
|
92
280
|
};
|
|
281
|
+
var resolveDependentFilter = (owner, config) => {
|
|
282
|
+
const { model, localField, foreignField, foreignFilter, extraForeignFilter } = config;
|
|
283
|
+
const document = owner.toObject({ virtuals: false });
|
|
284
|
+
if (foreignFilter !== void 0) {
|
|
285
|
+
const resolved = resolveFilter(foreignFilter, document);
|
|
286
|
+
if (resolved === null) return null;
|
|
287
|
+
assertFullFilterNotEmpty(resolved, model);
|
|
288
|
+
return resolved;
|
|
289
|
+
}
|
|
290
|
+
if (localField && foreignField) {
|
|
291
|
+
const localValue = owner.get(localField);
|
|
292
|
+
if (normalizeKeyList(localValue) === null) {
|
|
293
|
+
if (isPathDeselected(owner, localField)) {
|
|
294
|
+
throw new Error(
|
|
295
|
+
`cascadeDeletePlugin for model "${model}" cannot resolve dependents: localField "${localField}" was not selected on the document; load the document with that path projected instead of assuming an empty relation`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const relationship = buildRelationshipPredicate(foreignField, localValue);
|
|
301
|
+
if (!relationship) return null;
|
|
302
|
+
const extraFilter = resolveFilter(extraForeignFilter, document);
|
|
303
|
+
return combineWithRelationship(relationship, extraFilter);
|
|
304
|
+
}
|
|
305
|
+
return null;
|
|
306
|
+
};
|
|
307
|
+
var resolveTraversalLimits = (options) => ({
|
|
308
|
+
maxConcurrency: typeof options.maxConcurrency === "number" ? options.maxConcurrency : DEFAULT_MAX_CONCURRENCY,
|
|
309
|
+
batchSize: typeof options.batchSize === "number" ? options.batchSize : DEFAULT_BATCH_SIZE
|
|
310
|
+
});
|
|
311
|
+
var extractIdList = (rows) => rows.map((row) => {
|
|
312
|
+
if ((0, import_utils2.isPlainObject)(row)) return row._id;
|
|
313
|
+
if ((0, import_utils2.isObject)(row)) {
|
|
314
|
+
try {
|
|
315
|
+
const candidate = row._id;
|
|
316
|
+
if (candidate !== void 0) return candidate;
|
|
317
|
+
} catch {
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return row;
|
|
321
|
+
});
|
|
322
|
+
var fetchDependentIdPage = async (Target, filter, session, lastId, batchSize) => {
|
|
323
|
+
const paged = lastId === void 0 ? filter : { $and: [filter, { _id: { $gt: lastId } }] };
|
|
324
|
+
let query = Target.find(paged).select("_id").sort({ _id: 1 }).limit(batchSize).lean();
|
|
325
|
+
if (session) query = query.session(session);
|
|
326
|
+
const rows = await query;
|
|
327
|
+
return extractIdList(Array.isArray(rows) ? rows : []);
|
|
328
|
+
};
|
|
329
|
+
var loadHydratedDependent = async (Target, id, session) => {
|
|
330
|
+
const query = Target.findById(id);
|
|
331
|
+
const result = session ? await query.session(session) : await query;
|
|
332
|
+
return result ?? null;
|
|
333
|
+
};
|
|
334
|
+
var runWithConcurrencyLimit = async (items, limit, task) => {
|
|
335
|
+
if (items.length === 0) return;
|
|
336
|
+
const workerCount = Math.max(1, Math.min(limit, items.length));
|
|
337
|
+
let next = 0;
|
|
338
|
+
let failed = false;
|
|
339
|
+
let firstError = null;
|
|
340
|
+
const workers = Array.from({ length: workerCount }, () => {
|
|
341
|
+
const run = async () => {
|
|
342
|
+
while (true) {
|
|
343
|
+
if (failed) return;
|
|
344
|
+
const index = next;
|
|
345
|
+
next += 1;
|
|
346
|
+
if (index >= items.length) return;
|
|
347
|
+
try {
|
|
348
|
+
await task(items[index]);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
if (!failed) {
|
|
351
|
+
failed = true;
|
|
352
|
+
firstError = error;
|
|
353
|
+
}
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
return run();
|
|
359
|
+
});
|
|
360
|
+
try {
|
|
361
|
+
await Promise.all(workers);
|
|
362
|
+
} catch {
|
|
363
|
+
throw firstError;
|
|
364
|
+
}
|
|
365
|
+
};
|
|
93
366
|
function cascadeDeletePlugin(schema, options) {
|
|
367
|
+
assertPluginOptions(options);
|
|
94
368
|
const { model, localField, foreignField, foreignFilter, extraForeignFilter } = options ?? {};
|
|
369
|
+
const filterConfig = {
|
|
370
|
+
model,
|
|
371
|
+
localField,
|
|
372
|
+
foreignField,
|
|
373
|
+
foreignFilter,
|
|
374
|
+
extraForeignFilter
|
|
375
|
+
};
|
|
376
|
+
const traversalLimits = resolveTraversalLimits(options ?? {});
|
|
95
377
|
const findDependents = async function(queryOptions) {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
if (foreignFilter) {
|
|
100
|
-
query = resolveFilter(foreignFilter, document);
|
|
101
|
-
} else if (localField && foreignField) {
|
|
102
|
-
const localValue = this.get(localField);
|
|
103
|
-
const extraFilter = resolveFilter(extraForeignFilter, document) ?? {};
|
|
104
|
-
query = {
|
|
105
|
-
[foreignField]: Array.isArray(localValue) ? { $in: localValue } : localValue,
|
|
106
|
-
...extraFilter
|
|
107
|
-
};
|
|
108
|
-
}
|
|
378
|
+
const context = resolveExecutionContext(this, model, queryOptions?.session ?? null);
|
|
379
|
+
const { Target, session } = context;
|
|
380
|
+
const query = resolveDependentFilter(this, filterConfig);
|
|
109
381
|
if (!query || !(0, import_utils2.isPlainObject)(query)) {
|
|
110
382
|
return [];
|
|
111
383
|
}
|
|
112
|
-
|
|
384
|
+
const mergedOptions = session ? { ...queryOptions, session } : queryOptions;
|
|
385
|
+
return await applyQueryOptions(Target.find(query), mergedOptions);
|
|
113
386
|
};
|
|
114
387
|
const findOrphans = async function(queryOptions) {
|
|
115
388
|
if (!localField || !foreignField) return null;
|
|
116
|
-
const
|
|
389
|
+
const context = resolveExecutionContext(this, model, queryOptions?.session ?? null);
|
|
390
|
+
const { Target, session } = context;
|
|
117
391
|
const schemaValue = Target.schema.obj[foreignField];
|
|
118
392
|
if (!schemaValue) return null;
|
|
119
393
|
const isMyRef = isReference(schemaValue, this.modelName);
|
|
120
394
|
if (!isMyRef) return null;
|
|
121
|
-
const ids = await this.distinct("_id");
|
|
122
|
-
const extraFilter = resolveFilter(extraForeignFilter)
|
|
123
|
-
const
|
|
124
|
-
[foreignField]: { $not: { $in: ids } }
|
|
125
|
-
...extraFilter
|
|
395
|
+
const ids = session != null ? await this.distinct("_id", {}, { session }) : await this.distinct("_id");
|
|
396
|
+
const extraFilter = resolveFilter(extraForeignFilter);
|
|
397
|
+
const relationship = {
|
|
398
|
+
[foreignField]: { $not: { $in: ids } }
|
|
126
399
|
};
|
|
127
|
-
|
|
400
|
+
const query = combineWithRelationship(
|
|
401
|
+
relationship,
|
|
402
|
+
extraFilter
|
|
403
|
+
);
|
|
404
|
+
const mergedOptions = session ? { ...queryOptions, session } : queryOptions;
|
|
405
|
+
return await applyQueryOptions(Target.find(query), mergedOptions);
|
|
128
406
|
};
|
|
129
|
-
const deleteDependents = async function() {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
407
|
+
const deleteDependents = async function(operationOptions) {
|
|
408
|
+
const context = resolveExecutionContext(this, model, operationOptions?.session ?? null);
|
|
409
|
+
assertTransactionSupported(context);
|
|
410
|
+
const { Target, session } = context;
|
|
411
|
+
const filter = resolveDependentFilter(this, filterConfig);
|
|
412
|
+
if (!filter || !(0, import_utils2.isPlainObject)(filter)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const deleteSingle = async (document) => {
|
|
416
|
+
if (session != null && typeof document.$session === "function") {
|
|
417
|
+
try {
|
|
418
|
+
await document.$session?.(session);
|
|
419
|
+
} catch {
|
|
138
420
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
421
|
+
}
|
|
422
|
+
if (supportsDeleteOne(document)) {
|
|
423
|
+
return session != null ? document.deleteOne({ session }) : document.deleteOne();
|
|
424
|
+
}
|
|
425
|
+
if (supportsRemove(document)) {
|
|
426
|
+
return session != null ? document.remove({ session }) : document.remove();
|
|
427
|
+
}
|
|
428
|
+
return Promise.resolve();
|
|
429
|
+
};
|
|
430
|
+
const deleteById = async (id) => {
|
|
431
|
+
const hydrated = await loadHydratedDependent(Target, id, session);
|
|
432
|
+
if (!hydrated) return;
|
|
433
|
+
await deleteSingle(hydrated);
|
|
434
|
+
};
|
|
435
|
+
const effectiveConcurrency = session != null && isInTransaction(session) ? 1 : traversalLimits.maxConcurrency;
|
|
436
|
+
const seen = /* @__PURE__ */ new Set();
|
|
437
|
+
let lastId;
|
|
438
|
+
let firstPage = true;
|
|
439
|
+
for (; ; ) {
|
|
440
|
+
const ids = await fetchDependentIdPage(
|
|
441
|
+
Target,
|
|
442
|
+
filter,
|
|
443
|
+
session,
|
|
444
|
+
firstPage ? void 0 : lastId,
|
|
445
|
+
traversalLimits.batchSize
|
|
446
|
+
);
|
|
447
|
+
firstPage = false;
|
|
448
|
+
if (ids.length === 0) return;
|
|
449
|
+
const fresh = ids.filter((id) => {
|
|
450
|
+
const key = typeof id === "object" && id !== null ? String(id) : `${typeof id}:${String(id)}`;
|
|
451
|
+
if (seen.has(key)) return false;
|
|
452
|
+
seen.add(key);
|
|
453
|
+
return true;
|
|
454
|
+
});
|
|
455
|
+
await runWithConcurrencyLimit(fresh, effectiveConcurrency, deleteById);
|
|
456
|
+
lastId = ids[ids.length - 1];
|
|
457
|
+
if (ids.length < traversalLimits.batchSize) return;
|
|
458
|
+
}
|
|
142
459
|
};
|
|
460
|
+
schema.pre("deleteOne", { document: true, query: false }, async function preDeleteOne(...args) {
|
|
461
|
+
const explicitSession = args[1]?.session ?? args[0]?.session ?? null;
|
|
462
|
+
assertTransactionSupported(resolveExecutionContext(this, model, explicitSession));
|
|
463
|
+
});
|
|
143
464
|
schema.post("deleteOne", { document: true, query: false }, async function postDeleteOne() {
|
|
144
465
|
await deleteDependents.call(this);
|
|
145
466
|
});
|