mongoose 8.19.4 → 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.
Files changed (105) hide show
  1. package/eslint.config.mjs +198 -0
  2. package/lib/aggregate.js +17 -73
  3. package/lib/cast/bigint.js +1 -1
  4. package/lib/cast/double.js +1 -1
  5. package/lib/cast/uuid.js +5 -48
  6. package/lib/cast.js +3 -3
  7. package/lib/connection.js +0 -1
  8. package/lib/cursor/aggregationCursor.js +14 -24
  9. package/lib/cursor/queryCursor.js +7 -14
  10. package/lib/document.js +125 -121
  11. package/lib/drivers/node-mongodb-native/connection.js +3 -10
  12. package/lib/error/divergentArray.js +2 -2
  13. package/lib/error/objectParameter.js +1 -2
  14. package/lib/error/validation.js +0 -8
  15. package/lib/helpers/clone.js +1 -1
  16. package/lib/helpers/common.js +1 -1
  17. package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +10 -0
  18. package/lib/helpers/indexes/isIndexEqual.js +0 -1
  19. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  20. package/lib/helpers/model/applyHooks.js +43 -53
  21. package/lib/helpers/model/applyMethods.js +2 -2
  22. package/lib/helpers/model/applyStaticHooks.js +1 -48
  23. package/lib/helpers/model/castBulkWrite.js +1 -1
  24. package/lib/helpers/parallelLimit.js +18 -36
  25. package/lib/helpers/pluralize.js +3 -3
  26. package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
  27. package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
  28. package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
  29. package/lib/helpers/populate/getSchemaTypes.js +5 -5
  30. package/lib/helpers/query/cast$expr.js +8 -10
  31. package/lib/helpers/query/castFilterPath.js +1 -1
  32. package/lib/helpers/query/castUpdate.js +15 -13
  33. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  34. package/lib/helpers/schema/applyPlugins.js +1 -1
  35. package/lib/helpers/schema/getIndexes.js +1 -7
  36. package/lib/helpers/timestamps/setupTimestamps.js +3 -6
  37. package/lib/helpers/updateValidators.js +57 -111
  38. package/lib/model.js +419 -607
  39. package/lib/mongoose.js +41 -13
  40. package/lib/plugins/saveSubdocs.js +24 -51
  41. package/lib/plugins/sharding.js +5 -4
  42. package/lib/plugins/validateBeforeSave.js +3 -13
  43. package/lib/query.js +101 -145
  44. package/lib/queryHelpers.js +2 -2
  45. package/lib/schema/array.js +46 -84
  46. package/lib/schema/bigint.js +4 -2
  47. package/lib/schema/boolean.js +4 -2
  48. package/lib/schema/buffer.js +4 -2
  49. package/lib/schema/date.js +4 -2
  50. package/lib/schema/decimal128.js +4 -2
  51. package/lib/schema/documentArray.js +66 -91
  52. package/lib/schema/documentArrayElement.js +18 -11
  53. package/lib/schema/double.js +4 -2
  54. package/lib/schema/int32.js +4 -2
  55. package/lib/schema/map.js +87 -6
  56. package/lib/schema/mixed.js +4 -2
  57. package/lib/schema/number.js +4 -2
  58. package/lib/schema/objectId.js +4 -2
  59. package/lib/schema/string.js +5 -3
  60. package/lib/schema/subdocument.js +27 -31
  61. package/lib/schema/union.js +11 -3
  62. package/lib/schema/uuid.js +4 -23
  63. package/lib/schema.js +91 -91
  64. package/lib/schemaType.js +67 -59
  65. package/lib/types/array/index.js +2 -2
  66. package/lib/types/array/methods/index.js +4 -4
  67. package/lib/types/arraySubdocument.js +1 -1
  68. package/lib/types/buffer.js +10 -10
  69. package/lib/types/decimal128.js +1 -1
  70. package/lib/types/documentArray/index.js +1 -1
  71. package/lib/types/documentArray/methods/index.js +32 -18
  72. package/lib/types/double.js +1 -1
  73. package/lib/types/map.js +1 -2
  74. package/lib/types/objectid.js +1 -1
  75. package/lib/types/subdocument.js +15 -43
  76. package/lib/types/uuid.js +1 -1
  77. package/lib/utils.js +1 -8
  78. package/lib/validOptions.js +3 -3
  79. package/package.json +11 -24
  80. package/types/connection.d.ts +20 -11
  81. package/types/document.d.ts +96 -27
  82. package/types/index.d.ts +143 -39
  83. package/types/inferhydrateddoctype.d.ts +115 -0
  84. package/types/inferrawdoctype.d.ts +99 -75
  85. package/types/inferschematype.d.ts +17 -3
  86. package/types/middlewares.d.ts +0 -2
  87. package/types/models.d.ts +131 -199
  88. package/types/mongooseoptions.d.ts +6 -5
  89. package/types/pipelinestage.d.ts +1 -1
  90. package/types/query.d.ts +71 -139
  91. package/types/schemaoptions.d.ts +1 -1
  92. package/types/schematypes.d.ts +14 -10
  93. package/types/types.d.ts +3 -4
  94. package/types/utility.d.ts +68 -48
  95. package/types/validation.d.ts +18 -14
  96. package/browser.js +0 -8
  97. package/dist/browser.umd.js +0 -2
  98. package/lib/browser.js +0 -141
  99. package/lib/browserDocument.js +0 -101
  100. package/lib/documentProvider.js +0 -30
  101. package/lib/drivers/browser/binary.js +0 -14
  102. package/lib/drivers/browser/decimal128.js +0 -7
  103. package/lib/drivers/browser/index.js +0 -13
  104. package/lib/drivers/browser/objectid.js +0 -29
  105. package/lib/helpers/promiseOrCallback.js +0 -54
