mongoose 8.2.4 → 8.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.
package/types/index.d.ts CHANGED
@@ -27,6 +27,7 @@
27
27
  declare class NativeDate extends global.Date { }
28
28
 
29
29
  declare module 'mongoose' {
30
+ import Kareem = require('kareem');
30
31
  import events = require('events');
31
32
  import mongodb = require('mongodb');
32
33
  import mongoose = require('mongoose');
@@ -397,7 +398,6 @@ declare module 'mongoose' {
397
398
  ): this;
398
399
  // this = Document
399
400
  pre<T = THydratedDocumentType>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
400
- pre<T = THydratedDocumentType>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
401
401
  pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this;
402
402
  pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
403
403
  pre<T = THydratedDocumentType>(
@@ -415,7 +415,6 @@ declare module 'mongoose' {
415
415
  pre<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
416
416
  // method aggregate
417
417
  pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
418
- pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
419
418
  /* method insertMany */
420
419
  pre<T = TModelType>(
421
420
  method: 'insertMany' | RegExp,
@@ -426,16 +425,6 @@ declare module 'mongoose' {
426
425
  options?: InsertManyOptions & { lean?: boolean }
427
426
  ) => void | Promise<void>
428
427
  ): this;
429
- pre<T = TModelType>(
430
- method: 'insertMany' | RegExp,
431
- options: SchemaPreOptions,
432
- fn: (
433
- this: T,
434
- next: (err?: CallbackError) => void,
435
- docs: any | Array<any>,
436
- options?: InsertManyOptions & { lean?: boolean }
437
- ) => void | Promise<void>
438
- ): this;
439
428
  /* method bulkWrite */
440
429
  pre<T = TModelType>(
441
430
  method: 'bulkWrite' | RegExp,
@@ -446,16 +435,6 @@ declare module 'mongoose' {
446
435
  options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
447
436
  ) => void | Promise<void>
448
437
  ): this;
449
- pre<T = TModelType>(
450
- method: 'bulkWrite' | RegExp,
451
- options: SchemaPreOptions,
452
- fn: (
453
- this: T,
454
- next: (err?: CallbackError) => void,
455
- ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
456
- options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
457
- ) => void | Promise<void>
458
- ): this;
459
438
  /* method createCollection */
460
439
  pre<T = TModelType>(
461
440
  method: 'createCollection' | RegExp,
@@ -465,15 +444,6 @@ declare module 'mongoose' {
465
444
  options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
466
445
  ) => void | Promise<void>
467
446
  ): this;
468
- pre<T = TModelType>(
469
- method: 'createCollection' | RegExp,
470
- options: SchemaPreOptions,
471
- fn: (
472
- this: T,
473
- next: (err?: CallbackError) => void,
474
- options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
475
- ) => void | Promise<void>
476
- ): this;
477
447
 
478
448
  /** Object of currently defined query helpers on this schema. */
479
449
  query: TQueryHelpers;
@@ -722,5 +692,9 @@ declare module 'mongoose' {
722
692
  /* for ts-mongoose */
723
693
  export class mquery { }
724
694
 
695
+ export function overwriteMiddlewareResult(val: any): Kareem.OverwriteMiddlewareResult;
696
+
697
+ export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction;
698
+
725
699
  export default mongoose;
726
700
  }
@@ -1,4 +1,5 @@
1
1
  declare module 'mongoose' {
2
+ import Kareem = require('kareem');
2
3
 
3
4
  type MongooseQueryAndDocumentMiddleware = 'updateOne' | 'deleteOne';
4
5
 
@@ -37,13 +38,13 @@ declare module 'mongoose' {
37
38
  this: ThisType,
38
39
  next: CallbackWithoutResultAndOptionalError,
39
40
  opts?: Record<string, any>
40
- ) => void | Promise<void>;
41
+ ) => void | Promise<void> | Kareem.SkipWrappedFunction;
41
42
  type PreSaveMiddlewareFunction<ThisType = any> = (
42
43
  this: ThisType,
43
44
  next: CallbackWithoutResultAndOptionalError,
44
45
  opts: SaveOptions
45
- ) => void | Promise<void>;
46
- type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
46
+ ) => void | Promise<void> | Kareem.SkipWrappedFunction;
47
+ type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;
47
48
  type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
48
- type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
49
+ type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;
49
50
  }
package/types/query.d.ts CHANGED
@@ -741,7 +741,10 @@ declare module 'mongoose' {
741
741
  slice(val: number | Array<number>): this;
742
742
 
743
743
  /** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */
744
- sort(arg?: string | { [key: string]: SortOrder | { $meta: any } } | [string, SortOrder][] | undefined | null): this;
744
+ sort(
745
+ arg?: string | { [key: string]: SortOrder | { $meta: any } } | [string, SortOrder][] | undefined | null,
746
+ options?: { override?: boolean }
747
+ ): this;
745
748
 
746
749
  /** Sets the tailable option (for use with capped collections). */
747
750
  tailable(bool?: boolean, opts?: {
@@ -192,10 +192,15 @@ declare module 'mongoose' {
192
192
  [other: string]: any;
193
193
  }
194
194
 
195
- interface Validator {
196
- message?: string; type?: string; validator?: Function
195
+ interface Validator<DocType = any> {
196
+ message?: string | ((props: ValidatorProps) => string);
197
+ type?: string;
198
+ validator?: ValidatorFunction<DocType>;
199
+ reason?: Error;
197
200
  }
198
201
 
202
+ type ValidatorFunction<DocType = any> = (this: DocType, value: any, validatorProperties?: Validator) => any;
203
+
199
204
  class SchemaType<T = any, DocType = any> {
200
205
  /** SchemaType constructor */
201
206
  constructor(path: string, options?: AnyObject, instance?: string);
@@ -281,7 +286,10 @@ declare module 'mongoose' {
281
286
  validators: Validator[];
282
287
 
283
288
  /** Adds validator(s) for this document path. */
284
- validate(obj: RegExp | ((this: DocType, value: any, validatorProperties?: Validator) => any), errorMsg?: string, type?: string): this;
289
+ validate(obj: RegExp | ValidatorFunction<DocType> | Validator<DocType>, errorMsg?: string, type?: string): this;
290
+
291
+ /** Adds multiple validators for this document path. */
292
+ validateAll(validators: Array<RegExp | ValidatorFunction<DocType> | Validator<DocType>>): this;
285
293
 
286
294
  /** Default options for this SchemaType */
287
295
  defaultOptions?: Record<string, any>;
@@ -6,6 +6,7 @@ declare module 'mongoose' {
6
6
  path: string;
7
7
  fullPath: string;
8
8
  value: any;
9
+ reason?: Error;
9
10
  }
10
11
 
11
12
  interface ValidatorMessageFn {