mongoose 6.2.5 → 6.2.6

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ 6.2.6 / 2022-03-11
2
+ ==================
3
+ * fix(types): correct reference to cursor TypeScript bindings #11513 [SimonHausdorf](https://github.com/SimonHausdorf)
4
+ * fix(types): allow calling Query.prototype.populate() with array of strings #11518
5
+ * fix(types): export and refactor types of PreMiddlewareFunction, PreSaveMiddlewareFunction, PostMiddlewareFunction, ErrorHandlingMiddlewareFunction #11485 [Uzlopak](https://github.com/Uzlopak)
6
+
1
7
  6.2.5 / 2022-03-09
2
8
  ==================
3
9
  * fix(mongoose): add isObjectIdOrHexString() to better capture the most common use case for `isValidObjectId()` #11419
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.2.5",
4
+ "version": "6.2.6",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
File without changes
@@ -0,0 +1,244 @@
1
+ import mongodb = require('mongodb');
2
+
3
+ declare module 'mongoose' {
4
+
5
+ /** A list of paths to skip. If set, Mongoose will validate every modified path that is not in this list. */
6
+ type pathsToSkip = string[] | string;
7
+
8
+ class Document<T = any, TQueryHelpers = any, DocType = any> {
9
+ constructor(doc?: any);
10
+
11
+ /** This documents _id. */
12
+ _id?: T;
13
+
14
+ /** This documents __v. */
15
+ __v?: any;
16
+
17
+ /* Get all subdocs (by bfs) */
18
+ $getAllSubdocs(): Document[];
19
+
20
+ /** Don't run validation on this path or persist changes to this path. */
21
+ $ignore(path: string): void;
22
+
23
+ /** Checks if a path is set to its default. */
24
+ $isDefault(path: string): boolean;
25
+
26
+ /** Getter/setter, determines whether the document was removed or not. */
27
+ $isDeleted(val?: boolean): boolean;
28
+
29
+ /** Returns an array of all populated documents associated with the query */
30
+ $getPopulatedDocs(): Document[];
31
+
32
+ /**
33
+ * Returns true if the given path is nullish or only contains empty objects.
34
+ * Useful for determining whether this subdoc will get stripped out by the
35
+ * [minimize option](/docs/guide.html#minimize).
36
+ */
37
+ $isEmpty(path: string): boolean;
38
+
39
+ /** Checks if a path is invalid */
40
+ $isValid(path: string): boolean;
41
+
42
+ /**
43
+ * Empty object that you can use for storing properties on the document. This
44
+ * is handy for passing data to middleware without conflicting with Mongoose
45
+ * internals.
46
+ */
47
+ $locals: Record<string, unknown>;
48
+
49
+ /** Marks a path as valid, removing existing validation errors. */
50
+ $markValid(path: string): void;
51
+
52
+ /**
53
+ * A string containing the current operation that Mongoose is executing
54
+ * on this document. Can be `null`, `'save'`, `'validate'`, or `'remove'`.
55
+ */
56
+ $op: 'save' | 'validate' | 'remove' | null;
57
+
58
+ /**
59
+ * Getter/setter around the session associated with this document. Used to
60
+ * automatically set `session` if you `save()` a doc that you got from a
61
+ * query with an associated session.
62
+ */
63
+ $session(session?: mongodb.ClientSession | null): mongodb.ClientSession | null;
64
+
65
+ /** Alias for `set()`, used internally to avoid conflicts */
66
+ $set(path: string, val: any, type: any, options?: any): this;
67
+ $set(path: string, val: any, options?: any): this;
68
+ $set(value: any): this;
69
+
70
+ /** Set this property to add additional query filters when Mongoose saves this document and `isNew` is false. */
71
+ $where: Record<string, unknown>;
72
+
73
+ /** If this is a discriminator model, `baseModelName` is the name of the base model. */
74
+ baseModelName?: string;
75
+
76
+ /** Collection the model uses. */
77
+ collection: Collection;
78
+
79
+ /** Connection the model uses. */
80
+ db: Connection;
81
+
82
+ /** Removes this document from the db. */
83
+ delete(options: QueryOptions, callback: Callback): void;
84
+ delete(callback: Callback): void;
85
+ delete(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
86
+
87
+ /** Removes this document from the db. */
88
+ deleteOne(options: QueryOptions, callback: Callback): void;
89
+ deleteOne(callback: Callback): void;
90
+ deleteOne(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
91
+
92
+ /**
93
+ * Takes a populated field and returns it to its unpopulated state. If called with
94
+ * no arguments, then all populated fields are returned to their unpopulated state.
95
+ */
96
+ depopulate(path?: string | string[]): this;
97
+
98
+ /**
99
+ * Returns the list of paths that have been directly modified. A direct
100
+ * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`,
101
+ * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`.
102
+ */
103
+ directModifiedPaths(): Array<string>;
104
+
105
+ /**
106
+ * Returns true if this document is equal to another document.
107
+ *
108
+ * Documents are considered equal when they have matching `_id`s, unless neither
109
+ * document has an `_id`, in which case this function falls back to using
110
+ * `deepEqual()`.
111
+ */
112
+ equals(doc: Document<T>): boolean;
113
+
114
+ /** Returns the current validation errors. */
115
+ errors?: Error.ValidationError;
116
+
117
+ /** Returns the value of a path. */
118
+ get(path: string, type?: any, options?: any): any;
119
+
120
+ /**
121
+ * Returns the changes that happened to the document
122
+ * in the format that will be sent to MongoDB.
123
+ */
124
+ getChanges(): UpdateQuery<this>;
125
+
126
+ /** The string version of this documents _id. */
127
+ id?: any;
128
+
129
+ /** Signal that we desire an increment of this documents version. */
130
+ increment(): this;
131
+
132
+ /**
133
+ * Initializes the document without setters or marking anything modified.
134
+ * Called internally after a document is returned from mongodb. Normally,
135
+ * you do **not** need to call this function on your own.
136
+ */
137
+ init(obj: AnyObject, opts?: AnyObject, callback?: Callback<this>): this;
138
+
139
+ /** Marks a path as invalid, causing validation to fail. */
140
+ invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
141
+
142
+ /** Returns true if `path` was directly set and modified, else false. */
143
+ isDirectModified(path: string): boolean;
144
+
145
+ /** Checks if `path` was explicitly selected. If no projection, always returns true. */
146
+ isDirectSelected(path: string): boolean;
147
+
148
+ /** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */
149
+ isInit(path: string): boolean;
150
+
151
+ /**
152
+ * Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path
153
+ * in this document is modified.
154
+ */
155
+ isModified(path?: string | Array<string>): boolean;
156
+
157
+ /** Boolean flag specifying if the document is new. */
158
+ isNew: boolean;
159
+
160
+ /** Checks if `path` was selected in the source query which initialized this document. */
161
+ isSelected(path: string): boolean;
162
+
163
+ /** Marks the path as having pending changes to write to the db. */
164
+ markModified(path: string, scope?: any): void;
165
+
166
+ /** Returns the list of paths that have been modified. */
167
+ modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
168
+
169
+ /** The name of the model */
170
+ modelName: string;
171
+
172
+ /**
173
+ * Overwrite all values in this document with the values of `obj`, except
174
+ * for immutable properties. Behaves similarly to `set()`, except for it
175
+ * unsets all properties that aren't in `obj`.
176
+ */
177
+ overwrite(obj: AnyObject): this;
178
+
179
+ /**
180
+ * If this document is a subdocument or populated document, returns the
181
+ * document's parent. Returns undefined otherwise.
182
+ */
183
+ $parent(): Document | undefined;
184
+
185
+ /** Populates document references. */
186
+ populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[], callback: Callback<this & Paths>): void;
187
+ populate<Paths = {}>(path: string, names: string, callback: Callback<this & Paths>): void;
188
+ populate<Paths = {}>(path: string, names: string): Promise<this & Paths>;
189
+ populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<this & Paths>;
190
+
191
+ /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
192
+ populated(path: string): any;
193
+
194
+ /** Removes this document from the db. */
195
+ remove(options: QueryOptions, callback: Callback): void;
196
+ remove(callback: Callback): void;
197
+ remove(options?: QueryOptions): Promise<this>;
198
+
199
+ /** Sends a replaceOne command with this document `_id` as the query selector. */
200
+ replaceOne(replacement?: AnyObject, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
201
+
202
+ /** Saves this document by inserting a new document into the database if [document.isNew](/docs/api.html#document_Document-isNew) is `true`, or sends an [updateOne](/docs/api.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`. */
203
+ save(options: SaveOptions, callback: Callback<this>): void;
204
+ save(callback: Callback<this>): void;
205
+ save(options?: SaveOptions): Promise<this>;
206
+
207
+ /** The document's schema. */
208
+ schema: Schema;
209
+
210
+ /** Sets the value of a path, or many paths. */
211
+ set(path: string, val: any, type: any, options?: any): this;
212
+ set(path: string, val: any, options?: any): this;
213
+ set(value: any): this;
214
+
215
+ /** The return value of this method is used in calls to JSON.stringify(doc). */
216
+ toJSON(options: ToObjectOptions & { flattenMaps: false }): LeanDocument<this>;
217
+ toJSON(options?: ToObjectOptions): FlattenMaps<LeanDocument<this>>;
218
+ toJSON<T = FlattenMaps<DocType>>(options?: ToObjectOptions): T;
219
+
220
+ /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
221
+ toObject(options?: ToObjectOptions): LeanDocument<this>;
222
+ toObject<T = DocType>(options?: ToObjectOptions): T;
223
+
224
+ /** Clears the modified state on the specified path. */
225
+ unmarkModified(path: string): void;
226
+
227
+ /** Sends an update command with this document `_id` as the query selector. */
228
+ update(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
229
+
230
+ /** Sends an updateOne command with this document `_id` as the query selector. */
231
+ updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
232
+
233
+ /** Executes registered validation rules for this document. */
234
+ validate(pathsToValidate: pathsToValidate, options: AnyObject, callback: CallbackWithoutResult): void;
235
+ validate(pathsToValidate: pathsToValidate, callback: CallbackWithoutResult): void;
236
+ validate(callback: CallbackWithoutResult): void;
237
+ validate(pathsToValidate?: pathsToValidate, options?: AnyObject): Promise<void>;
238
+ validate(options: { pathsToSkip?: pathsToSkip }): Promise<void>;
239
+
240
+ /** Executes registered validation rules (skipping asynchronous validators) for this document. */
241
+ validateSync(options: { pathsToSkip?: pathsToSkip, [k: string]: any }): Error.ValidationError | null;
242
+ validateSync(pathsToValidate?: Array<string>, options?: AnyObject): Error.ValidationError | null;
243
+ }
244
+ }
package/types/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /// <reference path="./connection.d.ts" />
2
- /// <reference path="./cursor.ts" />
2
+ /// <reference path="./cursor.d.ts" />
3
+ /// <reference path="./document.d.ts" />
3
4
  /// <reference path="./error.d.ts" />
4
5
  /// <reference path="./pipelinestage.d.ts" />
5
6
  /// <reference path="./schemaoptions.d.ts" />
@@ -331,247 +332,8 @@ declare module 'mongoose' {
331
332
  getIndexes(): any;
332
333
  }
333
334
 
334
- class Document<T = any, TQueryHelpers = any, DocType = any> {
335
- constructor(doc?: any);
336
-
337
- /** This documents _id. */
338
- _id?: T;
339
-
340
- /** This documents __v. */
341
- __v?: any;
342
-
343
- /* Get all subdocs (by bfs) */
344
- $getAllSubdocs(): Document[];
345
-
346
- /** Don't run validation on this path or persist changes to this path. */
347
- $ignore(path: string): void;
348
-
349
- /** Checks if a path is set to its default. */
350
- $isDefault(path: string): boolean;
351
-
352
- /** Getter/setter, determines whether the document was removed or not. */
353
- $isDeleted(val?: boolean): boolean;
354
-
355
- /** Returns an array of all populated documents associated with the query */
356
- $getPopulatedDocs(): Document[];
357
-
358
- /**
359
- * Returns true if the given path is nullish or only contains empty objects.
360
- * Useful for determining whether this subdoc will get stripped out by the
361
- * [minimize option](/docs/guide.html#minimize).
362
- */
363
- $isEmpty(path: string): boolean;
364
-
365
- /** Checks if a path is invalid */
366
- $isValid(path: string): boolean;
367
-
368
- /**
369
- * Empty object that you can use for storing properties on the document. This
370
- * is handy for passing data to middleware without conflicting with Mongoose
371
- * internals.
372
- */
373
- $locals: Record<string, unknown>;
374
-
375
- /** Marks a path as valid, removing existing validation errors. */
376
- $markValid(path: string): void;
377
-
378
- /**
379
- * A string containing the current operation that Mongoose is executing
380
- * on this document. May be `null`, `'save'`, `'validate'`, or `'remove'`.
381
- */
382
- $op: string | null;
383
-
384
- /**
385
- * Getter/setter around the session associated with this document. Used to
386
- * automatically set `session` if you `save()` a doc that you got from a
387
- * query with an associated session.
388
- */
389
- $session(session?: mongodb.ClientSession | null): mongodb.ClientSession;
390
-
391
- /** Alias for `set()`, used internally to avoid conflicts */
392
- $set(path: string, val: any, options?: any): this;
393
- $set(path: string, val: any, type: any, options?: any): this;
394
- $set(value: any): this;
395
-
396
- /** Set this property to add additional query filters when Mongoose saves this document and `isNew` is false. */
397
- $where: Record<string, unknown>;
398
-
399
- /** If this is a discriminator model, `baseModelName` is the name of the base model. */
400
- baseModelName?: string;
401
-
402
- /** Collection the model uses. */
403
- collection: Collection;
404
-
405
- /** Connection the model uses. */
406
- db: Connection;
407
-
408
- /** Removes this document from the db. */
409
- delete(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
410
- delete(options: QueryOptions, cb?: Callback): void;
411
- delete(cb: Callback): void;
412
-
413
- /** Removes this document from the db. */
414
- deleteOne(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
415
- deleteOne(options: QueryOptions, cb?: Callback): void;
416
- deleteOne(cb: Callback): void;
417
-
418
- /**
419
- * Takes a populated field and returns it to its unpopulated state. If called with
420
- * no arguments, then all populated fields are returned to their unpopulated state.
421
- */
422
- depopulate(path?: string | string[]): this;
423
-
424
- /**
425
- * Returns the list of paths that have been directly modified. A direct
426
- * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`,
427
- * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`.
428
- */
429
- directModifiedPaths(): Array<string>;
430
-
431
- /**
432
- * Returns true if this document is equal to another document.
433
- *
434
- * Documents are considered equal when they have matching `_id`s, unless neither
435
- * document has an `_id`, in which case this function falls back to using
436
- * `deepEqual()`.
437
- */
438
- equals(doc: Document<T>): boolean;
439
-
440
- /** Hash containing current validation errors. */
441
- errors?: Error.ValidationError;
442
-
443
- /** Returns the value of a path. */
444
- get(path: string, type?: any, options?: any): any;
445
-
446
- /**
447
- * Returns the changes that happened to the document
448
- * in the format that will be sent to MongoDB.
449
- */
450
- getChanges(): UpdateQuery<this>;
451
-
452
- /** The string version of this documents _id. */
453
- id?: any;
454
-
455
- /** Signal that we desire an increment of this documents version. */
456
- increment(): this;
457
-
458
- /**
459
- * Initializes the document without setters or marking anything modified.
460
- * Called internally after a document is returned from mongodb. Normally,
461
- * you do **not** need to call this function on your own.
462
- */
463
- init(obj: any, opts?: any, cb?: Callback<this>): this;
464
-
465
- /** Marks a path as invalid, causing validation to fail. */
466
- invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
467
-
468
- /** Returns true if `path` was directly set and modified, else false. */
469
- isDirectModified(path: string): boolean;
470
-
471
- /** Checks if `path` was explicitly selected. If no projection, always returns true. */
472
- isDirectSelected(path: string): boolean;
473
-
474
- /** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */
475
- isInit(path: string): boolean;
476
-
477
- /**
478
- * Returns true if any of the given paths is modified, else false. If no arguments, returns `true` if any path
479
- * in this document is modified.
480
- */
481
- isModified(path?: string | Array<string>): boolean;
482
-
483
- /** Boolean flag specifying if the document is new. */
484
- isNew: boolean;
485
-
486
- /** Checks if `path` was selected in the source query which initialized this document. */
487
- isSelected(path: string): boolean;
488
-
489
- /** Marks the path as having pending changes to write to the db. */
490
- markModified(path: string, scope?: any): void;
491
-
492
- /** Returns the list of paths that have been modified. */
493
- modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
494
-
495
- /** The name of the model */
496
- modelName: string;
497
-
498
- /**
499
- * Overwrite all values in this document with the values of `obj`, except
500
- * for immutable properties. Behaves similarly to `set()`, except for it
501
- * unsets all properties that aren't in `obj`.
502
- */
503
- overwrite(obj: AnyObject): this;
504
-
505
- /**
506
- * If this document is a subdocument or populated document, returns the
507
- * document's parent. Returns undefined otherwise.
508
- */
509
- $parent(): Document | undefined;
510
-
511
- /** Populates document references. */
512
- populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<this & Paths>;
513
- populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[], callback: Callback<this & Paths>): void;
514
- populate<Paths = {}>(path: string, names: string): Promise<this & Paths>;
515
- populate<Paths = {}>(path: string, names: string, callback: Callback<this & Paths>): void;
516
-
517
- /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
518
- populated(path: string): any;
519
-
520
- /** Removes this document from the db. */
521
- remove(options?: QueryOptions): Promise<this>;
522
- remove(options?: QueryOptions, cb?: Callback): void;
523
-
524
- /** Sends a replaceOne command with this document `_id` as the query selector. */
525
- replaceOne(replacement?: AnyObject, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
526
- replaceOne(replacement?: AnyObject, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
527
-
528
- /** Saves this document by inserting a new document into the database if [document.isNew](/docs/api.html#document_Document-isNew) is `true`, or sends an [updateOne](/docs/api.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`. */
529
- save(options?: SaveOptions): Promise<this>;
530
- save(options?: SaveOptions, fn?: Callback<this>): void;
531
- save(fn?: Callback<this>): void;
532
-
533
- /** The document's schema. */
534
- schema: Schema;
535
-
536
- /** Sets the value of a path, or many paths. */
537
- set(path: string, val: any, options?: any): this;
538
- set(path: string, val: any, type: any, options?: any): this;
539
- set(value: any): this;
540
-
541
- /** The return value of this method is used in calls to JSON.stringify(doc). */
542
- toJSON(options: ToObjectOptions & { flattenMaps: false }): LeanDocument<this>;
543
- toJSON(options?: ToObjectOptions): FlattenMaps<LeanDocument<this>>;
544
- toJSON<T = FlattenMaps<DocType>>(options?: ToObjectOptions): T;
545
-
546
- /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
547
- toObject(options?: ToObjectOptions): LeanDocument<this>;
548
- toObject<T = DocType>(options?: ToObjectOptions): T;
549
-
550
- /** Clears the modified state on the specified path. */
551
- unmarkModified(path: string): void;
552
-
553
- /** Sends an update command with this document `_id` as the query selector. */
554
- update(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
555
-
556
- /** Sends an updateOne command with this document `_id` as the query selector. */
557
- updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
558
-
559
- /** Executes registered validation rules for this document. */
560
- validate(options:{ pathsToSkip?: pathsToSkip }): Promise<void>;
561
- validate(pathsToValidate?: pathsToValidate, options?: any): Promise<void>;
562
- validate(callback: CallbackWithoutResult): void;
563
- validate(pathsToValidate: pathsToValidate, callback: CallbackWithoutResult): void;
564
- validate(pathsToValidate: pathsToValidate, options: any, callback: CallbackWithoutResult): void;
565
-
566
- /** Executes registered validation rules (skipping asynchronous validators) for this document. */
567
- validateSync(options:{pathsToSkip?: pathsToSkip, [k:string]: any }): Error.ValidationError | null;
568
- validateSync(pathsToValidate?: Array<string>, options?: any): Error.ValidationError | null;
569
- }
570
-
571
335
  /** A list of paths to validate. If set, Mongoose will validate only the modified paths that are in the given list. */
572
336
  type pathsToValidate = string[] | string;
573
- /** A list of paths to skip. If set, Mongoose will validate every modified path that is not in this list. */
574
- type pathsToSkip = string[] | string;
575
337
 
576
338
  interface AcceptsDiscriminator {
577
339
  /** Adds a discriminator type. */
@@ -1101,10 +863,10 @@ declare module 'mongoose' {
1101
863
  type IndexDirection = 1 | -1 | '2d' | '2dsphere' | 'geoHaystack' | 'hashed' | 'text';
1102
864
  type IndexDefinition = Record<string, IndexDirection>;
1103
865
 
1104
- type PreMiddlewareFunction<T> = (this: T, next: (err?: CallbackError) => void) => void | Promise<void>;
1105
- type PreSaveMiddlewareFunction<T> = (this: T, next: (err?: CallbackError) => void, opts: SaveOptions) => void | Promise<void>;
1106
- type PostMiddlewareFunction<ThisType, ResType = any> = (this: ThisType, res: ResType, next: (err?: CallbackError) => void) => void | Promise<void>;
1107
- type ErrorHandlingMiddlewareFunction<ThisType, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: (err?: CallbackError) => void) => void;
866
+ export type PreMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
867
+ export type PreSaveMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise<void>;
868
+ export type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
869
+ export type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
1108
870
 
1109
871
  class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = any, TQueryHelpers = any> extends events.EventEmitter {
1110
872
  /**
@@ -1814,8 +1576,11 @@ declare module 'mongoose' {
1814
1576
 
1815
1577
  type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;
1816
1578
 
1817
- type UnpackedIntersection<T, U> = T extends (infer V)[] ? (V & U)[] : T & U;
1818
- type UnpackedIntersectionWithNull<T, U> = T extends null ? UnpackedIntersection<T, U> | null : UnpackedIntersection<T, U>;
1579
+ type UnpackedIntersection<T, U> = T extends (infer A)[]
1580
+ ? (Omit<A, keyof U> & U)[]
1581
+ : keyof U extends never
1582
+ ? T
1583
+ : Omit<T, keyof U> & U;
1819
1584
 
1820
1585
  type ProjectionFields<DocType> = {[Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any} & Record<string, any>;
1821
1586
 
@@ -2116,8 +1881,8 @@ declare module 'mongoose' {
2116
1881
  polygon(path: string, ...coordinatePairs: number[][]): this;
2117
1882
 
2118
1883
  /** Specifies paths which should be populated with other documents. */
2119
- populate<Paths = {}>(path: string, select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersectionWithNull<ResultType, Paths>, DocType, THelpers, RawDocType>;
2120
- populate<Paths = {}>(options: PopulateOptions | Array<PopulateOptions>): QueryWithHelpers<UnpackedIntersectionWithNull<ResultType, Paths>, DocType, THelpers, RawDocType>;
1884
+ populate<Paths = {}>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
1885
+ populate<Paths = {}>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
2121
1886
 
2122
1887
  /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
2123
1888
  projection(): ProjectionFields<DocType> | null;
@@ -2704,6 +2469,7 @@ declare module 'mongoose' {
2704
2469
  type Callback<T = any> = (error: CallbackError, result: T) => void;
2705
2470
 
2706
2471
  type CallbackWithoutResult = (error: CallbackError) => void;
2472
+ type CallbackWithoutResultAndOptionalError = (error?: CallbackError) => void;
2707
2473
 
2708
2474
  /* for ts-mongoose */
2709
2475
  class mquery {}