@@ -1,27 +1,36 @@
1
1
  import {
2
2
  IsSchemaTypeFromBuiltinClass,
3
3
  PathEnumOrString,
4
- type OptionalPathKeys,
5
- type RequiredPathKeys
4
+ OptionalPaths,
5
+ RequiredPaths,
6
+ IsPathRequired
6
7
  } from './inferschematype';
8
+ import { Binary, UUID } from 'mongodb';
7
9
 
8
10
  declare module 'mongoose' {
9
- export type InferRawDocType<DocDefinition, TSchemaOptions extends Record<any, any> = DefaultSchemaOptions> = Show<
10
- ApplySchemaOptions<
11
- {
12
- [K in RequiredPathKeys<DocDefinition, TSchemaOptions['typeKey']>]: ObtainRawDocumentPathType<
13
- DocDefinition[K],
14
- TSchemaOptions['typeKey']
15
- >;
16
- } & {
17
- [K in OptionalPathKeys<DocDefinition, TSchemaOptions['typeKey']>]?: ObtainRawDocumentPathType<
18
- DocDefinition[K],
19
- TSchemaOptions['typeKey']
20
- > | null;
21
- },
22
- TSchemaOptions
23
- >
24
- >;
11
+ export type InferRawDocTypeFromSchema<TSchema extends Schema<any>> = IsItRecordAndNotAny<ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>> extends true
12
+ ? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
13
+ : FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
14
+
15
+ export type InferRawDocType<
16
+ SchemaDefinition,
17
+ TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
18
+ TTransformOptions = { bufferToBinary: false }
19
+ > = Require_id<ApplySchemaOptions<{
20
+ [
21
+ K in keyof (RequiredPaths<SchemaDefinition, TSchemaOptions['typeKey']> &
22
+ OptionalPaths<SchemaDefinition, TSchemaOptions['typeKey']>)
23
+ ]: IsPathRequired<SchemaDefinition[K], TSchemaOptions['typeKey']> extends true
24
+ ? ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions>
25
+ : ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
26
+ }, TSchemaOptions>>;
27
+
28
+ /**
29
+ * @summary Allows users to optionally choose their own type for a schema field for stronger typing.
30
+ * Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`.
31
+ */
32
+ type RawDocTypeHint<T> = IsAny<T> extends true ? never
33
+ : T extends { __rawDocTypeHint: infer U } ? U: never;
25
34
 
26
35
  /**
27
36
  * @summary Obtains schema Path type.
@@ -29,14 +38,26 @@ declare module 'mongoose' {
29
38
  * @param {PathValueType} PathValueType Document definition path type.
30
39
  * @param {TypeKey} TypeKey A generic refers to document definition.
31
40
  */
32
- type ObtainRawDocumentPathType<PathValueType, TypeKey extends string = DefaultTypeKey> =
33
- TypeKey extends keyof PathValueType ?
34
- ResolveRawPathType<PathValueType[TypeKey], Omit<PathValueType, TypeKey>, TypeKey>
35
- : ResolveRawPathType<PathValueType, {}, TypeKey>;
41
+ type ObtainRawDocumentPathType<
42
+ PathValueType,
43
+ TypeKey extends string = DefaultTypeKey,
44
+ TTransformOptions = { bufferToBinary: false }
45
+ > = ResolveRawPathType<
46
+ TypeKey extends keyof PathValueType ?
47
+ TypeKey extends keyof PathValueType[TypeKey] ?
48
+ PathValueType
49
+ : PathValueType[TypeKey]
50
+ : PathValueType,
51
+ TypeKey extends keyof PathValueType ?
52
+ TypeKey extends keyof PathValueType[TypeKey] ?
53
+ {}
54
+ : Omit<PathValueType, TypeKey>
55
+ : {},
56
+ TypeKey,
57
+ TTransformOptions,
58
+ RawDocTypeHint<PathValueType>
59
+ >;
36
60
 
37
- // can be efficiently checked like:
38
- // `[T] extends [neverOrAny] ? T : ...`
39
- // to avoid edge cases
40
61
  type neverOrAny = ' ~neverOrAny~';
41
62
 
42
63
  /**
@@ -52,54 +73,57 @@ declare module 'mongoose' {
52
73
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
53
74
  * @returns Number, "Number" or "number" will be resolved to number type.
54
75
  */
55
- type ResolveRawPathType<
56
- PathValueType,
57
- Options extends SchemaTypeOptions<PathValueType> = {},
58
- TypeKey extends string = DefaultSchemaOptions['typeKey']
59
- > =
60
- [PathValueType] extends [neverOrAny] ? PathValueType
61
- : PathValueType extends Schema ? InferSchemaType<PathValueType>
62
- : PathValueType extends ReadonlyArray<infer Item> ?
63
- [Item] extends [never] ? any[]
64
- : Item extends Schema ?
65
- // If Item is a schema, infer its type.
66
- Array<InferSchemaType<Item>>
67
- : TypeKey extends keyof Item ?
68
- Item[TypeKey] extends Function | String ?
69
- // If Item has a type key that's a string or a callable, it must be a scalar,
70
- // so we can directly obtain its path type.
71
- ObtainRawDocumentPathType<Item, TypeKey>[]
72
- : // If the type key isn't callable, then this is an array of objects, in which case
73
- // we need to call InferRawDocType to correctly infer its type.
74
- Array<InferRawDocType<Item>>
75
- : IsSchemaTypeFromBuiltinClass<Item> extends true ? ResolveRawPathType<Item, { enum: Options['enum'] }, TypeKey>[]
76
- : IsItRecordAndNotAny<Item> extends true ?
77
- Item extends Record<string, never> ?
78
- ObtainRawDocumentPathType<Item, TypeKey>[]
79
- : Array<InferRawDocType<Item>>
80
- : ObtainRawDocumentPathType<Item, TypeKey>[]
81
- : PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']>
82
- : IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']>
83
- : PathValueType extends NumberSchemaDefinition ?
84
- Options['enum'] extends ReadonlyArray<any> ?
85
- Options['enum'][number]
86
- : number
87
- : PathValueType extends DateSchemaDefinition ? NativeDate
88
- : PathValueType extends BufferSchemaDefinition ? Buffer
89
- : PathValueType extends BooleanSchemaDefinition ? boolean
90
- : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
91
- : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
92
- : PathValueType extends BigintSchemaDefinition ? bigint
93
- : PathValueType extends UuidSchemaDefinition ? Buffer
94
- : PathValueType extends DoubleSchemaDefinition ? Types.Double
95
- : PathValueType extends MapSchemaDefinition ? Map<string, ObtainRawDocumentPathType<Options['of']>>
96
- : PathValueType extends UnionSchemaDefinition ?
97
- ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
98
- : PathValueType extends ArrayConstructor ? any[]
99
- : PathValueType extends typeof Schema.Types.Mixed ? any
100
- : IfEquals<PathValueType, ObjectConstructor> extends true ? any
101
- : IfEquals<PathValueType, {}> extends true ? any
102
- : PathValueType extends typeof SchemaType ? PathValueType['prototype']
103
- : PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
104
- : unknown;
76
+ type ResolveRawPathType<
77
+ PathValueType,
78
+ Options extends SchemaTypeOptions<PathValueType> = {},
79
+ TypeKey extends string = DefaultSchemaOptions['typeKey'],
80
+ TTransformOptions = { bufferToBinary: false },
81
+ TypeHint = never
82
+ > =
83
+ IsNotNever<TypeHint> extends true ? TypeHint
84
+ : [PathValueType] extends [neverOrAny] ? PathValueType
85
+ : PathValueType extends Schema<infer RawDocType, any, any, any, any, any, any, any, any, infer TSchemaDefinition> ? IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : InferRawDocType<TSchemaDefinition, DefaultSchemaOptions, TTransformOptions>
86
+ : PathValueType extends ReadonlyArray<infer Item> ?
87
+ IfEquals<Item, never> extends true ? any[]
88
+ : Item extends Schema<infer RawDocType, any, any, any, any, any, any, any, any, infer TSchemaDefinition> ?
89
+ // If Item is a schema, infer its type.
90
+ Array<IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : InferRawDocType<TSchemaDefinition, DefaultSchemaOptions, TTransformOptions>>
91
+ : TypeKey extends keyof Item ?
92
+ Item[TypeKey] extends Function | String ?
93
+ // If Item has a type key that's a string or a callable, it must be a scalar,
94
+ // so we can directly obtain its path type.
95
+ ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
96
+ : // If the type key isn't callable, then this is an array of objects, in which case
97
+ // we need to call InferRawDocType to correctly infer its type.
98
+ Array<InferRawDocType<Item, DefaultSchemaOptions, TTransformOptions>>
99
+ : IsSchemaTypeFromBuiltinClass<Item> extends true ? ResolveRawPathType<Item, { enum: Options['enum'] }, TypeKey, TTransformOptions>[]
100
+ : IsItRecordAndNotAny<Item> extends true ?
101
+ Item extends Record<string, never> ?
102
+ ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
103
+ : Array<InferRawDocType<Item, DefaultSchemaOptions, TTransformOptions>>
104
+ : ObtainRawDocumentPathType<Item, TypeKey, TTransformOptions>[]
105
+ : PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']>
106
+ : IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']>
107
+ : PathValueType extends NumberSchemaDefinition ?
108
+ Options['enum'] extends ReadonlyArray<any> ?
109
+ Options['enum'][number]
110
+ : number
111
+ : PathValueType extends DateSchemaDefinition ? NativeDate
112
+ : PathValueType extends BufferSchemaDefinition ? (TTransformOptions extends { bufferToBinary: true } ? Binary : Buffer)
113
+ : PathValueType extends BooleanSchemaDefinition ? boolean
114
+ : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
115
+ : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
116
+ : PathValueType extends BigintSchemaDefinition ? bigint
117
+ : PathValueType extends UuidSchemaDefinition ? Types.UUID
118
+ : PathValueType extends MapSchemaDefinition ? Record<string, ObtainRawDocumentPathType<Options['of'], TypeKey, TTransformOptions>>
119
+ : PathValueType extends DoubleSchemaDefinition ? Types.Double
120
+ : PathValueType extends UnionSchemaDefinition ?
121
+ ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
122
+ : PathValueType extends ArrayConstructor ? any[]
123
+ : PathValueType extends typeof Schema.Types.Mixed ? any
124
+ : IfEquals<PathValueType, ObjectConstructor> extends true ? any
125
+ : IfEquals<PathValueType, {}> extends true ? any
126
+ : PathValueType extends typeof SchemaType ? PathValueType['prototype']
127
+ : PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
128
+ : unknown;
105
129
  }
