@classytic/mongokit 3.39.0 → 3.40.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 +71 -0
- package/dist/{PaginationEngine-D-P19Y9m.d.mts → PaginationEngine-CRHyuUtw.d.mts} +17 -3
- package/dist/{PaginationEngine-DQ7beTCR.mjs → PaginationEngine-CfT2Cxkj.mjs} +320 -145
- package/dist/{Repository-D7fDpyFx.mjs → Repository-CYhSC9ut.mjs} +844 -794
- package/dist/{Repository-IxI983_m.d.mts → Repository-pnLw1rG5.d.mts} +113 -47
- package/dist/actions/index.d.mts +1 -1
- package/dist/actions/index.mjs +1 -1
- package/dist/adapter/index.mjs +1 -1
- package/dist/{adapter-BrVfPFSW.mjs → adapter-CkMoMF83.mjs} +1 -1
- package/dist/ai/index.d.mts +1 -1
- package/dist/better-auth/index.mjs +2 -2
- package/dist/{create-repository-CRXjDa3N.d.mts → create-repository-BUx9vfG9.d.mts} +5 -5
- package/dist/{create-repository-DOe9E_5F.mjs → create-repository-BYyWzLbt.mjs} +2 -2
- package/dist/idempotency/index.d.mts +23 -0
- package/dist/idempotency/index.mjs +134 -0
- package/dist/{index-CmjNyulu.d.mts → index-SYF8N28D.d.mts} +37 -19
- package/dist/index.d.mts +105 -39
- package/dist/index.mjs +332 -676
- package/dist/indexes/verify-indexes.d.mts +14 -1
- package/dist/indexes/verify-indexes.mjs +54 -7
- package/dist/join-scope-DSAznE8E.mjs +168 -0
- package/dist/{mongooseToJsonSchema-waUVQwK4.mjs → mongooseToJsonSchema-C3k4ide0.mjs} +5 -1
- package/dist/{multi-tenant.plugin-B-tzNeYL.d.mts → multi-tenant.plugin-a3UhQARj.d.mts} +19 -3
- package/dist/{operations-CrRIoc8Z.d.mts → operations-XiFgG5US.d.mts} +36 -13
- package/dist/outbox/index.d.mts +1 -1
- package/dist/outbox/index.mjs +6 -4
- package/dist/pagination/PaginationEngine.d.mts +2 -2
- package/dist/pagination/PaginationEngine.mjs +1 -1
- package/dist/pagination/utils/limits.d.mts +1 -1
- package/dist/{pagination-BpIv2bdS.d.mts → pagination-D6psses0.d.mts} +116 -13
- package/dist/{plugin-options-BuvtOSZf.d.mts → plugin-options-CRhIvSzx.d.mts} +16 -2
- package/dist/plugins/index.d.mts +8 -3
- package/dist/plugins/index.mjs +98 -25
- package/dist/query-defaults-BxUcSOoF.mjs +208 -0
- package/dist/read-model/index.d.mts +66 -0
- package/dist/read-model/index.mjs +193 -0
- package/dist/{repository-ZbLtL7Nk.d.mts → repository-DNHfjAZX.d.mts} +36 -4
- package/dist/sync/index.d.mts +53 -0
- package/dist/sync/index.mjs +371 -0
- package/dist/tenant/index.mjs +2 -0
- package/dist/testkit/index.d.mts +40 -3
- package/dist/testkit/index.mjs +113 -2
- package/dist/{timestamp.plugin-B5eiaItQ.mjs → timestamp.plugin-zfdLYZiO.mjs} +135 -32
- package/dist/{to-plain-B8ufk3Qz.d.mts → to-plain-CjgEInWV.d.mts} +2 -2
- package/dist/transaction-BrRYtH_b.mjs +655 -0
- package/dist/{update-c_01A0if.mjs → update-WgeAUJ_I.mjs} +212 -33
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.mjs +1 -1
- package/dist/{validation-chain.plugin-Bju1D1Sn.mjs → validation-chain.plugin-CHUHWMtk.mjs} +204 -279
- package/dist/{validation-chain.plugin-DvrLJIbJ.d.mts → validation-chain.plugin-CTcNJCmV.d.mts} +27 -5
- package/package.json +18 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
2
|
import { n as warn } from "./logger-K7dPmu2U.mjs";
|
|
3
3
|
import { t as createError } from "./error-BuByZwKU.mjs";
|
|
4
|
+
import { i as applyToQuery } from "./query-defaults-BxUcSOoF.mjs";
|
|
4
5
|
import { coerceFilterDates, isFilter } from "@classytic/repo-core/filter";
|
|
5
6
|
//#region src/actions/create.ts
|
|
6
7
|
var create_exports = /* @__PURE__ */ __exportAll({
|
|
@@ -15,7 +16,16 @@ var create_exports = /* @__PURE__ */ __exportAll({
|
|
|
15
16
|
*/
|
|
16
17
|
async function create(Model, data, options = {}) {
|
|
17
18
|
const document = new Model(data);
|
|
18
|
-
|
|
19
|
+
const saveOptions = { session: options.session };
|
|
20
|
+
const bound = options.queryOptions?.maxTimeMS;
|
|
21
|
+
if (bound !== void 0) Object.assign(saveOptions, { timeoutMS: bound });
|
|
22
|
+
const wc = options.queryOptions?.writeConcern;
|
|
23
|
+
if (wc) Object.assign(saveOptions, { writeConcern: {
|
|
24
|
+
...wc.w !== void 0 ? { w: wc.w } : {},
|
|
25
|
+
...wc.j !== void 0 ? { j: wc.j } : {},
|
|
26
|
+
...wc.wtimeoutMS !== void 0 ? { wtimeout: wc.wtimeoutMS } : {}
|
|
27
|
+
} });
|
|
28
|
+
await document.save(saveOptions);
|
|
19
29
|
return document;
|
|
20
30
|
}
|
|
21
31
|
/** Narrow a caught error to one carrying a partial-write report. */
|
|
@@ -54,10 +64,15 @@ function readFailures(err, dataArray) {
|
|
|
54
64
|
*/
|
|
55
65
|
async function createMany(Model, dataArray, options = {}) {
|
|
56
66
|
try {
|
|
57
|
-
|
|
67
|
+
const insertOptions = {
|
|
58
68
|
session: options.session,
|
|
59
69
|
ordered: options.ordered === true
|
|
60
|
-
}
|
|
70
|
+
};
|
|
71
|
+
const wc = options.queryOptions?.writeConcern;
|
|
72
|
+
if (wc) Object.assign(insertOptions, { writeConcern: wc });
|
|
73
|
+
const bound = options.queryOptions?.maxTimeMS;
|
|
74
|
+
if (bound !== void 0) Object.assign(insertOptions, { timeoutMS: bound });
|
|
75
|
+
return await Model.insertMany(dataArray, insertOptions);
|
|
61
76
|
} catch (err) {
|
|
62
77
|
const inserted = err.insertedDocs;
|
|
63
78
|
const failed = readFailures(err, dataArray);
|
|
@@ -95,6 +110,122 @@ async function upsert(Model, query, data, options = {}) {
|
|
|
95
110
|
});
|
|
96
111
|
}
|
|
97
112
|
//#endregion
|
|
113
|
+
//#region src/filter/cast-schema.ts
|
|
114
|
+
/** Operators whose operand is a single scalar to cast. */
|
|
115
|
+
const SCALAR_OPS = /* @__PURE__ */ new Set([
|
|
116
|
+
"$eq",
|
|
117
|
+
"$ne",
|
|
118
|
+
"$gt",
|
|
119
|
+
"$gte",
|
|
120
|
+
"$lt",
|
|
121
|
+
"$lte"
|
|
122
|
+
]);
|
|
123
|
+
/** Operators whose operand is an array of scalars to cast element-wise. */
|
|
124
|
+
const ARRAY_OPS = /* @__PURE__ */ new Set(["$in", "$nin"]);
|
|
125
|
+
/** Logical operators whose operand is an ARRAY of sub-filters. */
|
|
126
|
+
const LOGICAL_ARRAY_OPS$1 = /* @__PURE__ */ new Set([
|
|
127
|
+
"$and",
|
|
128
|
+
"$or",
|
|
129
|
+
"$nor"
|
|
130
|
+
]);
|
|
131
|
+
/** Logical operators whose operand is a SINGLE nested sub-filter. */
|
|
132
|
+
const LOGICAL_OBJECT_OPS = /* @__PURE__ */ new Set(["$not"]);
|
|
133
|
+
function isPlainObject(value) {
|
|
134
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Cast one value through a schema path. Returns the original value when the
|
|
138
|
+
* path is unknown, the value is nullish, or the cast throws — never raises.
|
|
139
|
+
*/
|
|
140
|
+
function castValue(schema, field, value) {
|
|
141
|
+
if (value === null || value === void 0) return value;
|
|
142
|
+
if (typeof value === "object" && !(value instanceof Date)) return value;
|
|
143
|
+
let schemaType;
|
|
144
|
+
try {
|
|
145
|
+
schemaType = schema.path(field);
|
|
146
|
+
} catch {
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
if (!schemaType || typeof schemaType.cast !== "function") return value;
|
|
150
|
+
try {
|
|
151
|
+
return schemaType.cast(value);
|
|
152
|
+
} catch {
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Cast the operand(s) of a field-level operator object, leaving operators we
|
|
158
|
+
* don't understand (`$regex`, `$exists`, `$size`, `$elemMatch`, …) alone.
|
|
159
|
+
*/
|
|
160
|
+
function castOperatorObject(schema, field, ops) {
|
|
161
|
+
let changed = false;
|
|
162
|
+
const out = {};
|
|
163
|
+
for (const [op, operand] of Object.entries(ops)) if (SCALAR_OPS.has(op)) {
|
|
164
|
+
const next = castValue(schema, field, operand);
|
|
165
|
+
out[op] = next;
|
|
166
|
+
if (next !== operand) changed = true;
|
|
167
|
+
} else if (ARRAY_OPS.has(op) && Array.isArray(operand)) {
|
|
168
|
+
const arr = operand.map((entry) => castValue(schema, field, entry));
|
|
169
|
+
out[op] = arr;
|
|
170
|
+
if (arr.some((v, i) => v !== operand[i])) changed = true;
|
|
171
|
+
} else out[op] = operand;
|
|
172
|
+
return changed ? out : ops;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Cast a record-shape Mongo filter against a Mongoose schema so it is safe to
|
|
176
|
+
* hand to a `$match` stage. Recurses `$and` / `$or` / `$nor` / `$not`, since a
|
|
177
|
+
* policy/tenant scope conjoined with a caller filter nests one level down.
|
|
178
|
+
*
|
|
179
|
+
* Filter IR nodes and non-objects pass through untouched — IR values are built
|
|
180
|
+
* in code (already correctly typed) and carry no schema paths to resolve.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* // organizationId is an ObjectId column; arc supplies the scope id as a string
|
|
185
|
+
* castFilterToSchema({ organizationId: '6a6a…' }, Order.schema)
|
|
186
|
+
* // → { organizationId: ObjectId('6a6a…') }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
function castFilterToSchema(filter, schema) {
|
|
190
|
+
if (!isPlainObject(filter)) return filter;
|
|
191
|
+
if (typeof filter.op === "string") return filter;
|
|
192
|
+
let changed = false;
|
|
193
|
+
const out = {};
|
|
194
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
195
|
+
if (LOGICAL_ARRAY_OPS$1.has(key) && Array.isArray(value)) {
|
|
196
|
+
const arr = value.map((entry) => castFilterToSchema(entry, schema));
|
|
197
|
+
out[key] = arr;
|
|
198
|
+
if (arr.some((v, i) => v !== value[i])) changed = true;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (LOGICAL_OBJECT_OPS.has(key) && isPlainObject(value)) {
|
|
202
|
+
const nested = castFilterToSchema(value, schema);
|
|
203
|
+
out[key] = nested;
|
|
204
|
+
if (nested !== value) changed = true;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (key.startsWith("$")) {
|
|
208
|
+
out[key] = value;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (isPlainObject(value)) {
|
|
212
|
+
const keys = Object.keys(value);
|
|
213
|
+
if (keys.length > 0 && keys.every((k) => k.startsWith("$"))) {
|
|
214
|
+
const nextOps = castOperatorObject(schema, key, value);
|
|
215
|
+
out[key] = nextOps;
|
|
216
|
+
if (nextOps !== value) changed = true;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
out[key] = value;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const next = castValue(schema, key, value);
|
|
223
|
+
out[key] = next;
|
|
224
|
+
if (next !== value) changed = true;
|
|
225
|
+
}
|
|
226
|
+
return changed ? out : filter;
|
|
227
|
+
}
|
|
228
|
+
//#endregion
|
|
98
229
|
//#region src/filter/compile.ts
|
|
99
230
|
/**
|
|
100
231
|
* Compile a Filter IR node to a MongoDB query object. Returns `{}`
|
|
@@ -114,6 +245,12 @@ async function upsert(Model, query, data, options = {}) {
|
|
|
114
245
|
* handles them"). This set covers every operator the arc aggregation
|
|
115
246
|
* guard accepts in `parseDateRange` and `hasFilterOnField`.
|
|
116
247
|
*/
|
|
248
|
+
/**
|
|
249
|
+
* The query-shorthand vocabulary, shared with `compileFilterToMongoExpr` so
|
|
250
|
+
* both filter surfaces recognise the same operator names. Exported because a
|
|
251
|
+
* second private copy is how the two compilers drifted apart in the first
|
|
252
|
+
* place: one accepted `{ f: { eq: 'x' } }` and the other read it as a literal.
|
|
253
|
+
*/
|
|
117
254
|
const SHORTHAND_OPS = /* @__PURE__ */ new Set([
|
|
118
255
|
"eq",
|
|
119
256
|
"ne",
|
|
@@ -191,7 +328,10 @@ function schemaDateOracle(schema) {
|
|
|
191
328
|
}
|
|
192
329
|
function compileFilterToMongo(input, schema) {
|
|
193
330
|
if (!input) return {};
|
|
194
|
-
if (!isFilter(input))
|
|
331
|
+
if (!isFilter(input)) {
|
|
332
|
+
const expanded = expandShorthands(input);
|
|
333
|
+
return coerceFilterDates(schema ? castFilterToSchema(expanded, schema) : expanded, schema ? { isDateField: schemaDateOracle(schema) } : void 0);
|
|
334
|
+
}
|
|
195
335
|
return compile(input);
|
|
196
336
|
}
|
|
197
337
|
function compile(filter) {
|
|
@@ -199,7 +339,7 @@ function compile(filter) {
|
|
|
199
339
|
case "true": return {};
|
|
200
340
|
case "false": return { $expr: false };
|
|
201
341
|
case "eq": return { [filter.field]: filter.value };
|
|
202
|
-
case "ne": return filter.value === null ? { [filter.field]: { $ne: null } } : {
|
|
342
|
+
case "ne": return filter.value === null ? { [filter.field]: { $ne: null } } : { [filter.field]: { $nin: [filter.value, null] } };
|
|
203
343
|
case "gt": return { [filter.field]: { $gt: filter.value } };
|
|
204
344
|
case "gte": return { [filter.field]: { $gte: filter.value } };
|
|
205
345
|
case "lt": return { [filter.field]: { $lt: filter.value } };
|
|
@@ -209,7 +349,7 @@ function compile(filter) {
|
|
|
209
349
|
return { [filter.field]: { $in: [...filter.values] } };
|
|
210
350
|
case "nin":
|
|
211
351
|
if (filter.values.length === 0) return {};
|
|
212
|
-
return { [filter.field]: { $nin: [...filter.values] } };
|
|
352
|
+
return { [filter.field]: { $nin: filter.values.includes(null) ? [...filter.values] : [...filter.values, null] } };
|
|
213
353
|
case "exists": return filter.exists ? { [filter.field]: { $ne: null } } : { [filter.field]: null };
|
|
214
354
|
case "like": return { [filter.field]: likeToRegex(filter.pattern, filter.caseSensitivity) };
|
|
215
355
|
/**
|
|
@@ -233,10 +373,10 @@ function compile(filter) {
|
|
|
233
373
|
$options: filter.flags
|
|
234
374
|
} : { $regex: filter.pattern } };
|
|
235
375
|
case "and": {
|
|
236
|
-
|
|
237
|
-
|
|
376
|
+
const parts = filter.children.map(compile).filter((part) => Object.keys(part).length > 0);
|
|
377
|
+
if (parts.length === 0) return {};
|
|
238
378
|
if (parts.length === 1) return parts[0];
|
|
239
|
-
return { $and: parts };
|
|
379
|
+
return mergeConjunction(parts) ?? { $and: parts };
|
|
240
380
|
}
|
|
241
381
|
case "or": {
|
|
242
382
|
if (filter.children.length === 0) return { $expr: false };
|
|
@@ -268,7 +408,7 @@ function compile(filter) {
|
|
|
268
408
|
* same way sqlitekit's `LIKE` semantically does.
|
|
269
409
|
*/
|
|
270
410
|
function likeToRegex(pattern, caseSensitivity) {
|
|
271
|
-
let out = "
|
|
411
|
+
let out = "";
|
|
272
412
|
for (let i = 0; i < pattern.length; i++) {
|
|
273
413
|
const ch = pattern[i];
|
|
274
414
|
if (ch === "\\" && i + 1 < pattern.length) {
|
|
@@ -283,12 +423,44 @@ function likeToRegex(pattern, caseSensitivity) {
|
|
|
283
423
|
else if (ch === "_") out += ".";
|
|
284
424
|
else out += escapeRegexChar(ch);
|
|
285
425
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
426
|
+
const source = `${out.startsWith(".*") ? out.slice(2) : `^${out}`}`;
|
|
427
|
+
const anchored = source.endsWith(".*") && !source.endsWith("\\.*") ? source.slice(0, -2) : `${source}$`;
|
|
428
|
+
return caseSensitivity === "sensitive" ? { $regex: anchored } : {
|
|
429
|
+
$regex: anchored,
|
|
289
430
|
$options: "i"
|
|
290
431
|
};
|
|
291
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* Merge an AND of field conditions into one object (`{ price: { $gte, $lte } }`) when no field
|
|
435
|
+
* repeats an operator; `undefined` when a merge would change meaning, so the caller keeps `$and`.
|
|
436
|
+
*/
|
|
437
|
+
function mergeConjunction(parts) {
|
|
438
|
+
const merged = {};
|
|
439
|
+
for (const part of parts) for (const [field, condition] of Object.entries(part)) {
|
|
440
|
+
if (field.startsWith("$")) return void 0;
|
|
441
|
+
if (!(field in merged)) {
|
|
442
|
+
merged[field] = condition;
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
const left = asOperators(merged[field]);
|
|
446
|
+
const right = asOperators(condition);
|
|
447
|
+
if (!left || !right || Object.keys(right).some((op) => op in left)) return void 0;
|
|
448
|
+
merged[field] = {
|
|
449
|
+
...left,
|
|
450
|
+
...right
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
return merged;
|
|
454
|
+
}
|
|
455
|
+
/** A field condition as an operator object — an equality value becomes `{ $eq }`. */
|
|
456
|
+
function asOperators(condition) {
|
|
457
|
+
if (condition instanceof RegExp || Array.isArray(condition)) return void 0;
|
|
458
|
+
if (condition !== null && typeof condition === "object" && !(condition instanceof Date)) {
|
|
459
|
+
const keys = Object.keys(condition);
|
|
460
|
+
return keys.length > 0 && keys.every((k) => k.startsWith("$")) ? condition : void 0;
|
|
461
|
+
}
|
|
462
|
+
return condition === null ? void 0 : { $eq: condition };
|
|
463
|
+
}
|
|
292
464
|
function escapeRegexChar(ch) {
|
|
293
465
|
return /[.*+?^${}()|[\]\\]/.test(ch) ? `\\${ch}` : ch;
|
|
294
466
|
}
|
|
@@ -808,7 +980,7 @@ async function facet(Model, facets, options = {}) {
|
|
|
808
980
|
*/
|
|
809
981
|
async function distinct(Model, field, query = {}, options = {}) {
|
|
810
982
|
const q = Model.distinct(field, query).session(options.session ?? null);
|
|
811
|
-
|
|
983
|
+
applyToQuery(q, options.queryOptions ?? {});
|
|
812
984
|
return q;
|
|
813
985
|
}
|
|
814
986
|
/**
|
|
@@ -869,7 +1041,9 @@ async function deleteById(Model, id, options = {}) {
|
|
|
869
1041
|
...options.query,
|
|
870
1042
|
_id: id
|
|
871
1043
|
};
|
|
872
|
-
|
|
1044
|
+
const q = Model.findOneAndDelete(query).session(options.session ?? null);
|
|
1045
|
+
applyToQuery(q, options.queryOptions ?? {});
|
|
1046
|
+
if (!await q) return null;
|
|
873
1047
|
return {
|
|
874
1048
|
message: "Deleted successfully",
|
|
875
1049
|
id: String(id)
|
|
@@ -890,7 +1064,9 @@ async function deleteMany(Model, query, options = {}) {
|
|
|
890
1064
|
* Delete by query. Returns `null` on miss unless `throwOnNotFound: true`.
|
|
891
1065
|
*/
|
|
892
1066
|
async function deleteByQuery(Model, query, options = {}) {
|
|
893
|
-
const
|
|
1067
|
+
const q = Model.findOneAndDelete(query).session(options.session ?? null);
|
|
1068
|
+
applyToQuery(q, options.queryOptions ?? {});
|
|
1069
|
+
const document = await q;
|
|
894
1070
|
if (!document) {
|
|
895
1071
|
if (options.throwOnNotFound === true) throw createError(404, "Document not found");
|
|
896
1072
|
return null;
|
|
@@ -975,7 +1151,7 @@ async function getById(Model, id, options = {}) {
|
|
|
975
1151
|
if (options.populate) query.populate(parsePopulate$1(options.populate));
|
|
976
1152
|
if (options.lean) query.lean();
|
|
977
1153
|
if (options.session) query.session(options.session);
|
|
978
|
-
|
|
1154
|
+
applyToQuery(query, options.queryOptions ?? {});
|
|
979
1155
|
const document = await query.exec();
|
|
980
1156
|
if (!document && options.throwOnNotFound === true) throw createError(404, "Document not found");
|
|
981
1157
|
return document;
|
|
@@ -996,7 +1172,7 @@ async function getByQuery(Model, query, options = {}) {
|
|
|
996
1172
|
if (options.sort) mongoQuery.sort(options.sort);
|
|
997
1173
|
if (options.lean) mongoQuery.lean();
|
|
998
1174
|
if (options.session) mongoQuery.session(options.session);
|
|
999
|
-
|
|
1175
|
+
applyToQuery(mongoQuery, options.queryOptions ?? {});
|
|
1000
1176
|
const document = await mongoQuery.exec();
|
|
1001
1177
|
if (!document && options.throwOnNotFound === true) throw createError(404, "Document not found");
|
|
1002
1178
|
return document;
|
|
@@ -1023,7 +1199,7 @@ async function getAll(Model, query = {}, options = {}) {
|
|
|
1023
1199
|
if (options.skip) mongoQuery = mongoQuery.skip(options.skip);
|
|
1024
1200
|
if (options.lean !== false) mongoQuery = mongoQuery.lean();
|
|
1025
1201
|
if (options.session) mongoQuery = mongoQuery.session(options.session);
|
|
1026
|
-
|
|
1202
|
+
applyToQuery(mongoQuery, options.queryOptions ?? {});
|
|
1027
1203
|
return mongoQuery.exec();
|
|
1028
1204
|
}
|
|
1029
1205
|
/**
|
|
@@ -1064,7 +1240,7 @@ async function getOrCreate(Model, query, createData, options = {}) {
|
|
|
1064
1240
|
*/
|
|
1065
1241
|
async function count(Model, query = {}, options = {}) {
|
|
1066
1242
|
const q = Model.countDocuments(query).session(options.session ?? null);
|
|
1067
|
-
|
|
1243
|
+
applyToQuery(q, options.queryOptions ?? {});
|
|
1068
1244
|
return q;
|
|
1069
1245
|
}
|
|
1070
1246
|
/**
|
|
@@ -1072,7 +1248,7 @@ async function count(Model, query = {}, options = {}) {
|
|
|
1072
1248
|
*/
|
|
1073
1249
|
async function exists(Model, query, options = {}) {
|
|
1074
1250
|
const q = Model.exists(query).session(options.session ?? null);
|
|
1075
|
-
|
|
1251
|
+
applyToQuery(q, options.queryOptions ?? {});
|
|
1076
1252
|
return q;
|
|
1077
1253
|
}
|
|
1078
1254
|
//#endregion
|
|
@@ -1088,6 +1264,10 @@ var update_exports = /* @__PURE__ */ __exportAll({
|
|
|
1088
1264
|
updateWithConstraints: () => updateWithConstraints,
|
|
1089
1265
|
updateWithValidation: () => updateWithValidation
|
|
1090
1266
|
});
|
|
1267
|
+
function withQueryOptions(query, queryOptions) {
|
|
1268
|
+
applyToQuery(query, queryOptions ?? {});
|
|
1269
|
+
return query;
|
|
1270
|
+
}
|
|
1091
1271
|
function assertUpdatePipelineAllowed(update, updatePipeline) {
|
|
1092
1272
|
if (Array.isArray(update) && updatePipeline !== true) throw createError(400, "Update pipelines (array updates) are disabled by default; pass `{ updatePipeline: true }` to explicitly allow pipeline-style updates.");
|
|
1093
1273
|
}
|
|
@@ -1109,13 +1289,13 @@ async function update(Model, id, data, options = {}) {
|
|
|
1109
1289
|
...options.query,
|
|
1110
1290
|
_id: id
|
|
1111
1291
|
};
|
|
1112
|
-
return await Model.findOneAndUpdate(query, data, {
|
|
1292
|
+
return await withQueryOptions(Model.findOneAndUpdate(query, data, {
|
|
1113
1293
|
returnDocument: "after",
|
|
1114
1294
|
runValidators: true,
|
|
1115
1295
|
session: options.session,
|
|
1116
1296
|
...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {},
|
|
1117
1297
|
...options.arrayFilters ? { arrayFilters: options.arrayFilters } : {}
|
|
1118
|
-
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false) ?? null;
|
|
1298
|
+
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false), options.queryOptions) ?? null;
|
|
1119
1299
|
}
|
|
1120
1300
|
/**
|
|
1121
1301
|
* Update with query constraints (optimized)
|
|
@@ -1127,13 +1307,13 @@ async function updateWithConstraints(Model, id, data, constraints = {}, options
|
|
|
1127
1307
|
...constraints,
|
|
1128
1308
|
_id: id
|
|
1129
1309
|
};
|
|
1130
|
-
return await Model.findOneAndUpdate(query, data, {
|
|
1310
|
+
return await withQueryOptions(Model.findOneAndUpdate(query, data, {
|
|
1131
1311
|
returnDocument: "after",
|
|
1132
1312
|
runValidators: true,
|
|
1133
1313
|
session: options.session,
|
|
1134
1314
|
...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {},
|
|
1135
1315
|
...options.arrayFilters ? { arrayFilters: options.arrayFilters } : {}
|
|
1136
|
-
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false);
|
|
1316
|
+
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false), options.queryOptions);
|
|
1137
1317
|
}
|
|
1138
1318
|
/**
|
|
1139
1319
|
* Update with validation (smart optimization)
|
|
@@ -1190,12 +1370,12 @@ async function updateWithValidation(Model, id, data, validationOptions = {}, opt
|
|
|
1190
1370
|
*/
|
|
1191
1371
|
async function updateMany(Model, query, data, options = {}) {
|
|
1192
1372
|
assertUpdatePipelineAllowed(data, options.updatePipeline);
|
|
1193
|
-
const result = await Model.updateMany(query, data, {
|
|
1373
|
+
const result = await withQueryOptions(Model.updateMany(query, data, {
|
|
1194
1374
|
runValidators: true,
|
|
1195
1375
|
session: options.session,
|
|
1196
1376
|
...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {},
|
|
1197
1377
|
...options.arrayFilters ? { arrayFilters: options.arrayFilters } : {}
|
|
1198
|
-
});
|
|
1378
|
+
}), options.queryOptions);
|
|
1199
1379
|
return {
|
|
1200
1380
|
matchedCount: result.matchedCount,
|
|
1201
1381
|
modifiedCount: result.modifiedCount
|
|
@@ -1206,13 +1386,13 @@ async function updateMany(Model, query, data, options = {}) {
|
|
|
1206
1386
|
*/
|
|
1207
1387
|
async function updateByQuery(Model, query, data, options = {}) {
|
|
1208
1388
|
assertUpdatePipelineAllowed(data, options.updatePipeline);
|
|
1209
|
-
const document = await Model.findOneAndUpdate(query, data, {
|
|
1389
|
+
const document = await withQueryOptions(Model.findOneAndUpdate(query, data, {
|
|
1210
1390
|
returnDocument: "after",
|
|
1211
1391
|
runValidators: true,
|
|
1212
1392
|
session: options.session,
|
|
1213
1393
|
...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {},
|
|
1214
1394
|
...options.arrayFilters ? { arrayFilters: options.arrayFilters } : {}
|
|
1215
|
-
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false);
|
|
1395
|
+
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? false), options.queryOptions);
|
|
1216
1396
|
if (!document && options.throwOnNotFound === true) throw createError(404, "Document not found");
|
|
1217
1397
|
return document;
|
|
1218
1398
|
}
|
|
@@ -1226,7 +1406,7 @@ async function updateByQuery(Model, query, data, options = {}) {
|
|
|
1226
1406
|
async function findOneAndUpdate(Model, filter, update, options = {}) {
|
|
1227
1407
|
assertUpdatePipelineAllowed(update, options.updatePipeline);
|
|
1228
1408
|
const returnDocument = options.returnDocument ?? "after";
|
|
1229
|
-
return await Model.findOneAndUpdate(filter, update, {
|
|
1409
|
+
return await withQueryOptions(Model.findOneAndUpdate(filter, update, {
|
|
1230
1410
|
returnDocument,
|
|
1231
1411
|
upsert: options.upsert ?? false,
|
|
1232
1412
|
runValidators: options.runValidators ?? true,
|
|
@@ -1234,9 +1414,8 @@ async function findOneAndUpdate(Model, filter, update, options = {}) {
|
|
|
1234
1414
|
...options.sort ? { sort: options.sort } : {},
|
|
1235
1415
|
...options.arrayFilters ? { arrayFilters: options.arrayFilters } : {},
|
|
1236
1416
|
...options.collation ? { collation: options.collation } : {},
|
|
1237
|
-
...options.maxTimeMS ? { maxTimeMS: options.maxTimeMS } : {},
|
|
1238
1417
|
...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {}
|
|
1239
|
-
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? true);
|
|
1418
|
+
}).select(options.select || "").populate(parsePopulate(options.populate)).lean(options.lean ?? true), options.queryOptions);
|
|
1240
1419
|
}
|
|
1241
1420
|
/**
|
|
1242
1421
|
* Increment field
|
|
@@ -1257,4 +1436,4 @@ async function pullFromArray(Model, id, field, value, options = {}) {
|
|
|
1257
1436
|
return update(Model, id, { $pull: { [field]: value } }, options);
|
|
1258
1437
|
}
|
|
1259
1438
|
//#endregion
|
|
1260
|
-
export {
|
|
1439
|
+
export { isCreateManyPartialError as C, create_exports as S, SHORTHAND_OPS as _, count as a, create as b, getByQuery as c, deleteById as d, deleteByQuery as f, LookupBuilder as g, distinct as h, update_exports as i, getOrCreate as l, aggregate_exports as m, update as n, exists as o, delete_exports as p, updateByQuery as r, getById as s, findOneAndUpdate as t, read_exports as u, compileFilterToMongo as v, upsert as w, createMany as x, castFilterToSchema as y };
|
package/dist/utils/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as getMongooseProjection, a as createMemoryCache, b as createError, c as IdType, d as isObjectIdInstance, f as isValidIdForType, g as getFieldsForUser, h as filterResponseData, i as buildCrudSchemasFromMongooseSchema, l as getSchemaIdType, m as createFieldPreset, n as CrudSchemasFramework, o as LoggerConfig, p as toObjectId, r as buildCrudSchemasFromModel, s as configureLogger, t as toPlain, v as CreateErrorOptions, y as ParseDuplicateKeyOptions } from "../to-plain-
|
|
1
|
+
import { _ as getMongooseProjection, a as createMemoryCache, b as createError, c as IdType, d as isObjectIdInstance, f as isValidIdForType, g as getFieldsForUser, h as filterResponseData, i as buildCrudSchemasFromMongooseSchema, l as getSchemaIdType, m as createFieldPreset, n as CrudSchemasFramework, o as LoggerConfig, p as toObjectId, r as buildCrudSchemasFromModel, s as configureLogger, t as toPlain, v as CreateErrorOptions, y as ParseDuplicateKeyOptions } from "../to-plain-CjgEInWV.mjs";
|
|
2
2
|
import { ClientSession, Model } from "mongoose";
|
|
3
3
|
//#region src/utils/id-chunks.d.ts
|
|
4
4
|
/**
|
package/dist/utils/index.mjs
CHANGED
|
@@ -4,5 +4,5 @@ import { t as configureLogger } from "../logger-K7dPmu2U.mjs";
|
|
|
4
4
|
import { t as createError } from "../error-BuByZwKU.mjs";
|
|
5
5
|
import { i as getMongooseProjection, n as filterResponseData, r as getFieldsForUser, t as createFieldPreset } from "../field-selection-FSQFLpsU.mjs";
|
|
6
6
|
import { n as createMemoryCache, t as toPlain } from "../to-plain-vksRrLtc.mjs";
|
|
7
|
-
import { n as buildCrudSchemasFromMongooseSchema, t as buildCrudSchemasFromModel } from "../mongooseToJsonSchema-
|
|
7
|
+
import { n as buildCrudSchemasFromMongooseSchema, t as buildCrudSchemasFromModel } from "../mongooseToJsonSchema-C3k4ide0.mjs";
|
|
8
8
|
export { DEFAULT_ID_CHUNK, buildCrudSchemasFromModel, buildCrudSchemasFromMongooseSchema, collectIds, configureLogger, createError, createFieldPreset, createMemoryCache, filterResponseData, getFieldsForUser, getMongooseProjection, getSchemaIdType, idChunks, isObjectIdInstance, isValidIdForType, keysetFilter, narrowToIds, selectKeysetChunk, toObjectId, toPlain };
|