@zodmon/core 0.4.0 → 0.6.0
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 +167 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +406 -19
- package/dist/index.d.ts +406 -19
- package/dist/index.js +165 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ObjectId, Collection, MongoClientOptions } from 'mongodb';
|
|
1
|
+
import { ObjectId, FindCursor, Collection, MongoClientOptions } from 'mongodb';
|
|
2
2
|
import { ZodPipe, ZodCustom, ZodTransform, z, ZodDefault } from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -290,6 +290,124 @@ type CollectionDefinition<TShape extends z.core.$ZodShape = z.core.$ZodShape> =
|
|
|
290
290
|
/** Erased collection type for use in generic contexts. */
|
|
291
291
|
type AnyCollection = CollectionDefinition<z.core.$ZodShape>;
|
|
292
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Type-safe sort specification for a document type.
|
|
295
|
+
*
|
|
296
|
+
* Constrains sort keys to top-level fields of `T` with direction `1` (ascending)
|
|
297
|
+
* or `-1` (descending). Dot-path sorts deferred to v1.0.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* const sort: TypedSort<User> = { name: 1, createdAt: -1 }
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
type TypedSort<T> = Partial<Record<keyof T & string, 1 | -1>>;
|
|
305
|
+
/**
|
|
306
|
+
* Type-safe cursor wrapping MongoDB's `FindCursor`.
|
|
307
|
+
*
|
|
308
|
+
* Provides chainable query modifiers (`sort`, `skip`, `limit`) that return
|
|
309
|
+
* `this` for fluent chaining, and terminal methods (`toArray`,
|
|
310
|
+
* `[Symbol.asyncIterator]`) that validate each document against the
|
|
311
|
+
* collection's Zod schema before returning.
|
|
312
|
+
*
|
|
313
|
+
* Created by {@link find} — do not construct directly.
|
|
314
|
+
*
|
|
315
|
+
* @typeParam TDef - The collection definition type, used to infer the document type.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* const docs = await find(users, { role: 'admin' })
|
|
320
|
+
* .sort({ name: 1 })
|
|
321
|
+
* .limit(10)
|
|
322
|
+
* .toArray()
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
declare class TypedFindCursor<TDef extends AnyCollection> {
|
|
326
|
+
/** @internal */
|
|
327
|
+
private cursor;
|
|
328
|
+
/** @internal */
|
|
329
|
+
private schema;
|
|
330
|
+
/** @internal */
|
|
331
|
+
private collectionName;
|
|
332
|
+
/** @internal */
|
|
333
|
+
private mode;
|
|
334
|
+
/** @internal */
|
|
335
|
+
constructor(cursor: FindCursor<InferDocument<TDef>>, definition: TDef, mode: ValidationMode | false);
|
|
336
|
+
/**
|
|
337
|
+
* Set the sort order for the query.
|
|
338
|
+
*
|
|
339
|
+
* Only top-level document fields are accepted as sort keys.
|
|
340
|
+
* Values must be `1` (ascending) or `-1` (descending).
|
|
341
|
+
*
|
|
342
|
+
* @param spec - Sort specification mapping field names to sort direction.
|
|
343
|
+
* @returns `this` for chaining.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* find(users, {}).sort({ name: 1, age: -1 }).toArray()
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
sort(spec: TypedSort<InferDocument<TDef>>): this;
|
|
351
|
+
/**
|
|
352
|
+
* Skip the first `n` documents in the result set.
|
|
353
|
+
*
|
|
354
|
+
* @param n - Number of documents to skip.
|
|
355
|
+
* @returns `this` for chaining.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* find(users, {}).skip(10).limit(10).toArray() // page 2
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
skip(n: number): this;
|
|
363
|
+
/**
|
|
364
|
+
* Limit the number of documents returned.
|
|
365
|
+
*
|
|
366
|
+
* @param n - Maximum number of documents to return.
|
|
367
|
+
* @returns `this` for chaining.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* find(users, {}).limit(10).toArray() // at most 10 docs
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
limit(n: number): this;
|
|
375
|
+
/**
|
|
376
|
+
* Execute the query and return all matching documents as an array.
|
|
377
|
+
*
|
|
378
|
+
* Each document is validated against the collection's Zod schema
|
|
379
|
+
* according to the resolved validation mode.
|
|
380
|
+
*
|
|
381
|
+
* @returns Array of validated documents.
|
|
382
|
+
* @throws {ZodmonValidationError} When a document fails schema validation in strict/strip mode.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* const admins = await find(users, { role: 'admin' }).toArray()
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
toArray(): Promise<InferDocument<TDef>[]>;
|
|
390
|
+
/**
|
|
391
|
+
* Async iterator for streaming documents one at a time.
|
|
392
|
+
*
|
|
393
|
+
* Each yielded document is validated against the collection's Zod schema.
|
|
394
|
+
* Memory-efficient for large result sets.
|
|
395
|
+
*
|
|
396
|
+
* @yields Validated documents one at a time.
|
|
397
|
+
* @throws {ZodmonValidationError} When a document fails schema validation.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* for await (const user of find(users, {})) {
|
|
402
|
+
* console.log(user.name)
|
|
403
|
+
* }
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
[Symbol.asyncIterator](): AsyncGenerator<InferDocument<TDef>>;
|
|
407
|
+
/** @internal Validate a single raw document against the schema. */
|
|
408
|
+
private validateDoc;
|
|
409
|
+
}
|
|
410
|
+
|
|
293
411
|
/**
|
|
294
412
|
* Comparison operators for a field value of type `V`.
|
|
295
413
|
*
|
|
@@ -388,12 +506,11 @@ type DotPathType<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K
|
|
|
388
506
|
* // Builder functions mixed with object literals
|
|
389
507
|
* const filter: TypedFilter<User> = { age: $gte(18), role: $in(['admin', 'mod']) }
|
|
390
508
|
*
|
|
391
|
-
* // Logical composition
|
|
392
|
-
*
|
|
393
|
-
* $or
|
|
394
|
-
* {
|
|
395
|
-
*
|
|
396
|
-
* )
|
|
509
|
+
* // Logical composition — T inferred from find() context
|
|
510
|
+
* posts.find($and(
|
|
511
|
+
* $or({ published: true }, { views: $gte(100) }),
|
|
512
|
+
* { title: $regex(/guide/i) },
|
|
513
|
+
* ))
|
|
397
514
|
*
|
|
398
515
|
* // Dynamic conditional building
|
|
399
516
|
* const conditions: TypedFilter<User>[] = []
|
|
@@ -464,6 +581,44 @@ declare function findOne<TDef extends AnyCollection>(handle: CollectionHandle<TD
|
|
|
464
581
|
* ```
|
|
465
582
|
*/
|
|
466
583
|
declare function findOneOrThrow<TDef extends AnyCollection>(handle: CollectionHandle<TDef>, filter: TypedFilter<InferDocument<TDef>>, options?: FindOneOptions): Promise<InferDocument<TDef>>;
|
|
584
|
+
/**
|
|
585
|
+
* Options for {@link find}.
|
|
586
|
+
*/
|
|
587
|
+
type FindOptions = {
|
|
588
|
+
/** Override the collection-level validation mode, or `false` to skip validation entirely. */
|
|
589
|
+
validate?: ValidationMode | false;
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* Find all documents matching the filter, returning a chainable typed cursor.
|
|
593
|
+
*
|
|
594
|
+
* The cursor is lazy — no query is executed until a terminal method
|
|
595
|
+
* (`toArray`, `for await`) is called. Use `sort`, `skip`, and `limit`
|
|
596
|
+
* to shape the query before executing.
|
|
597
|
+
*
|
|
598
|
+
* Each document is validated against the collection's Zod schema when
|
|
599
|
+
* a terminal method consumes it.
|
|
600
|
+
*
|
|
601
|
+
* @param handle - The collection handle to query.
|
|
602
|
+
* @param filter - Type-safe filter to match documents.
|
|
603
|
+
* @param options - Optional validation overrides.
|
|
604
|
+
* @returns A typed cursor for chaining query modifiers.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* ```ts
|
|
608
|
+
* const admins = await find(users, { role: 'admin' })
|
|
609
|
+
* .sort({ name: 1 })
|
|
610
|
+
* .limit(10)
|
|
611
|
+
* .toArray()
|
|
612
|
+
* ```
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* for await (const user of find(users, {})) {
|
|
617
|
+
* console.log(user.name)
|
|
618
|
+
* }
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
declare function find<TDef extends AnyCollection>(handle: CollectionHandle<TDef>, filter: TypedFilter<InferDocument<TDef>>, options?: FindOptions): TypedFindCursor<TDef>;
|
|
467
622
|
|
|
468
623
|
/**
|
|
469
624
|
* Typed wrapper around a MongoDB driver `Collection`.
|
|
@@ -561,6 +716,27 @@ declare class CollectionHandle<TDef extends AnyCollection = AnyCollection> {
|
|
|
561
716
|
* ```
|
|
562
717
|
*/
|
|
563
718
|
findOneOrThrow(filter: TypedFilter<InferDocument<TDef>>, options?: FindOneOptions): Promise<InferDocument<TDef>>;
|
|
719
|
+
/**
|
|
720
|
+
* Find all documents matching the filter, returning a chainable typed cursor.
|
|
721
|
+
*
|
|
722
|
+
* The cursor is lazy — no query is executed until a terminal method
|
|
723
|
+
* (`toArray`, `for await`) is called. Use `sort`, `skip`, and `limit`
|
|
724
|
+
* to shape the query before executing.
|
|
725
|
+
*
|
|
726
|
+
* @param filter - Type-safe filter to match documents.
|
|
727
|
+
* @param options - Optional validation overrides.
|
|
728
|
+
* @returns A typed cursor for chaining query modifiers.
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```ts
|
|
732
|
+
* const users = db.use(Users)
|
|
733
|
+
* const admins = await users.find({ role: 'admin' })
|
|
734
|
+
* .sort({ name: 1 })
|
|
735
|
+
* .limit(10)
|
|
736
|
+
* .toArray()
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
find(filter: TypedFilter<InferDocument<TDef>>, options?: FindOptions): TypedFindCursor<TDef>;
|
|
564
740
|
}
|
|
565
741
|
|
|
566
742
|
/**
|
|
@@ -678,6 +854,8 @@ declare function extractFieldIndexes(shape: z.core.$ZodShape): FieldIndexDefinit
|
|
|
678
854
|
*/
|
|
679
855
|
declare function collection<TShape extends z.core.$ZodShape>(name: string, shape: TShape, options?: CollectionOptions<Extract<keyof TShape, string>>): CollectionDefinition<TShape>;
|
|
680
856
|
|
|
857
|
+
type IndexDirection = 1 | -1;
|
|
858
|
+
type CompoundIndexOptions = NonNullable<CompoundIndexDefinition['options']>;
|
|
681
859
|
/**
|
|
682
860
|
* A builder for compound index definitions.
|
|
683
861
|
*
|
|
@@ -697,9 +875,6 @@ declare function collection<TShape extends z.core.$ZodShape>(name: string, shape
|
|
|
697
875
|
* index({ email: 1, role: -1 }).unique().name('email_role_idx')
|
|
698
876
|
* ```
|
|
699
877
|
*/
|
|
700
|
-
|
|
701
|
-
type IndexDirection = 1 | -1;
|
|
702
|
-
type CompoundIndexOptions = NonNullable<CompoundIndexDefinition['options']>;
|
|
703
878
|
declare class IndexBuilder<TKeys extends string> {
|
|
704
879
|
readonly fields: Partial<Record<TKeys, IndexDirection>>;
|
|
705
880
|
readonly options: CompoundIndexOptions;
|
|
@@ -1012,16 +1187,14 @@ declare const $not: <O extends Record<string, unknown>>(op: O) => {
|
|
|
1012
1187
|
*
|
|
1013
1188
|
* @example
|
|
1014
1189
|
* ```ts
|
|
1190
|
+
* // T inferred from collection's find() context
|
|
1015
1191
|
* users.find($or({ role: 'admin' }, { age: $gte(18) }))
|
|
1016
1192
|
*
|
|
1017
|
-
* //
|
|
1018
|
-
* const
|
|
1019
|
-
* if (name) conditions.push({ name })
|
|
1020
|
-
* if (role) conditions.push({ role })
|
|
1021
|
-
* users.find($or(...conditions))
|
|
1193
|
+
* // Explicit generic for standalone usage
|
|
1194
|
+
* const filter = $or<User>({ role: 'admin' }, { age: $gte(18) })
|
|
1022
1195
|
* ```
|
|
1023
1196
|
*/
|
|
1024
|
-
declare const $or: <T>(...filters: TypedFilter<T
|
|
1197
|
+
declare const $or: <T>(...filters: NoInfer<TypedFilter<T>>[]) => TypedFilter<T>;
|
|
1025
1198
|
/**
|
|
1026
1199
|
* Joins filter clauses with a logical AND. Matches documents that satisfy
|
|
1027
1200
|
* all of the provided filters. Useful for dynamic filter building where
|
|
@@ -1029,6 +1202,7 @@ declare const $or: <T>(...filters: TypedFilter<T>[]) => TypedFilter<T>;
|
|
|
1029
1202
|
*
|
|
1030
1203
|
* @example
|
|
1031
1204
|
* ```ts
|
|
1205
|
+
* // T inferred from collection's find() context
|
|
1032
1206
|
* users.find($and(
|
|
1033
1207
|
* $or({ role: 'admin' }, { role: 'moderator' }),
|
|
1034
1208
|
* { age: $gte(18) },
|
|
@@ -1036,7 +1210,7 @@ declare const $or: <T>(...filters: TypedFilter<T>[]) => TypedFilter<T>;
|
|
|
1036
1210
|
* ))
|
|
1037
1211
|
* ```
|
|
1038
1212
|
*/
|
|
1039
|
-
declare const $and: <T>(...filters: TypedFilter<T
|
|
1213
|
+
declare const $and: <T>(...filters: NoInfer<TypedFilter<T>>[]) => TypedFilter<T>;
|
|
1040
1214
|
/**
|
|
1041
1215
|
* Joins filter clauses with a logical NOR. Matches documents that fail
|
|
1042
1216
|
* all of the provided filters.
|
|
@@ -1047,7 +1221,7 @@ declare const $and: <T>(...filters: TypedFilter<T>[]) => TypedFilter<T>;
|
|
|
1047
1221
|
* users.find($nor({ role: 'banned' }, { role: 'suspended' }))
|
|
1048
1222
|
* ```
|
|
1049
1223
|
*/
|
|
1050
|
-
declare const $nor: <T>(...filters: TypedFilter<T
|
|
1224
|
+
declare const $nor: <T>(...filters: NoInfer<TypedFilter<T>>[]) => TypedFilter<T>;
|
|
1051
1225
|
/**
|
|
1052
1226
|
* Escape hatch for unsupported or raw MongoDB filter operators.
|
|
1053
1227
|
* Wraps an untyped filter object so it can be passed where `TypedFilter<T>` is expected.
|
|
@@ -1060,6 +1234,219 @@ declare const $nor: <T>(...filters: TypedFilter<T>[]) => TypedFilter<T>;
|
|
|
1060
1234
|
*/
|
|
1061
1235
|
declare const raw: <T = any>(filter: Record<string, unknown>) => TypedFilter<T>;
|
|
1062
1236
|
|
|
1237
|
+
/**
|
|
1238
|
+
* Extracts the element type from an array type.
|
|
1239
|
+
*
|
|
1240
|
+
* @example
|
|
1241
|
+
* ```ts
|
|
1242
|
+
* type E = ArrayElement<string[]> // string
|
|
1243
|
+
* type N = ArrayElement<number[]> // number
|
|
1244
|
+
* ```
|
|
1245
|
+
*/
|
|
1246
|
+
type ArrayElement<T> = T extends ReadonlyArray<infer E> ? E : never;
|
|
1247
|
+
/**
|
|
1248
|
+
* Fields valid for `$set`, `$setOnInsert`, `$min`, and `$max` operators.
|
|
1249
|
+
*
|
|
1250
|
+
* Allows top-level fields with matching value types plus dot-notation paths
|
|
1251
|
+
* for nested field updates.
|
|
1252
|
+
*
|
|
1253
|
+
* @example
|
|
1254
|
+
* ```ts
|
|
1255
|
+
* const set: SetFields<User> = { name: 'Alice', 'address.city': 'NYC' }
|
|
1256
|
+
* ```
|
|
1257
|
+
*/
|
|
1258
|
+
type SetFields<T> = {
|
|
1259
|
+
[K in keyof T]?: T[K];
|
|
1260
|
+
} & {
|
|
1261
|
+
[P in DotPaths<T>]?: DotPathType<T, P>;
|
|
1262
|
+
};
|
|
1263
|
+
/**
|
|
1264
|
+
* Fields valid for the `$inc` operator — only number-typed fields.
|
|
1265
|
+
*
|
|
1266
|
+
* Includes dot-notation paths that resolve to number types.
|
|
1267
|
+
*
|
|
1268
|
+
* @example
|
|
1269
|
+
* ```ts
|
|
1270
|
+
* const inc: IncFields<User> = { age: 1, 'stats.views': 5 }
|
|
1271
|
+
* ```
|
|
1272
|
+
*/
|
|
1273
|
+
type IncFields<T> = {
|
|
1274
|
+
[K in keyof T as NonNullable<T[K]> extends number ? K : never]?: number;
|
|
1275
|
+
} & {
|
|
1276
|
+
[P in DotPaths<T> as DotPathType<T, P> extends number ? P : never]?: number;
|
|
1277
|
+
};
|
|
1278
|
+
/**
|
|
1279
|
+
* Modifiers for the `$push` operator.
|
|
1280
|
+
*
|
|
1281
|
+
* Use with `$push` to insert multiple elements and control array position/size.
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```ts
|
|
1285
|
+
* const push = { tags: { $each: ['a', 'b'], $position: 0, $slice: 10 } }
|
|
1286
|
+
* ```
|
|
1287
|
+
*/
|
|
1288
|
+
type PushModifiers<E> = {
|
|
1289
|
+
/** Array of elements to push. */
|
|
1290
|
+
$each: E[];
|
|
1291
|
+
/** Position at which to insert elements. */
|
|
1292
|
+
$position?: number;
|
|
1293
|
+
/** Maximum array length after push. */
|
|
1294
|
+
$slice?: number;
|
|
1295
|
+
/** Sort order applied after push. */
|
|
1296
|
+
$sort?: 1 | -1 | Record<string, 1 | -1>;
|
|
1297
|
+
};
|
|
1298
|
+
/**
|
|
1299
|
+
* Fields valid for the `$push` operator — only array-typed fields.
|
|
1300
|
+
*
|
|
1301
|
+
* Accepts a single element value or a modifier object with `$each`.
|
|
1302
|
+
*
|
|
1303
|
+
* @example
|
|
1304
|
+
* ```ts
|
|
1305
|
+
* const push: PushFields<User> = { tags: 'dev' }
|
|
1306
|
+
* const pushMany: PushFields<User> = { tags: { $each: ['a', 'b'] } }
|
|
1307
|
+
* ```
|
|
1308
|
+
*/
|
|
1309
|
+
type PushFields<T> = {
|
|
1310
|
+
[K in keyof T as NonNullable<T[K]> extends ReadonlyArray<unknown> ? K : never]?: ArrayElement<NonNullable<T[K]>> | PushModifiers<ArrayElement<NonNullable<T[K]>>>;
|
|
1311
|
+
};
|
|
1312
|
+
/**
|
|
1313
|
+
* Fields valid for the `$pull` operator — only array-typed fields.
|
|
1314
|
+
*
|
|
1315
|
+
* For primitive arrays: accepts a value or comparison operators.
|
|
1316
|
+
* For object arrays: also accepts a `TypedFilter` on the element type.
|
|
1317
|
+
*
|
|
1318
|
+
* @example
|
|
1319
|
+
* ```ts
|
|
1320
|
+
* const pull: PullFields<User> = { tags: 'old' }
|
|
1321
|
+
* const pullQuery: PullFields<User> = { scores: { $lt: 50 } }
|
|
1322
|
+
* const pullObj: PullFields<User> = { comments: { author: 'spam' } }
|
|
1323
|
+
* ```
|
|
1324
|
+
*/
|
|
1325
|
+
type PullFields<T> = {
|
|
1326
|
+
[K in keyof T as NonNullable<T[K]> extends ReadonlyArray<unknown> ? K : never]?: ArrayElement<NonNullable<T[K]>> | ComparisonOperators<ArrayElement<NonNullable<T[K]>>> | (ArrayElement<NonNullable<T[K]>> extends Record<string, unknown> ? TypedFilter<ArrayElement<NonNullable<T[K]>>> : unknown);
|
|
1327
|
+
};
|
|
1328
|
+
/**
|
|
1329
|
+
* Modifier for the `$addToSet` operator's `$each` syntax.
|
|
1330
|
+
*
|
|
1331
|
+
* @example
|
|
1332
|
+
* ```ts
|
|
1333
|
+
* const addToSet = { tags: { $each: ['a', 'b'] } }
|
|
1334
|
+
* ```
|
|
1335
|
+
*/
|
|
1336
|
+
type AddToSetEach<E> = {
|
|
1337
|
+
$each: E[];
|
|
1338
|
+
};
|
|
1339
|
+
/**
|
|
1340
|
+
* Fields valid for the `$addToSet` operator — only array-typed fields.
|
|
1341
|
+
*
|
|
1342
|
+
* Accepts a single element value or `{ $each: [...] }` for multiple elements.
|
|
1343
|
+
*
|
|
1344
|
+
* @example
|
|
1345
|
+
* ```ts
|
|
1346
|
+
* const add: AddToSetFields<User> = { tags: 'dev' }
|
|
1347
|
+
* const addMany: AddToSetFields<User> = { tags: { $each: ['a', 'b'] } }
|
|
1348
|
+
* ```
|
|
1349
|
+
*/
|
|
1350
|
+
type AddToSetFields<T> = {
|
|
1351
|
+
[K in keyof T as NonNullable<T[K]> extends ReadonlyArray<unknown> ? K : never]?: ArrayElement<NonNullable<T[K]>> | AddToSetEach<ArrayElement<NonNullable<T[K]>>>;
|
|
1352
|
+
};
|
|
1353
|
+
/**
|
|
1354
|
+
* Fields valid for the `$pop` operator — only array-typed fields.
|
|
1355
|
+
*
|
|
1356
|
+
* Value `1` removes the last element, `-1` removes the first.
|
|
1357
|
+
*
|
|
1358
|
+
* @example
|
|
1359
|
+
* ```ts
|
|
1360
|
+
* const pop: PopFields<User> = { tags: -1 } // remove first
|
|
1361
|
+
* ```
|
|
1362
|
+
*/
|
|
1363
|
+
type PopFields<T> = {
|
|
1364
|
+
[K in keyof T as NonNullable<T[K]> extends ReadonlyArray<unknown> ? K : never]?: 1 | -1;
|
|
1365
|
+
};
|
|
1366
|
+
/**
|
|
1367
|
+
* Fields valid for the `$unset` operator — any existing field.
|
|
1368
|
+
*
|
|
1369
|
+
* Value is `''`, `true`, or `1` (all mean "remove this field").
|
|
1370
|
+
*
|
|
1371
|
+
* @example
|
|
1372
|
+
* ```ts
|
|
1373
|
+
* const unset: UnsetFields<User> = { middleName: '' }
|
|
1374
|
+
* ```
|
|
1375
|
+
*/
|
|
1376
|
+
type UnsetFields<T> = {
|
|
1377
|
+
[K in keyof T]?: '' | true | 1;
|
|
1378
|
+
};
|
|
1379
|
+
/**
|
|
1380
|
+
* Fields valid for the `$currentDate` operator — only Date-typed fields.
|
|
1381
|
+
*
|
|
1382
|
+
* Sets the field to the current date. Value is `true` or `{ $type: 'date' }`.
|
|
1383
|
+
*
|
|
1384
|
+
* @example
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* const cd: CurrentDateFields<User> = { createdAt: true }
|
|
1387
|
+
* ```
|
|
1388
|
+
*/
|
|
1389
|
+
type CurrentDateFields<T> = {
|
|
1390
|
+
[K in keyof T as NonNullable<T[K]> extends Date ? K : never]?: true | {
|
|
1391
|
+
$type: 'date';
|
|
1392
|
+
};
|
|
1393
|
+
};
|
|
1394
|
+
/**
|
|
1395
|
+
* Fields valid for the `$rename` operator.
|
|
1396
|
+
*
|
|
1397
|
+
* Renames an existing field to a new name. Key is the current field name,
|
|
1398
|
+
* value is the new name as a string.
|
|
1399
|
+
*
|
|
1400
|
+
* @example
|
|
1401
|
+
* ```ts
|
|
1402
|
+
* const rename: RenameFields<User> = { name: 'fullName' }
|
|
1403
|
+
* ```
|
|
1404
|
+
*/
|
|
1405
|
+
type RenameFields<T> = {
|
|
1406
|
+
[K in keyof T & string]?: string;
|
|
1407
|
+
};
|
|
1408
|
+
/**
|
|
1409
|
+
* Strict type-safe MongoDB update filter.
|
|
1410
|
+
*
|
|
1411
|
+
* Validates update operators against field types at compile time. Each operator
|
|
1412
|
+
* constrains which fields it accepts (e.g. `$inc` only on numbers, `$push` only
|
|
1413
|
+
* on arrays). Supports dot-notation paths for nested field access.
|
|
1414
|
+
*
|
|
1415
|
+
* @example
|
|
1416
|
+
* ```ts
|
|
1417
|
+
* const update: TypedUpdateFilter<User> = {
|
|
1418
|
+
* $set: { name: 'Alice' },
|
|
1419
|
+
* $inc: { age: 1 },
|
|
1420
|
+
* }
|
|
1421
|
+
* ```
|
|
1422
|
+
*/
|
|
1423
|
+
type TypedUpdateFilter<T> = {
|
|
1424
|
+
/** Sets the value of one or more fields. */
|
|
1425
|
+
$set?: SetFields<T>;
|
|
1426
|
+
/** Sets fields only when inserting (upsert). Same typing as `$set`. */
|
|
1427
|
+
$setOnInsert?: SetFields<T>;
|
|
1428
|
+
/** Increments numeric fields by the given amount. Only accepts number-typed fields. */
|
|
1429
|
+
$inc?: IncFields<T>;
|
|
1430
|
+
/** Updates the field if the given value is less than the current value. */
|
|
1431
|
+
$min?: SetFields<T>;
|
|
1432
|
+
/** Updates the field if the given value is greater than the current value. */
|
|
1433
|
+
$max?: SetFields<T>;
|
|
1434
|
+
/** Appends a value to an array field. Supports `$each`, `$position`, `$slice`, `$sort`. */
|
|
1435
|
+
$push?: PushFields<T>;
|
|
1436
|
+
/** Removes matching values from an array field. */
|
|
1437
|
+
$pull?: PullFields<T>;
|
|
1438
|
+
/** Adds a value to an array only if it doesn't already exist. */
|
|
1439
|
+
$addToSet?: AddToSetFields<T>;
|
|
1440
|
+
/** Removes the first (`-1`) or last (`1`) element of an array. */
|
|
1441
|
+
$pop?: PopFields<T>;
|
|
1442
|
+
/** Removes the specified fields from the document. */
|
|
1443
|
+
$unset?: UnsetFields<T>;
|
|
1444
|
+
/** Sets the field to the current date. Only accepts Date-typed fields. */
|
|
1445
|
+
$currentDate?: CurrentDateFields<T>;
|
|
1446
|
+
/** Renames a field. */
|
|
1447
|
+
$rename?: RenameFields<T>;
|
|
1448
|
+
};
|
|
1449
|
+
|
|
1063
1450
|
/**
|
|
1064
1451
|
* Type-level marker that carries the target collection type through the
|
|
1065
1452
|
* type system. Intersected with the schema return type by `.ref()` so
|
|
@@ -1144,4 +1531,4 @@ declare function getRefMetadata(schema: unknown): RefMetadata | undefined;
|
|
|
1144
1531
|
*/
|
|
1145
1532
|
declare function installRefExtension(): void;
|
|
1146
1533
|
|
|
1147
|
-
export { $and, $eq, $exists, $gt, $gte, $in, $lt, $lte, $ne, $nin, $nor, $not, $or, $regex, type AnyCollection, type CollectionDefinition, CollectionHandle, type CollectionOptions, type ComparisonOperators, type CompoundIndexDefinition, Database, type DotPathType, type DotPaths, type FieldIndexDefinition, type FindOneOptions, IndexBuilder, type IndexMetadata, type IndexOptions, type InferDocument, type InferInsert, type RefMarker, type RefMetadata, type ResolvedShape, type TypedFilter, type ValidationMode, type ZodObjectId, ZodmonNotFoundError, ZodmonValidationError, collection, createClient, extractDbName, extractFieldIndexes, findOne, findOneOrThrow, getIndexMetadata, getRefMetadata, index, insertMany, insertOne, installExtensions, installRefExtension, isOid, objectId, oid, raw };
|
|
1534
|
+
export { $and, $eq, $exists, $gt, $gte, $in, $lt, $lte, $ne, $nin, $nor, $not, $or, $regex, type AddToSetEach, type AddToSetFields, type AnyCollection, type ArrayElement, type CollectionDefinition, CollectionHandle, type CollectionOptions, type ComparisonOperators, type CompoundIndexDefinition, type CurrentDateFields, Database, type DotPathType, type DotPaths, type FieldIndexDefinition, type FindOneOptions, type FindOptions, type IncFields, IndexBuilder, type IndexMetadata, type IndexOptions, type InferDocument, type InferInsert, type PopFields, type PullFields, type PushFields, type PushModifiers, type RefMarker, type RefMetadata, type RenameFields, type ResolvedShape, type SetFields, type TypedFilter, TypedFindCursor, type TypedSort, type TypedUpdateFilter, type UnsetFields, type ValidationMode, type ZodObjectId, ZodmonNotFoundError, ZodmonValidationError, collection, createClient, extractDbName, extractFieldIndexes, find, findOne, findOneOrThrow, getIndexMetadata, getRefMetadata, index, insertMany, insertOne, installExtensions, installRefExtension, isOid, objectId, oid, raw };
|