mongoose 9.2.4 → 9.3.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.
Files changed (37) hide show
  1. package/lib/aggregate.js +34 -11
  2. package/lib/connection.js +10 -24
  3. package/lib/cursor/aggregationCursor.js +2 -2
  4. package/lib/cursor/changeStream.js +55 -35
  5. package/lib/cursor/queryCursor.js +3 -3
  6. package/lib/document.js +218 -205
  7. package/lib/drivers/node-mongodb-native/collection.js +1 -1
  8. package/lib/helpers/projection/parseProjection.js +9 -4
  9. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  10. package/lib/helpers/setDefaultsOnInsert.js +7 -5
  11. package/lib/helpers/update/removeUnusedArrayFilters.js +7 -2
  12. package/lib/helpers/updateValidators.js +1 -8
  13. package/lib/model.js +79 -52
  14. package/lib/mongoose.js +4 -6
  15. package/lib/options/schemaDocumentArrayOptions.js +23 -0
  16. package/lib/options/schemaSubdocumentOptions.js +23 -0
  17. package/lib/plugins/index.js +0 -1
  18. package/lib/query.js +133 -94
  19. package/lib/schema/buffer.js +1 -1
  20. package/lib/schema/documentArray.js +1 -1
  21. package/lib/schema.js +17 -17
  22. package/lib/schemaType.js +15 -7
  23. package/package.json +5 -4
  24. package/types/aggregate.d.ts +3 -0
  25. package/types/connection.d.ts +2 -2
  26. package/types/cursor.d.ts +1 -1
  27. package/types/document.d.ts +4 -4
  28. package/types/index.d.ts +8 -2
  29. package/types/indexes.d.ts +1 -1
  30. package/types/inferhydrateddoctype.d.ts +23 -3
  31. package/types/inferrawdoctype.d.ts +32 -10
  32. package/types/inferschematype.d.ts +31 -4
  33. package/types/pipelinestage.d.ts +3 -2
  34. package/types/query.d.ts +2 -2
  35. package/types/schemaoptions.d.ts +8 -6
  36. package/types/schematypes.d.ts +3 -0
  37. package/lib/plugins/validateBeforeSave.js +0 -47
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "9.2.4",
4
+ "version": "9.3.1",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -21,7 +21,7 @@
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
23
  "kareem": "3.2.0",
24
- "mongodb": "~7.0",
24
+ "mongodb": "~7.1",
25
25
  "mpath": "0.9.0",
26
26
  "mquery": "6.0.0",
27
27
  "ms": "2.1.3",
@@ -41,10 +41,10 @@
41
41
  "dox": "1.0.0",
42
42
  "eslint": "10.0.2",
43
43
  "eslint-plugin-mocha-no-only": "1.2.0",
44
- "expect-type": "^1.3.0",
45
44
  "express": "^4.19.2",
46
45
  "fs-extra": "~11.3.0",
47
46
  "globals": "^17.4.0",
47
+ "glob": "^13.0.6",
48
48
  "highlight.js": "11.11.1",
49
49
  "linkinator": "7.x",
50
50
  "lodash.isequal": "4.5.0",
@@ -85,7 +85,8 @@
85
85
  "docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x",
86
86
  "docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rm -rf ./docs/7.x && mv ./tmp ./docs/7.x",
87
87
  "docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rm -rf ./docs/8.x && mv ./tmp ./docs/8.x",
88
- "docs:check-links": "linkinator http://127.0.0.1:8089 --silent",
88
+ "docs:check-links": "linkinator http://127.0.0.1:8089 --silent --recurse --skip cdn.carbonads.com --skip m.servedby-buysellads.com --skip \"127\\.0\\.0\\.1:8089\/docs\/\\d+.x\"",
89
+ "docs:update-mongodb-links": "node ./scripts/update-mongodb-links.js",
89
90
  "lint": "eslint .",
90
91
  "lint-js": "eslint . --ext .js --ext .cjs",
91
92
  "lint-ts": "eslint . --ext .ts",