@@ -59,6 +59,9 @@ declare module 'mongoose' {
59
59
  */
60
60
  export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
61
61
 
62
+ export type DefaultIdVirtual = { id: string };
63
+ export type AddDefaultId<DocType, TVirtuals, TSchemaOptions> = (DocType extends { id: any } ? TVirtuals : TSchemaOptions extends { id: false } ? TVirtuals : TVirtuals & { id: string });
64
+
62
65
  /**
63
66
  * @summary Obtains schema Generic type by using generic alias.
64
67
  * @param {TSchema} TSchema A generic of schema type instance.
@@ -76,6 +79,8 @@ declare module 'mongoose' {
76
79
  | 'TSchemaOptions'
77
80
  | 'DocType'
78
81
  | 'THydratedDocumentType'
82
+ | 'TSchemaDefinition'
83
+ | 'TLeanResultType'
79
84
  > =
80
85
  TSchema extends (
81
86
  Schema<
@@ -87,7 +92,9 @@ declare module 'mongoose' {
87
92
  infer TStaticMethods,
88
93
  infer TSchemaOptions,
89
94
  infer DocType,
90
- infer THydratedDocumentType
95
+ infer THydratedDocumentType,
96
+ infer TSchemaDefinition,
97
+ infer TLeanResultType
91
98
  >
92
99
  ) ?
93
100
  {
@@ -95,11 +102,13 @@ declare module 'mongoose' {
95
102
  M: M;
96
103
  TInstanceMethods: TInstanceMethods;
97
104
  TQueryHelpers: TQueryHelpers;
98
- TVirtuals: TVirtuals;
105
+ TVirtuals: AddDefaultId<DocType, TVirtuals, TSchemaOptions>;
99
106
  TStaticMethods: TStaticMethods;
100
107
  TSchemaOptions: TSchemaOptions;
101
108
  DocType: DocType;
102
109
  THydratedDocumentType: THydratedDocumentType;
110
+ TSchemaDefinition: TSchemaDefinition;
111
+ TLeanResultType: TLeanResultType;
103
112
  }[alias]
104
113
  : unknown;
105
114
 
@@ -236,6 +245,10 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> =
236
245
  : T extends Record<string, infer V> ? V
237
246
  : string;
238
247
 
248
+ type UnionToType<T extends readonly any[]> = T[number] extends infer U
249
+ ? ResolvePathType<U>
250
+ : never;
251
+
239
252
  type IsSchemaTypeFromBuiltinClass<T> =
240
253
  T extends typeof String ? true
241
254
  : T extends typeof Number ? true
@@ -254,6 +267,7 @@ type IsSchemaTypeFromBuiltinClass<T> =
254
267
  : T extends Types.Decimal128 ? true
255
268
  : T extends NativeDate ? true
256
269
  : T extends typeof Schema.Types.Mixed ? true
270
+ : T extends Types.UUID ? true
257
271
  : unknown extends Buffer ? false
258
272
  : T extends Buffer ? true
259
273
  : false;
@@ -301,12 +315,12 @@ type ResolvePathType<
301
315
  Options['enum'][number]
302
316
  : number
303
317
  : PathValueType extends DateSchemaDefinition ? NativeDate
318
+ : PathValueType extends UuidSchemaDefinition ? Types.UUID
304
319
  : PathValueType extends BufferSchemaDefinition ? Buffer
305
320
  : PathValueType extends BooleanSchemaDefinition ? boolean
306
321
  : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
307
322
  : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
308
323
  : PathValueType extends BigintSchemaDefinition ? bigint
309
- : PathValueType extends UuidSchemaDefinition ? Buffer
310
324
  : PathValueType extends DoubleSchemaDefinition ? Types.Double
311
325
  : PathValueType extends MapSchemaDefinition ? Map<string, ObtainDocumentPathType<Options['of']>>
312
326
  : PathValueType extends UnionSchemaDefinition ?
@@ -36,12 +36,10 @@ declare module 'mongoose' {
36
36
 
37
37
  type PreMiddlewareFunction<ThisType = any> = (
38
38
  this: ThisType,
39
- next: CallbackWithoutResultAndOptionalError,
40
39
  opts?: Record<string, any>
41
40
  ) => void | Promise<void> | Kareem.SkipWrappedFunction;
42
41
  type PreSaveMiddlewareFunction<ThisType = any> = (
43
42
  this: ThisType,
44
- next: CallbackWithoutResultAndOptionalError,
45
43
  opts: SaveOptions
46
44
  ) => void | Promise<void> | Kareem.SkipWrappedFunction;
47
45
  type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;