mongoose 8.20.0 → 9.0.0-rc0
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/eslint.config.mjs +198 -0
- package/lib/aggregate.js +17 -73
- package/lib/cast/bigint.js +1 -1
- package/lib/cast/double.js +1 -1
- package/lib/cast/uuid.js +5 -48
- package/lib/cast.js +2 -2
- package/lib/connection.js +0 -1
- package/lib/cursor/aggregationCursor.js +14 -24
- package/lib/cursor/queryCursor.js +7 -14
- package/lib/document.js +125 -121
- package/lib/drivers/node-mongodb-native/connection.js +3 -10
- package/lib/error/objectParameter.js +1 -2
- package/lib/error/validation.js +0 -8
- package/lib/helpers/clone.js +1 -1
- package/lib/helpers/common.js +1 -1
- package/lib/helpers/indexes/isIndexEqual.js +0 -1
- package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
- package/lib/helpers/model/applyHooks.js +43 -53
- package/lib/helpers/model/applyMethods.js +2 -2
- package/lib/helpers/model/applyStaticHooks.js +1 -48
- package/lib/helpers/model/castBulkWrite.js +1 -1
- package/lib/helpers/parallelLimit.js +18 -36
- package/lib/helpers/pluralize.js +3 -3
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
- package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
- package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
- package/lib/helpers/populate/getSchemaTypes.js +5 -5
- package/lib/helpers/query/cast$expr.js +8 -10
- package/lib/helpers/query/castFilterPath.js +1 -1
- package/lib/helpers/query/castUpdate.js +14 -12
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
- package/lib/helpers/schema/applyPlugins.js +1 -1
- package/lib/helpers/schema/getIndexes.js +1 -7
- package/lib/helpers/timestamps/setupTimestamps.js +3 -6
- package/lib/helpers/updateValidators.js +57 -111
- package/lib/model.js +419 -607
- package/lib/mongoose.js +41 -13
- package/lib/plugins/saveSubdocs.js +24 -51
- package/lib/plugins/sharding.js +5 -4
- package/lib/plugins/validateBeforeSave.js +3 -13
- package/lib/query.js +101 -145
- package/lib/queryHelpers.js +2 -2
- package/lib/schema/array.js +41 -84
- package/lib/schema/documentArray.js +57 -94
- package/lib/schema/documentArrayElement.js +16 -11
- package/lib/schema/string.js +1 -1
- package/lib/schema/subdocument.js +22 -28
- package/lib/schema/uuid.js +0 -21
- package/lib/schema.js +81 -39
- package/lib/schemaType.js +39 -57
- package/lib/types/array/index.js +2 -2
- package/lib/types/array/methods/index.js +4 -4
- package/lib/types/arraySubdocument.js +1 -1
- package/lib/types/buffer.js +10 -10
- package/lib/types/decimal128.js +1 -1
- package/lib/types/documentArray/index.js +1 -1
- package/lib/types/documentArray/methods/index.js +5 -3
- package/lib/types/double.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +15 -43
- package/lib/types/uuid.js +1 -1
- package/lib/utils.js +1 -8
- package/lib/validOptions.js +3 -3
- package/package.json +11 -24
- package/types/connection.d.ts +20 -11
- package/types/document.d.ts +95 -26
- package/types/index.d.ts +143 -39
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +99 -75
- package/types/inferschematype.d.ts +17 -3
- package/types/middlewares.d.ts +0 -2
- package/types/models.d.ts +131 -199
- package/types/mongooseoptions.d.ts +6 -5
- package/types/pipelinestage.d.ts +1 -1
- package/types/query.d.ts +71 -139
- package/types/schemaoptions.d.ts +1 -1
- package/types/schematypes.d.ts +14 -10
- package/types/types.d.ts +3 -4
- package/types/utility.d.ts +68 -48
- package/types/validation.d.ts +18 -14
- package/browser.js +0 -8
- package/dist/browser.umd.js +0 -2
- package/lib/browser.js +0 -141
- package/lib/browserDocument.js +0 -101
- package/lib/documentProvider.js +0 -30
- package/lib/drivers/browser/binary.js +0 -14
- package/lib/drivers/browser/decimal128.js +0 -7
- package/lib/drivers/browser/index.js +0 -13
- package/lib/drivers/browser/objectid.js +0 -29
- package/lib/helpers/promiseOrCallback.js +0 -54
package/types/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
/// <reference path="./types.d.ts" />
|
|
21
21
|
/// <reference path="./utility.d.ts" />
|
|
22
22
|
/// <reference path="./validation.d.ts" />
|
|
23
|
+
/// <reference path="./inferhydrateddoctype.d.ts" />
|
|
23
24
|
/// <reference path="./inferrawdoctype.d.ts" />
|
|
24
25
|
/// <reference path="./inferschematype.d.ts" />
|
|
25
26
|
/// <reference path="./virtuals.d.ts" />
|
|
@@ -64,13 +65,15 @@ declare module 'mongoose' {
|
|
|
64
65
|
* Sanitizes query filters against query selector injection attacks by wrapping
|
|
65
66
|
* any nested objects that have a property whose name starts with `$` in a `$eq`.
|
|
66
67
|
*/
|
|
67
|
-
export function sanitizeFilter<T>(filter:
|
|
68
|
+
export function sanitizeFilter<T>(filter: QueryFilter<T>): QueryFilter<T>;
|
|
68
69
|
|
|
69
70
|
/** Gets mongoose options */
|
|
70
71
|
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
|
|
71
72
|
|
|
72
73
|
export function omitUndefined<T extends Record<string, any>>(val: T): T;
|
|
73
74
|
|
|
75
|
+
export type HydratedDocFromModel<M extends Model<any>> = ReturnType<M['hydrate']>;
|
|
76
|
+
|
|
74
77
|
/* ! ignore */
|
|
75
78
|
export type CompileModelOptions = {
|
|
76
79
|
overwriteModels?: boolean,
|
|
@@ -87,14 +90,20 @@ declare module 'mongoose' {
|
|
|
87
90
|
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
88
91
|
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
89
92
|
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
ObtainSchemaGeneric<TSchema, '
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
// If first schema generic param is set, that means we have an explicit raw doc type,
|
|
94
|
+
// so user should also specify a hydrated doc type if the auto inferred one isn't correct.
|
|
95
|
+
IsItRecordAndNotAny<ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>> extends true
|
|
96
|
+
? ObtainSchemaGeneric<TSchema, 'THydratedDocumentType'>
|
|
97
|
+
: HydratedDocument<
|
|
98
|
+
InferSchemaType<TSchema>,
|
|
99
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
100
|
+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
101
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
102
|
+
InferSchemaType<TSchema>,
|
|
103
|
+
ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>
|
|
104
|
+
>,
|
|
105
|
+
TSchema,
|
|
106
|
+
ObtainSchemaGeneric<TSchema, 'TLeanResultType'>
|
|
98
107
|
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
|
99
108
|
|
|
100
109
|
export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
|
|
@@ -158,25 +167,27 @@ declare module 'mongoose' {
|
|
|
158
167
|
|
|
159
168
|
/** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
|
|
160
169
|
export type HydratedDocument<
|
|
161
|
-
|
|
170
|
+
HydratedDocPathsType,
|
|
162
171
|
TOverrides = {},
|
|
163
172
|
TQueryHelpers = {},
|
|
164
173
|
TVirtuals = {},
|
|
165
|
-
|
|
174
|
+
RawDocType = HydratedDocPathsType,
|
|
175
|
+
TSchemaOptions = DefaultSchemaOptions
|
|
166
176
|
> = IfAny<
|
|
167
|
-
|
|
177
|
+
HydratedDocPathsType,
|
|
168
178
|
any,
|
|
169
179
|
TOverrides extends Record<string, never> ?
|
|
170
|
-
Document<unknown, TQueryHelpers,
|
|
180
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions> :
|
|
171
181
|
IfAny<
|
|
172
182
|
TOverrides,
|
|
173
|
-
Document<unknown, TQueryHelpers,
|
|
174
|
-
Document<unknown, TQueryHelpers,
|
|
175
|
-
Default__v<Require_id<
|
|
183
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
|
|
184
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & MergeType<
|
|
185
|
+
Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
|
|
176
186
|
TOverrides
|
|
177
187
|
>
|
|
178
188
|
>
|
|
179
189
|
>;
|
|
190
|
+
|
|
180
191
|
export type HydratedSingleSubdocument<
|
|
181
192
|
DocType,
|
|
182
193
|
TOverrides = {}
|
|
@@ -214,6 +225,7 @@ declare module 'mongoose' {
|
|
|
214
225
|
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'> & ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
215
226
|
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
216
227
|
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
228
|
+
InferSchemaType<TSchema>,
|
|
217
229
|
ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>
|
|
218
230
|
>;
|
|
219
231
|
|
|
@@ -287,7 +299,16 @@ declare module 'mongoose' {
|
|
|
287
299
|
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
288
300
|
ResolveSchemaOptions<TSchemaOptions>
|
|
289
301
|
>,
|
|
290
|
-
THydratedDocumentType = HydratedDocument<
|
|
302
|
+
THydratedDocumentType = HydratedDocument<
|
|
303
|
+
DocType,
|
|
304
|
+
AddDefaultId<DocType, TVirtuals, TSchemaOptions> & TInstanceMethods,
|
|
305
|
+
TQueryHelpers,
|
|
306
|
+
AddDefaultId<DocType, TVirtuals, TSchemaOptions>,
|
|
307
|
+
IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : DocType,
|
|
308
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
309
|
+
>,
|
|
310
|
+
TSchemaDefinition = SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType>,
|
|
311
|
+
LeanResultType = IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : Default__v<Require_id<BufferToBinary<FlattenMaps<DocType>>>>
|
|
291
312
|
>
|
|
292
313
|
extends events.EventEmitter {
|
|
293
314
|
/**
|
|
@@ -311,6 +332,69 @@ declare module 'mongoose' {
|
|
|
311
332
|
> | ResolveSchemaOptions<TSchemaOptions>
|
|
312
333
|
);
|
|
313
334
|
|
|
335
|
+
/* Creates a new schema with the given definition and options. Equivalent to `new Schema(definition, options)`, but with better automatic type inference. */
|
|
336
|
+
static create<
|
|
337
|
+
TSchemaDefinition extends SchemaDefinition<undefined, RawDocType, THydratedDocumentType>,
|
|
338
|
+
TSchemaOptions extends DefaultSchemaOptions,
|
|
339
|
+
RawDocType extends ApplySchemaOptions<
|
|
340
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
341
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
342
|
+
>,
|
|
343
|
+
THydratedDocumentType extends AnyObject = HydratedDocument<
|
|
344
|
+
InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
345
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
346
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
347
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
348
|
+
RawDocType
|
|
349
|
+
>
|
|
350
|
+
>(def: TSchemaDefinition): Schema<
|
|
351
|
+
RawDocType,
|
|
352
|
+
Model<RawDocType, any, any, any>,
|
|
353
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
354
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
355
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
356
|
+
TSchemaOptions extends { statics: any } ? TSchemaOptions['statics'] : {},
|
|
357
|
+
TSchemaOptions,
|
|
358
|
+
ApplySchemaOptions<
|
|
359
|
+
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
360
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
361
|
+
>,
|
|
362
|
+
THydratedDocumentType,
|
|
363
|
+
TSchemaDefinition,
|
|
364
|
+
ApplySchemaOptions<
|
|
365
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>, { bufferToBinary: true }>,
|
|
366
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
367
|
+
>
|
|
368
|
+
>;
|
|
369
|
+
|
|
370
|
+
static create<
|
|
371
|
+
TSchemaDefinition extends SchemaDefinition<undefined, RawDocType, THydratedDocumentType>,
|
|
372
|
+
TSchemaOptions extends SchemaOptions<InferRawDocType<TSchemaDefinition>>,
|
|
373
|
+
RawDocType extends ApplySchemaOptions<
|
|
374
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
375
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
376
|
+
>,
|
|
377
|
+
THydratedDocumentType extends AnyObject = HydratedDocument<InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>>
|
|
378
|
+
>(def: TSchemaDefinition, options: TSchemaOptions): Schema<
|
|
379
|
+
RawDocType,
|
|
380
|
+
Model<RawDocType, any, any, any>,
|
|
381
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
382
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
383
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
384
|
+
TSchemaOptions extends { statics: any } ? TSchemaOptions['statics'] : {},
|
|
385
|
+
TSchemaOptions,
|
|
386
|
+
ApplySchemaOptions<
|
|
387
|
+
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
388
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
389
|
+
>,
|
|
390
|
+
THydratedDocumentType,
|
|
391
|
+
TSchemaDefinition,
|
|
392
|
+
ApplySchemaOptions<
|
|
393
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>, { bufferToBinary: true }>,
|
|
394
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
395
|
+
>
|
|
396
|
+
>;
|
|
397
|
+
|
|
314
398
|
/** Adds key path / schema type pairs to this schema. */
|
|
315
399
|
add(obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType> | Schema, prefix?: string): this;
|
|
316
400
|
|
|
@@ -331,7 +415,7 @@ declare module 'mongoose' {
|
|
|
331
415
|
clearIndexes(): this;
|
|
332
416
|
|
|
333
417
|
/** Returns a copy of this schema */
|
|
334
|
-
clone
|
|
418
|
+
clone(): this;
|
|
335
419
|
|
|
336
420
|
discriminator<DisSchema = Schema>(name: string | number, schema: DisSchema, options?: DiscriminatorOptions): this;
|
|
337
421
|
|
|
@@ -410,7 +494,7 @@ declare module 'mongoose' {
|
|
|
410
494
|
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
411
495
|
post<T = THydratedDocumentType>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
412
496
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T, Array<any>>): this;
|
|
413
|
-
post
|
|
497
|
+
post(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<TModelType>): this;
|
|
414
498
|
|
|
415
499
|
// this = never since it never happens
|
|
416
500
|
post<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: false }, fn: PostMiddlewareFunction<never, never>): this;
|
|
@@ -451,14 +535,14 @@ declare module 'mongoose' {
|
|
|
451
535
|
// method aggregate and insertMany with PostMiddlewareFunction
|
|
452
536
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
453
537
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
454
|
-
post
|
|
455
|
-
post
|
|
538
|
+
post(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<TModelType, TModelType>): this;
|
|
539
|
+
post(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<TModelType, TModelType>): this;
|
|
456
540
|
|
|
457
541
|
// method aggregate and insertMany with ErrorHandlingMiddlewareFunction
|
|
458
542
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
459
543
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
460
|
-
post
|
|
461
|
-
post
|
|
544
|
+
post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<TModelType>): this;
|
|
545
|
+
post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<TModelType>): this;
|
|
462
546
|
|
|
463
547
|
/** Defines a pre hook for the model. */
|
|
464
548
|
// this = never since it never happens
|
|
@@ -493,31 +577,28 @@ declare module 'mongoose' {
|
|
|
493
577
|
// method aggregate
|
|
494
578
|
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
495
579
|
/* method insertMany */
|
|
496
|
-
pre
|
|
580
|
+
pre(
|
|
497
581
|
method: 'insertMany' | RegExp,
|
|
498
582
|
fn: (
|
|
499
|
-
this:
|
|
500
|
-
next: (err?: CallbackError) => void,
|
|
583
|
+
this: TModelType,
|
|
501
584
|
docs: any | Array<any>,
|
|
502
585
|
options?: InsertManyOptions & { lean?: boolean }
|
|
503
586
|
) => void | Promise<void>
|
|
504
587
|
): this;
|
|
505
588
|
/* method bulkWrite */
|
|
506
|
-
pre
|
|
589
|
+
pre(
|
|
507
590
|
method: 'bulkWrite' | RegExp,
|
|
508
591
|
fn: (
|
|
509
|
-
this:
|
|
510
|
-
next: (err?: CallbackError) => void,
|
|
592
|
+
this: TModelType,
|
|
511
593
|
ops: Array<AnyBulkWriteOperation<any>>,
|
|
512
594
|
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
|
|
513
595
|
) => void | Promise<void>
|
|
514
596
|
): this;
|
|
515
597
|
/* method createCollection */
|
|
516
|
-
pre
|
|
598
|
+
pre(
|
|
517
599
|
method: 'createCollection' | RegExp,
|
|
518
600
|
fn: (
|
|
519
|
-
this:
|
|
520
|
-
next: (err?: CallbackError) => void,
|
|
601
|
+
this: TModelType,
|
|
521
602
|
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
|
|
522
603
|
) => void | Promise<void>
|
|
523
604
|
): this;
|
|
@@ -561,7 +642,7 @@ declare module 'mongoose' {
|
|
|
561
642
|
virtuals: TVirtuals;
|
|
562
643
|
|
|
563
644
|
/** Returns the virtual type with the given `name`. */
|
|
564
|
-
virtualpath
|
|
645
|
+
virtualpath(name: string): VirtualType<THydratedDocumentType> | null;
|
|
565
646
|
|
|
566
647
|
static ObjectId: typeof Schema.Types.ObjectId;
|
|
567
648
|
}
|
|
@@ -570,7 +651,7 @@ declare module 'mongoose' {
|
|
|
570
651
|
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String | Schema.Types.String;
|
|
571
652
|
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean | Schema.Types.Boolean;
|
|
572
653
|
export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date | Schema.Types.Date;
|
|
573
|
-
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId | Types.ObjectId;
|
|
654
|
+
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId | Types.ObjectId | typeof Types.ObjectId;
|
|
574
655
|
export type BufferSchemaDefinition = typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer;
|
|
575
656
|
export type Decimal128SchemaDefinition = 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 | Schema.Types.Decimal128 | Types.Decimal128;
|
|
576
657
|
export type BigintSchemaDefinition = 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | Schema.Types.BigInt | typeof BigInt | BigInt;
|
|
@@ -591,7 +672,7 @@ declare module 'mongoose' {
|
|
|
591
672
|
|
|
592
673
|
export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> =
|
|
593
674
|
// ThisType intersection here avoids corrupting ThisType for SchemaTypeOptions (see test gh11828)
|
|
594
|
-
| SchemaDefinitionWithBuiltInClass<T> & ThisType<
|
|
675
|
+
| SchemaDefinitionWithBuiltInClass<T> & ThisType<THydratedDocumentType>
|
|
595
676
|
| SchemaTypeOptions<T extends undefined ? any : T, EnforcedDocType, THydratedDocumentType>
|
|
596
677
|
| typeof SchemaType
|
|
597
678
|
| Schema<any, any, any>
|
|
@@ -601,7 +682,7 @@ declare module 'mongoose' {
|
|
|
601
682
|
| SchemaDefinition<T, EnforcedDocType, THydratedDocumentType>
|
|
602
683
|
| SchemaDefinition<Unpacked<T>, EnforcedDocType, THydratedDocumentType>[]
|
|
603
684
|
| typeof Schema.Types.Mixed
|
|
604
|
-
| MixedSchemaTypeOptions<EnforcedDocType>;
|
|
685
|
+
| MixedSchemaTypeOptions<EnforcedDocType, THydratedDocumentType>;
|
|
605
686
|
|
|
606
687
|
export type SchemaDefinition<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> = T extends undefined
|
|
607
688
|
? { [path: string]: SchemaDefinitionProperty; }
|
|
@@ -610,7 +691,7 @@ declare module 'mongoose' {
|
|
|
610
691
|
export type AnyArray<T> = T[] | ReadonlyArray<T>;
|
|
611
692
|
export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
|
|
612
693
|
|
|
613
|
-
export interface MixedSchemaTypeOptions<EnforcedDocType> extends SchemaTypeOptions<Schema.Types.Mixed, EnforcedDocType> {
|
|
694
|
+
export interface MixedSchemaTypeOptions<EnforcedDocType, THydratedDocumentType> extends SchemaTypeOptions<Schema.Types.Mixed, EnforcedDocType, THydratedDocumentType> {
|
|
614
695
|
type: typeof Schema.Types.Mixed;
|
|
615
696
|
}
|
|
616
697
|
|
|
@@ -656,7 +737,7 @@ declare module 'mongoose' {
|
|
|
656
737
|
count?: boolean;
|
|
657
738
|
|
|
658
739
|
/** Add an extra match condition to `populate()`. */
|
|
659
|
-
match?:
|
|
740
|
+
match?: QueryFilter<any> | ((doc: Record<string, any>, virtual?: this) => Record<string, any> | null);
|
|
660
741
|
|
|
661
742
|
/** Add a default `limit` to the `populate()` query. */
|
|
662
743
|
limit?: number;
|
|
@@ -844,6 +925,25 @@ declare module 'mongoose' {
|
|
|
844
925
|
: BufferToBinary<T[K]>;
|
|
845
926
|
} : T;
|
|
846
927
|
|
|
928
|
+
/**
|
|
929
|
+
* Converts any Buffer properties into "{ type: 'buffer', data: [1, 2, 3] }" format for JSON serialization
|
|
930
|
+
*/
|
|
931
|
+
export type UUIDToJSON<T> = T extends mongodb.UUID
|
|
932
|
+
? string
|
|
933
|
+
: T extends Document
|
|
934
|
+
? T
|
|
935
|
+
: T extends TreatAsPrimitives
|
|
936
|
+
? T
|
|
937
|
+
: T extends Record<string, any> ? {
|
|
938
|
+
[K in keyof T]: T[K] extends mongodb.UUID
|
|
939
|
+
? string
|
|
940
|
+
: T[K] extends Types.DocumentArray<infer ItemType>
|
|
941
|
+
? Types.DocumentArray<UUIDToJSON<ItemType>>
|
|
942
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
943
|
+
? HydratedSingleSubdocument<SubdocType>
|
|
944
|
+
: UUIDToJSON<T[K]>;
|
|
945
|
+
} : T;
|
|
946
|
+
|
|
847
947
|
/**
|
|
848
948
|
* Converts any ObjectId properties into strings for JSON serialization
|
|
849
949
|
*/
|
|
@@ -903,7 +1003,9 @@ declare module 'mongoose' {
|
|
|
903
1003
|
FlattenMaps<
|
|
904
1004
|
BufferToJSON<
|
|
905
1005
|
ObjectIdToString<
|
|
906
|
-
|
|
1006
|
+
UUIDToJSON<
|
|
1007
|
+
DateToString<T>
|
|
1008
|
+
>
|
|
907
1009
|
>
|
|
908
1010
|
>
|
|
909
1011
|
>
|
|
@@ -930,5 +1032,7 @@ declare module 'mongoose' {
|
|
|
930
1032
|
|
|
931
1033
|
export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction;
|
|
932
1034
|
|
|
1035
|
+
export function overwriteMiddlewareArguments(val: any): Kareem.OverwriteArguments;
|
|
1036
|
+
|
|
933
1037
|
export default mongoose;
|
|
934
1038
|
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IsPathRequired,
|
|
3
|
+
IsSchemaTypeFromBuiltinClass,
|
|
4
|
+
RequiredPaths,
|
|
5
|
+
OptionalPaths,
|
|
6
|
+
PathEnumOrString
|
|
7
|
+
} from './inferschematype';
|
|
8
|
+
import { UUID } from 'mongodb';
|
|
9
|
+
|
|
10
|
+
declare module 'mongoose' {
|
|
11
|
+
export type InferHydratedDocTypeFromSchema<TSchema extends Schema<any>> = ObtainSchemaGeneric<TSchema, 'THydratedDocumentType'>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Given a schema definition, returns the hydrated document type from the schema definition.
|
|
15
|
+
*/
|
|
16
|
+
export type InferHydratedDocType<
|
|
17
|
+
DocDefinition,
|
|
18
|
+
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions
|
|
19
|
+
> = Require_id<ApplySchemaOptions<{
|
|
20
|
+
[
|
|
21
|
+
K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
|
|
22
|
+
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)
|
|
23
|
+
]: IsPathRequired<DocDefinition[K], TSchemaOptions['typeKey']> extends true
|
|
24
|
+
? ObtainHydratedDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>
|
|
25
|
+
: ObtainHydratedDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']> | null;
|
|
26
|
+
}, TSchemaOptions>>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @summary Obtains schema Path type.
|
|
30
|
+
* @description Obtains Path type by separating path type from other options and calling {@link ResolveHydratedPathType}
|
|
31
|
+
* @param {PathValueType} PathValueType Document definition path type.
|
|
32
|
+
* @param {TypeKey} TypeKey A generic refers to document definition.
|
|
33
|
+
*/
|
|
34
|
+
type ObtainHydratedDocumentPathType<
|
|
35
|
+
PathValueType,
|
|
36
|
+
TypeKey extends string = DefaultTypeKey
|
|
37
|
+
> = ResolveHydratedPathType<
|
|
38
|
+
TypeKey extends keyof PathValueType
|
|
39
|
+
? TypeKey extends keyof PathValueType[TypeKey]
|
|
40
|
+
? PathValueType
|
|
41
|
+
: PathValueType[TypeKey]
|
|
42
|
+
: PathValueType,
|
|
43
|
+
TypeKey extends keyof PathValueType
|
|
44
|
+
? TypeKey extends keyof PathValueType[TypeKey]
|
|
45
|
+
? {}
|
|
46
|
+
: Omit<PathValueType, TypeKey>
|
|
47
|
+
: {},
|
|
48
|
+
TypeKey,
|
|
49
|
+
HydratedDocTypeHint<PathValueType>
|
|
50
|
+
>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @summary Allows users to optionally choose their own type for a schema field for stronger typing.
|
|
54
|
+
*/
|
|
55
|
+
type HydratedDocTypeHint<T> = T extends { __hydratedDocTypeHint: infer U } ? U: never;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Same as inferSchemaType, except:
|
|
59
|
+
*
|
|
60
|
+
* 1. Replace `Types.DocumentArray` and `Types.Array` with vanilla `Array`
|
|
61
|
+
* 2. Replace `ObtainDocumentPathType` with `ObtainHydratedDocumentPathType`
|
|
62
|
+
* 3. Replace `ResolvePathType` with `ResolveHydratedPathType`
|
|
63
|
+
*
|
|
64
|
+
* @summary Resolve path type by returning the corresponding type.
|
|
65
|
+
* @param {PathValueType} PathValueType Document definition path type.
|
|
66
|
+
* @param {Options} Options Document definition path options except path type.
|
|
67
|
+
* @param {TypeKey} TypeKey A generic of literal string type. Refers to the property used for path type definition.
|
|
68
|
+
* @returns Type
|
|
69
|
+
*/
|
|
70
|
+
type ResolveHydratedPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey'], TypeHint = never> =
|
|
71
|
+
IsNotNever<TypeHint> extends true ? TypeHint
|
|
72
|
+
: PathValueType extends Schema<any, any, any, any, any, any, any, any, infer THydratedDocumentType> ?
|
|
73
|
+
THydratedDocumentType :
|
|
74
|
+
PathValueType extends AnyArray<infer Item> ?
|
|
75
|
+
IfEquals<Item, never, any[], Item extends Schema<infer EmbeddedRawDocType, any, any, any, any, any, any, any, infer EmbeddedHydratedDocType extends AnyObject, infer TSchemaDefition> ?
|
|
76
|
+
IsItRecordAndNotAny<EmbeddedRawDocType> extends true ?
|
|
77
|
+
Types.DocumentArray<EmbeddedRawDocType, Types.Subdocument<EmbeddedHydratedDocType['_id'], unknown, EmbeddedHydratedDocType> & EmbeddedHydratedDocType> :
|
|
78
|
+
Types.DocumentArray<InferRawDocType<TSchemaDefition>, Types.Subdocument<InferHydratedDocType<TSchemaDefition>['_id'], unknown, InferHydratedDocType<TSchemaDefition>> & InferHydratedDocType<TSchemaDefition>> :
|
|
79
|
+
Item extends Record<TypeKey, any> ?
|
|
80
|
+
Item[TypeKey] extends Function | String ?
|
|
81
|
+
Types.Array<ObtainHydratedDocumentPathType<Item, TypeKey>> :
|
|
82
|
+
Types.DocumentArray<
|
|
83
|
+
InferRawDocType<Item>,
|
|
84
|
+
Types.Subdocument<InferHydratedDocType<Item>['_id'], unknown, InferHydratedDocType<Item>> & InferHydratedDocType<Item>
|
|
85
|
+
>:
|
|
86
|
+
IsSchemaTypeFromBuiltinClass<Item> extends true ?
|
|
87
|
+
Types.Array<ResolveHydratedPathType<Item, { enum: Options['enum'] }, TypeKey>> :
|
|
88
|
+
IsItRecordAndNotAny<Item> extends true ?
|
|
89
|
+
Item extends Record<string, never> ?
|
|
90
|
+
Types.Array<ObtainHydratedDocumentPathType<Item, TypeKey>> :
|
|
91
|
+
Types.DocumentArray<
|
|
92
|
+
InferRawDocType<Item>,
|
|
93
|
+
Types.Subdocument<InferHydratedDocType<Item>['_id'], unknown, InferHydratedDocType<Item>> & InferHydratedDocType<Item>
|
|
94
|
+
> :
|
|
95
|
+
Types.Array<ObtainHydratedDocumentPathType<Item, TypeKey>>
|
|
96
|
+
>
|
|
97
|
+
: PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']>
|
|
98
|
+
: IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']>
|
|
99
|
+
: PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number
|
|
100
|
+
: PathValueType extends DateSchemaDefinition ? NativeDate
|
|
101
|
+
: PathValueType extends BufferSchemaDefinition ? Buffer
|
|
102
|
+
: PathValueType extends BooleanSchemaDefinition ? boolean
|
|
103
|
+
: PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
|
|
104
|
+
: PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
|
|
105
|
+
: PathValueType extends BigintSchemaDefinition ? bigint
|
|
106
|
+
: PathValueType extends UuidSchemaDefinition ? UUID
|
|
107
|
+
: PathValueType extends DoubleSchemaDefinition ? Types.Double
|
|
108
|
+
: PathValueType extends typeof Schema.Types.Mixed ? any
|
|
109
|
+
: PathValueType extends MapSchemaDefinition ? Map<string, ObtainHydratedDocumentPathType<Options['of']>>
|
|
110
|
+
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
|
|
111
|
+
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
|
|
112
|
+
: PathValueType extends ArrayConstructor ? Types.Array<any>
|
|
113
|
+
: PathValueType extends Record<string, any> ? InferHydratedDocType<PathValueType, { typeKey: TypeKey }>
|
|
114
|
+
: unknown;
|
|
115
|
+
}
|