@@ -125,6 +125,9 @@ declare module 'mongoose' {
125
125
  /** Returns the current pipeline */
126
126
  pipeline(): PipelineStage[];
127
127
 
128
+ /** Returns the current pipeline typed for use in `$unionWith` */
129
+ pipelineForUnionWith(): PipelineStage.UnionWithPipelineStage[];
130
+
128
131
  /** Appends a new $project operator to this aggregate pipeline. */
129
132
  project(arg: PipelineStage.Project['$project'] | string): this;
130
133
 
@@ -143,7 +143,7 @@ declare module 'mongoose' {
143
143
  get(key: string): any;
144
144
 
145
145
  /**
146
- * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
146
+ * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html) instance
147
147
  * that this connection uses to talk to MongoDB.
148
148
  */
149
149
  getClient(): mongodb.MongoClient;
@@ -252,7 +252,7 @@ declare module 'mongoose' {
252
252
  set(key: string, value: any): any;
253
253
 
254
254
  /**
255
- * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
255
+ * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html) instance
256
256
  * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
257
257
  * reuse it.
258
258
  */
package/types/cursor.d.ts CHANGED
@@ -17,7 +17,7 @@ declare module 'mongoose' {
17
17
  [Symbol.asyncDispose](): Promise<void>;
18
18
 
19
19
  /**
20
- * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html#addCursorFlag).
20
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html#addCursorFlag).
21
21
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
22
22
  */
23
23
  addCursorFlag(flag: CursorFlag, value: boolean): this;
@@ -89,8 +89,8 @@ declare module 'mongoose' {
89
89
  $markValid(path: string): void;
90
90
 
91
91
  /** Returns the model with the given name on this document's associated connection. */
92
- $model<ModelType = Model<unknown>>(name: string): ModelType;
93
- $model<ModelType = Model<DocType>>(): ModelType;
92
+ $model<ModelType extends Model<unknown>>(name: string): ModelType;
93
+ $model<ModelType extends Model<DocType>>(): ModelType;
94
94
 
95
95
  /**
96
96
  * A string containing the current operation that Mongoose is executing
@@ -218,8 +218,8 @@ declare module 'mongoose' {
218
218
  markModified(path: string, scope?: any): void;
219
219
 
220
220
  /** Returns the model with the given name on this document's associated connection. */
221
- model<ModelType = Model<unknown>>(name: string): ModelType;
222
- model<ModelType = Model<DocType>>(): ModelType;
221
+ model<ModelType extends Model<unknown>>(name: string): ModelType;
222
+ model<ModelType extends Model<DocType>>(): ModelType;
223
223
 
224
224
  /** Returns the list of paths that have been modified. */
225
225
  modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
package/types/index.d.ts CHANGED
@@ -309,7 +309,7 @@ declare module 'mongoose' {
309
309
  IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : DocType,
310
310
  ResolveSchemaOptions<TSchemaOptions>
311
311
  >,
312
- TSchemaDefinition = SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType>,
312
+ TSchemaDefinition = IfAny<RawDocType, unknown, SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType>>,
313
313
  LeanResultType = IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : Default__v<Require_id<BufferToBinary<FlattenMaps<DocType>>>>
314
314
  >
315
315
  extends events.EventEmitter {
@@ -379,7 +379,12 @@ declare module 'mongoose' {
379
379
  >,
380
380
  TMethods = TSchemaOptions extends { methods: infer M } ? { [K in keyof M]: OmitThisParameter<M[K]> } : {},
381
381
  TStatics = TSchemaOptions extends { statics: infer S } ? { [K in keyof S]: OmitThisParameter<S[K]> } : {}
382
- >(def: TSchemaDefinition, options: TSchemaOptions): Schema<
382
+ >(def: TSchemaDefinition, options: TSchemaOptions & {
383
+ statics?: SchemaOptionsStaticsPropertyType<
384
+ TStatics,
385
+ Model<RawDocType>
386
+ >
387
+ }): Schema<
383
388
  RawDocType,
384
389
  Model<RawDocType, any, any, any>,
385
390
  TMethods,
@@ -497,6 +502,7 @@ declare module 'mongoose' {
497
502
 
498
503
  /** Registers a plugin for this schema. */
499
504
  plugin<PFunc extends PluginFunction<DocType, TModelType, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
505
+ plugin<PFunc extends PluginFunction<DocType, Model<DocType, any, any, any, any, any>, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
500
506
 
501
507
  /** Defines a post hook for the model. */
502
508
 
@@ -14,7 +14,7 @@ declare module 'mongoose' {
14
14
  cleanIndexes(options?: { toDrop?: string[], hideIndexes?: boolean }): Promise<string[]>;
15
15
 
16
16
  /**
17
- * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#createIndex)
17
+ * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/7.0/classes/Collection.html#createIndex)
18
18
  * function.
19
19
  */
20
20
  createIndexes(options?: mongodb.CreateIndexesOptions): Promise<void>;
@@ -46,13 +46,32 @@ declare module 'mongoose' {
46
46
  : Omit<PathValueType, TypeKey>
47
47
  : {},
48
48
  TypeKey,
49
- HydratedDocTypeHint<PathValueType>
49
+ HydratedDocTypeHint<PathValueType>,
50
+ HydratedDiscriminatorEnumType<PathValueType>
50
51
  >;
51
52
 
52
53
  /**
53
54
  * @summary Allows users to optionally choose their own type for a schema field for stronger typing.
54
55
  */
55
- type HydratedDocTypeHint<T> = T extends { __hydratedDocTypeHint: infer U } ? U: never;
56
+ type HydratedDocTypeHint<T> = T extends { __hydratedDocTypeHint: infer U } ? U
57
+ : never;
58
+
59
+ type ResolveDiscriminatorHydratedPathType<TBaseSchema extends Schema, TDiscriminators> =
60
+ IsAny<TDiscriminators> extends true ? never
61
+ : TDiscriminators extends Record<string, any> ?
62
+ TDiscriminators[keyof TDiscriminators] extends Schema ?
63
+ MergeType<InferHydratedDocTypeFromSchema<TBaseSchema>, InferHydratedDocTypeFromSchema<TDiscriminators[keyof TDiscriminators]>>
64
+ : never
65
+ : never;
66
+
67
+ type HydratedDiscriminatorEnumType<T> = string extends keyof T ? never
68
+ : number extends keyof T ? never
69
+ : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ?
70
+ IsAny<BaseType> extends true ? never
71
+ : BaseType extends Schema ?
72
+ ResolveDiscriminatorHydratedPathType<BaseType, TDiscriminators>
73
+ : never
74
+ : never;
56
75
 
57
76
  /**
58
77
  * Same as inferSchemaType, except:
@@ -67,8 +86,9 @@ declare module 'mongoose' {
67
86
  * @param {TypeKey} TypeKey A generic of literal string type. Refers to the property used for path type definition.
68
87
  * @returns Type
69
88
  */
70
- type ResolveHydratedPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey'], TypeHint = never> =
89
+ type ResolveHydratedPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey'], TypeHint = never, TDiscriminatorEnumType = never> =
71
90
  IsNotNever<TypeHint> extends true ? TypeHint
91
+ : IsNotNever<TDiscriminatorEnumType> extends true ? TDiscriminatorEnumType
72
92
  : PathValueType extends Schema<any, any, any, any, any, any, any, any, infer THydratedDocumentType> ?
73
93
  THydratedDocumentType :
74
94
  PathValueType extends AnyArray<infer Item> ?
@@ -10,7 +10,9 @@ import { Binary, UUID } from 'mongodb';
10
10
  declare module 'mongoose' {
11
11
  export type InferRawDocTypeFromSchema<TSchema extends Schema<any>> = IsItRecordAndNotAny<ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>> extends true
12
12
  ? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
13
- : FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
13
+ : unknown extends ObtainSchemaGeneric<TSchema, 'TSchemaDefinition'>
14
+ ? Require_id<FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>>
15
+ : InferRawDocType<ObtainSchemaGeneric<TSchema, 'TSchemaDefinition'>>;
14
16
 
15
17
  export type InferRawDocTypeWithout_id<
16
18
  SchemaDefinition,
@@ -40,6 +42,23 @@ declare module 'mongoose' {
40
42
  type RawDocTypeHint<T> = IsAny<T> extends true ? never
41
43
  : T extends { __rawDocTypeHint: infer U } ? U: never;
42
44
 
45
+ type ResolveDiscriminatorRawPathType<TBaseSchema extends Schema, TDiscriminators> =
46
+ IsAny<TDiscriminators> extends true ? never
47
+ : TDiscriminators extends Record<string, any> ?
48
+ TDiscriminators[keyof TDiscriminators] extends Schema ?
49
+ MergeType<InferRawDocTypeFromSchema<TBaseSchema>, InferRawDocTypeFromSchema<TDiscriminators[keyof TDiscriminators]>>
50
+ : never
51
+ : never;
52
+
53
+ type RawDiscriminatorEnumType<T> = string extends keyof T ? never
54
+ : number extends keyof T ? never
55
+ : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ?
56
+ IsAny<BaseType> extends true ? never
57
+ : BaseType extends Schema ?
58
+ ResolveDiscriminatorRawPathType<BaseType, TDiscriminators>
59
+ : never
60
+ : never;
61
+
43
62
  /**
44
63
  * @summary Obtains schema Path type.
45
64
  * @description Obtains Path type by separating path type from other options and calling {@link ResolveRawPathType}
@@ -64,6 +83,7 @@ declare module 'mongoose' {
64
83
  TypeKey,
65
84
  TTransformOptions,
66
85
  RawDocTypeHint<PathValueType>,
86
+ RawDiscriminatorEnumType<PathValueType>,
67
87
  TypeKey extends keyof PathValueType ? false : true
68
88
  >;
69
89
 
@@ -88,25 +108,27 @@ declare module 'mongoose' {
88
108
  TypeKey extends string = DefaultSchemaOptions['typeKey'],
89
109
  TTransformOptions = { bufferToBinary: false },
90
110
  TypeHint = never,
111
+ TDiscriminatorEnumType = never,
91
112
  IsNestedPath extends boolean = false
92
113
  > =
93
114
  IsNotNever<TypeHint> extends true ? TypeHint
115
+ : IsNotNever<TDiscriminatorEnumType> extends true ? TDiscriminatorEnumType
94
116
  : [PathValueType] extends [neverOrAny] ? PathValueType
95
- : PathValueType extends Schema<infer RawDocType, any, any, any, any, any, infer TSchemaOptions, infer DocType, any, infer TSchemaDefinition> ?
96
- IsItRecordAndNotAny<RawDocType> extends true ?
97
- RawDocType :
98
- string extends keyof TSchemaDefinition ?
99
- TSchemaOptions extends { _id: false } ?
100
- FlattenMaps<SubdocsToPOJOs<DocType>> :
101
- Require_id<FlattenMaps<SubdocsToPOJOs<DocType>>> :
102
- InferRawDocType<TSchemaDefinition, TSchemaOptions & Record<any, any>, TTransformOptions>
117
+ : PathValueType extends Schema<infer RawDocType, any, any, any, any, any, infer TSchemaOptions, infer DocType, any, infer TSchemaDefinition> ?
118
+ IsItRecordAndNotAny<RawDocType> extends true ?
119
+ RawDocType :
120
+ unknown extends TSchemaDefinition ?
121
+ TSchemaOptions extends { _id: false } ?
122
+ FlattenMaps<SubdocsToPOJOs<DocType>> :
123
+ Require_id<FlattenMaps<SubdocsToPOJOs<DocType>>> :
124
+ InferRawDocType<TSchemaDefinition, TSchemaOptions & Record<any, any>, TTransformOptions>
103
125
  : PathValueType extends ReadonlyArray<infer Item> ?
104
126
  IfEquals<Item, never> extends true ? any[]
105
127
  : Item extends Schema<infer RawDocType, any, any, any, any, any, infer TSchemaOptions, infer DocType, any, infer TSchemaDefinition> ?
106
128
  // If Item is a schema, infer its type.
107
129
  Array<IsItRecordAndNotAny<RawDocType> extends true ?
108
130
  RawDocType :
109
- string extends keyof TSchemaDefinition ?
131
+ unknown extends TSchemaDefinition ?
110
132
  TSchemaOptions extends { _id: false } ?
111
133
  FlattenMaps<SubdocsToPOJOs<DocType>> :
112
134
  Require_id<FlattenMaps<SubdocsToPOJOs<DocType>>> :
@@ -8,10 +8,12 @@ import {
8
8
  DefaultSchemaOptions,
9
9
  DefaultTypeKey,
10
10
  DoubleSchemaDefinition,
11
+ IfAny,
11
12
  IfEquals,
12
13
  InferSchemaType,
13
14
  IsItRecordAndNotAny,
14
15
  MapSchemaDefinition,
16
+ MergeType,
15
17
  NumberSchemaDefinition,
16
18
  ObjectIdSchemaDefinition,
17
19
  ObtainDocumentType,
@@ -243,7 +245,8 @@ type ObtainDocumentPathType<PathValueType, TypeKey extends string = DefaultTypeK
243
245
  : Omit<PathValueType, TypeKey>
244
246
  : {},
245
247
  TypeKey,
246
- TypeHint<PathValueType>
248
+ TypeHint<PathValueType>,
249
+ DiscriminatorEnumType<PathValueType>
247
250
  >;
248
251
 
249
252
  /**
@@ -260,6 +263,22 @@ type UnionToType<T extends readonly any[]> = T[number] extends infer U
260
263
  ? ResolvePathType<U>
261
264
  : never;
262
265
 
266
+ type ResolveDiscriminatorPathType<TBaseSchema extends Schema, TDiscriminators> =
267
+ IfAny<TDiscriminators, never,
268
+ TDiscriminators extends Record<string, any> ?
269
+ TDiscriminators[keyof TDiscriminators] extends Schema ?
270
+ MergeType<InferSchemaType<TBaseSchema>, InferSchemaType<TDiscriminators[keyof TDiscriminators]>>
271
+ : never
272
+ : never>;
273
+
274
+ type DiscriminatorEnumType<T> = string extends keyof T ? never
275
+ : number extends keyof T ? never
276
+ : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ?
277
+ IfAny<BaseType, never, BaseType extends Schema ?
278
+ ResolveDiscriminatorPathType<BaseType, TDiscriminators>
279
+ : never>
280
+ : never;
281
+
263
282
  type IsSchemaTypeFromBuiltinClass<T> =
264
283
  T extends typeof String ? true
265
284
  : unknown extends Buffer ? false
@@ -294,9 +313,12 @@ type ResolvePathType<
294
313
  PathValueType,
295
314
  Options extends SchemaTypeOptions<PathValueType> = {},
296
315
  TypeKey extends string = DefaultSchemaOptions['typeKey'],
297
- TypeHint = never
316
+ TypeHint = never,
317
+ TDiscriminatorEnumType = never
298
318
  > = [TypeHint] extends [never]
299
- ? PathValueType extends Schema ? InferSchemaType<PathValueType>
319
+ ? [TDiscriminatorEnumType] extends [never]
320
+ ? PathValueType extends Schema ?
321
+ InferSchemaType<PathValueType>
300
322
  : PathValueType extends AnyArray<infer Item> ?
301
323
  [Item] extends [never]
302
324
  ? any[]
@@ -304,7 +326,11 @@ type ResolvePathType<
304
326
  // If Item is a schema, infer its type.
305
327
  Types.DocumentArray<InferSchemaType<Item>>
306
328
  : Item extends Record<TypeKey, any> ?
307
- Item[TypeKey] extends Function | String ?
329
+ Item[TypeKey] extends Schema ?
330
+ Types.DocumentArray<
331
+ [DiscriminatorEnumType<Item>] extends [never] ? InferSchemaType<Item[TypeKey]> : DiscriminatorEnumType<Item>
332
+ >
333
+ : Item[TypeKey] extends Function | String ?
308
334
  // If Item has a type key that's a string or a callable, it must be a scalar,
309
335
  // so we can directly obtain its path type.
310
336
  ObtainDocumentPathType<Item, TypeKey>[]
@@ -349,4 +375,5 @@ type ResolvePathType<
349
375
  }
350
376
  >
351
377
  : unknown
378
+ : TDiscriminatorEnumType
352
379
  : TypeHint;
@@ -115,6 +115,7 @@ declare module 'mongoose' {
115
115
  }
116
116
 
117
117
  export type FacetPipelineStage = Exclude<PipelineStage, PipelineStage.CollStats | PipelineStage.Facet | PipelineStage.GeoNear | PipelineStage.IndexStats | PipelineStage.Out | PipelineStage.Merge | PipelineStage.PlanCacheStats>;
118
+ export type UnionWithPipelineStage = Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>;
118
119
 
119
120
  export interface GeoNear {
120
121
  /** [`$geoNear` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/geoNear/) */
@@ -303,8 +304,8 @@ declare module 'mongoose' {
303
304
  /** [`$unionWith` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith/) */
304
305
  $unionWith:
305
306
  | string
306
- | { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>[] }
307
- | { coll?: string; pipeline: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>[] }
307
+ | { coll: string; pipeline?: UnionWithPipelineStage[] }
308
+ | { coll?: string; pipeline: UnionWithPipelineStage[] }
308
309
  }
309
310
 
310
311
  export interface Unset {
package/types/query.d.ts CHANGED
@@ -212,7 +212,7 @@ declare module 'mongoose' {
212
212
  _mongooseOptions: QueryOptions<RawDocType>;
213
213
 
214
214
  /**
215
- * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
215
+ * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html).
216
216
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
217
217
  * This is equivalent to calling `.cursor()` with no arguments.
218
218
  */
@@ -296,7 +296,7 @@ declare module 'mongoose' {
296
296
  ): QueryWithHelpers<number, DocType, THelpers, RawDocType, 'countDocuments', TDocOverrides>;
297
297
 
298
298
  /**
299
- * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
299
+ * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html).
300
300
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
301
301
  */
302
302
  cursor(options?: QueryOptions<RawDocType>): Cursor<Unpacked<ResultType>, QueryOptions<RawDocType>>;
@@ -7,6 +7,13 @@ declare module 'mongoose' {
7
7
  currentTime?: () => (NativeDate | number);
8
8
  }
9
9
 
10
+ type SchemaOptionsStaticsPropertyType<TStaticMethods, TModelType> = IfEquals<
11
+ TStaticMethods,
12
+ {},
13
+ Record<string, (...args: any[]) => unknown> & ThisType<TModelType>,
14
+ { [K in keyof TStaticMethods]: OmitThisParameter<TStaticMethods[K]> } & ThisType<TModelType>
15
+ >;
16
+
10
17
  type TypeKeyBaseType = string;
11
18
 
12
19
  type DefaultTypeKey = 'type';
@@ -222,12 +229,7 @@ declare module 'mongoose' {
222
229
  /**
223
230
  * Model Statics methods.
224
231
  */
225
- statics?: IfEquals<
226
- TStaticMethods,
227
- {},
228
- { [name: string]: (this: TModelType, ...args: any[]) => unknown },
229
- AddThisParameter<TStaticMethods, TModelType>
230
- >
232
+ statics?: SchemaOptionsStaticsPropertyType<TStaticMethods, TModelType>
231
233
 
232
234
  /**
233
235
  * Document instance methods.
@@ -212,6 +212,9 @@ declare module 'mongoose' {
212
212
  /** If set, overrides the child schema's `_id` option. Only allowed for subdocuments and subdocument arrays. */
213
213
  _id?: boolean;
214
214
 
215
+ /** Embedded discriminators for this path. Only allowed for subdocuments and subdocument arrays. */
216
+ discriminators?: Record<string, Schema<unknown>>;
217
+
215
218
  /** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */
216
219
  of?: Function | SchemaDefinitionProperty<any> | (Function | SchemaDefinitionProperty<any>)[];
217
220
 
@@ -1,47 +0,0 @@
1
- 'use strict';
2
-
3
- const symbols = require('../schema/symbols');
4
-
5
- /*!
6
- * ignore
7
- */
8
-
9
- module.exports = function validateBeforeSave(schema) {
10
- const unshift = true;
11
- schema.pre('save', false, validateBeforeSavePreSave, null, unshift);
12
- };
13
-
14
- async function validateBeforeSavePreSave(options) {
15
- // Nested docs have their own presave
16
- if (this.$isSubdocument) {
17
- return;
18
- }
19
-
20
- const hasValidateBeforeSaveOption = options &&
21
- (typeof options === 'object') &&
22
- ('validateBeforeSave' in options);
23
-
24
- let shouldValidate;
25
- if (hasValidateBeforeSaveOption) {
26
- shouldValidate = !!options.validateBeforeSave;
27
- } else {
28
- shouldValidate = this.$__schema.options.validateBeforeSave;
29
- }
30
-
31
- // Validate
32
- if (shouldValidate) {
33
- const hasValidateModifiedOnlyOption = options &&
34
- (typeof options === 'object') &&
35
- ('validateModifiedOnly' in options);
36
- const validateOptions = hasValidateModifiedOnlyOption ?
37
- { validateModifiedOnly: options.validateModifiedOnly } :
38
- null;
39
- await this.$validate(validateOptions).then(
40
- () => {
41
- this.$op = 'save';
42
- }
43
- );
44
- }
45
- }
46
-
47
- validateBeforeSavePreSave[symbols.builtInMiddleware] = true;