mongoose 8.16.4 → 8.17.0

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/models.d.ts CHANGED
@@ -23,7 +23,14 @@ declare module 'mongoose' {
23
23
  ): U;
24
24
  }
25
25
 
26
- interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
26
+ export type MongooseBulkWriteResult = mongodb.BulkWriteResult & {
27
+ mongoose?: {
28
+ validationErrors: Error[],
29
+ results: Array<Error | mongodb.WriteError | null>
30
+ }
31
+ };
32
+
33
+ export interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
27
34
  session?: ClientSession;
28
35
  skipValidation?: boolean;
29
36
  throwOnValidationError?: boolean;
@@ -266,7 +273,6 @@ declare module 'mongoose' {
266
273
  THydratedDocumentType = HydratedDocument<TRawDocType, TVirtuals & TInstanceMethods, TQueryHelpers, TVirtuals>,
267
274
  TSchema = any> extends
268
275
  NodeJS.EventEmitter,
269
- AcceptsDiscriminator,
270
276
  IndexManager,
271
277
  SessionStarter {
272
278
  new <DocType = Partial<TRawDocType>>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): THydratedDocumentType;
@@ -306,20 +312,20 @@ declare module 'mongoose' {
306
312
  * round trip to the MongoDB server.
307
313
  */
308
314
  bulkWrite<DocContents = TRawDocType>(
309
- writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
315
+ writes: Array<AnyBulkWriteOperation<DocContents>>,
310
316
  options: MongooseBulkWriteOptions & { ordered: false }
311
- ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[], results: Array<Error | mongodb.WriteError | null> } }>;
317
+ ): Promise<MongooseBulkWriteResult>;
312
318
  bulkWrite<DocContents = TRawDocType>(
313
- writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
319
+ writes: Array<AnyBulkWriteOperation<DocContents>>,
314
320
  options?: MongooseBulkWriteOptions
315
- ): Promise<mongodb.BulkWriteResult>;
321
+ ): Promise<MongooseBulkWriteResult>;
316
322
 
317
323
  /**
318
324
  * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than
319
325
  * sending multiple `save()` calls because with `bulkSave()` there is only one
320
326
  * network round trip to the MongoDB server.
321
327
  */
322
- bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<mongodb.BulkWriteResult>;
328
+ bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<MongooseBulkWriteResult>;
323
329
 
324
330
  /** Collection the model uses. */
325
331
  collection: Collection;
@@ -327,7 +333,7 @@ declare module 'mongoose' {
327
333
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
328
334
  countDocuments(
329
335
  filter?: RootFilterQuery<TRawDocType>,
330
- options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType>) | null
336
+ options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType> & mongodb.Abortable) | null
331
337
  ): QueryWithHelpers<
332
338
  number,
333
339
  THydratedDocumentType,
@@ -419,6 +425,28 @@ declare module 'mongoose' {
419
425
  TInstanceMethods & TVirtuals
420
426
  >;
421
427
 
428
+ /** Adds a discriminator type. */
429
+ discriminator<TDiscriminatorSchema extends Schema<any, any>>(
430
+ name: string | number,
431
+ schema: TDiscriminatorSchema,
432
+ value?: string | number | ObjectId | DiscriminatorOptions
433
+ ): Model<
434
+ TRawDocType & InferSchemaType<TDiscriminatorSchema>,
435
+ TQueryHelpers & ObtainSchemaGeneric<TDiscriminatorSchema, 'TQueryHelpers'>,
436
+ TInstanceMethods & ObtainSchemaGeneric<TDiscriminatorSchema, 'TInstanceMethods'>,
437
+ TVirtuals & ObtainSchemaGeneric<TDiscriminatorSchema, 'TVirtuals'>
438
+ > & ObtainSchemaGeneric<TDiscriminatorSchema, 'TStaticMethods'>;
439
+ discriminator<D>(
440
+ name: string | number,
441
+ schema: Schema,
442
+ value?: string | number | ObjectId | DiscriminatorOptions
443
+ ): Model<D>;
444
+ discriminator<T, U>(
445
+ name: string | number,
446
+ schema: Schema<T, U>,
447
+ value?: string | number | ObjectId | DiscriminatorOptions
448
+ ): U;
449
+
422
450
  /**
423
451
  * Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name.
424
452
  * This function only works when connected to MongoDB Atlas.
@@ -462,7 +490,7 @@ declare module 'mongoose' {
462
490
  findOne<ResultDoc = THydratedDocumentType>(
463
491
  filter: RootFilterQuery<TRawDocType>,
464
492
  projection: ProjectionType<TRawDocType> | null | undefined,
465
- options: QueryOptions<TRawDocType> & { lean: true }
493
+ options: QueryOptions<TRawDocType> & { lean: true } & mongodb.Abortable
466
494
  ): QueryWithHelpers<
467
495
  GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
468
496
  ResultDoc,
@@ -474,7 +502,7 @@ declare module 'mongoose' {
474
502
  findOne<ResultDoc = THydratedDocumentType>(
475
503
  filter?: RootFilterQuery<TRawDocType>,
476
504
  projection?: ProjectionType<TRawDocType> | null,
477
- options?: QueryOptions<TRawDocType> | null
505
+ options?: QueryOptions<TRawDocType> & mongodb.Abortable | null
478
506
  ): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne', TInstanceMethods & TVirtuals>;
479
507
  findOne<ResultDoc = THydratedDocumentType>(
480
508
  filter?: RootFilterQuery<TRawDocType>,
@@ -693,7 +721,7 @@ declare module 'mongoose' {
693
721
  find<ResultDoc = THydratedDocumentType>(
694
722
  filter: RootFilterQuery<TRawDocType>,
695
723
  projection: ProjectionType<TRawDocType> | null | undefined,
696
- options: QueryOptions<TRawDocType> & { lean: true }
724
+ options: QueryOptions<TRawDocType> & { lean: true } & mongodb.Abortable
697
725
  ): QueryWithHelpers<
698
726
  GetLeanResultType<TRawDocType, TRawDocType[], 'find'>,
699
727
  ResultDoc,
@@ -705,7 +733,7 @@ declare module 'mongoose' {
705
733
  find<ResultDoc = THydratedDocumentType>(
706
734
  filter: RootFilterQuery<TRawDocType>,
707
735
  projection?: ProjectionType<TRawDocType> | null | undefined,
708
- options?: QueryOptions<TRawDocType> | null | undefined
736
+ options?: QueryOptions<TRawDocType> & mongodb.Abortable | null | undefined
709
737
  ): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>;
710
738
  find<ResultDoc = THydratedDocumentType>(
711
739
  filter: RootFilterQuery<TRawDocType>,
@@ -878,7 +906,7 @@ declare module 'mongoose' {
878
906
  replaceOne<ResultDoc = THydratedDocumentType>(
879
907
  filter?: RootFilterQuery<TRawDocType>,
880
908
  replacement?: TRawDocType | AnyObject,
881
- options?: (mongodb.ReplaceOptions & MongooseQueryOptions<TRawDocType>) | null
909
+ options?: (mongodb.ReplaceOptions & QueryOptions<TRawDocType>) | null
882
910
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne', TInstanceMethods & TVirtuals>;
883
911
 
884
912
  /** Apply changes made to this model's schema after this model was compiled. */
package/types/query.d.ts CHANGED
@@ -31,19 +31,9 @@ declare module 'mongoose' {
31
31
  | 'strictQuery'
32
32
  | 'translateAliases';
33
33
 
34
- type MongooseQueryOptions<
35
- DocType = unknown,
36
- Keys extends keyof QueryOptions<DocType> = MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'
37
- > = Pick<QueryOptions<DocType>, Keys> & {
38
- [other: string]: any;
39
- };
34
+ type MongooseBaseQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys>;
40
35
 
41
- type MongooseBaseQueryOptions<DocType = unknown> = MongooseQueryOptions<DocType, MongooseBaseQueryOptionKeys>;
42
-
43
- type MongooseUpdateQueryOptions<DocType = unknown> = MongooseQueryOptions<
44
- DocType,
45
- MongooseBaseQueryOptionKeys | 'timestamps'
46
- >;
36
+ type MongooseUpdateQueryOptions<DocType = unknown> = Pick<QueryOptions<DocType>, MongooseBaseQueryOptionKeys | 'timestamps'>;
47
37
 
48
38
  type ProjectionFields<DocType> = { [Key in keyof DocType]?: any } & Record<string, any>;
49
39
 
@@ -239,7 +229,7 @@ declare module 'mongoose' {
239
229
  : MergeType<ResultType, Paths>;
240
230
 
241
231
  class Query<ResultType, DocType, THelpers = {}, RawDocType = unknown, QueryOp = 'find', TDocOverrides = Record<string, never>> implements SessionOperation {
242
- _mongooseOptions: MongooseQueryOptions<DocType>;
232
+ _mongooseOptions: QueryOptions<DocType>;
243
233
 
244
234
  /**
245
235
  * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
@@ -634,7 +624,7 @@ declare module 'mongoose' {
634
624
  * Getter/setter around the current mongoose-specific options for this query
635
625
  * Below are the current Mongoose-specific options.
636
626
  */
637
- mongooseOptions(val?: MongooseQueryOptions): MongooseQueryOptions;
627
+ mongooseOptions(val?: QueryOptions<DocType>): QueryOptions<DocType>;
638
628
 
639
629
  /** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
640
630
  ne<K = string>(path: K, val: any): this;
@@ -268,6 +268,9 @@ declare module 'mongoose' {
268
268
  /** Array containing default setters for all instances of this SchemaType */
269
269
  static setters: ((val?: unknown, priorVal?: unknown, doc?: Document<unknown>, options?: Record<string, any> | null) => unknown)[];
270
270
 
271
+ /** Contains the handlers for different query operators for this schema type. */
272
+ $conditionalHandlers: { [op: string]: (val: any, context: any) => any };
273
+
271
274
  /** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */
272
275
  OptionsConstructor: SchemaTypeOptions<T>;
273
276
 
@@ -350,7 +353,7 @@ declare module 'mongoose' {
350
353
  validateAll(validators: Array<RegExp | ValidatorFunction<DocType> | Validator<DocType>>): this;
351
354
 
352
355
  /** Default options for this SchemaType */
353
- defaultOptions?: Record<string, any>;
356
+ static defaultOptions?: Record<string, any>;
354
357
  }
355
358
 
356
359
  namespace Schema {
@@ -368,7 +371,7 @@ declare module 'mongoose' {
368
371
  caster?: SchemaType;
369
372
 
370
373
  /** Default options for this SchemaType */
371
- defaultOptions: Record<string, any>;
374
+ static defaultOptions: Record<string, any>;
372
375
 
373
376
  /**
374
377
  * Adds an enum validator if this is an array of strings or numbers. Equivalent to
@@ -382,7 +385,7 @@ declare module 'mongoose' {
382
385
  static schemaName: 'BigInt';
383
386
 
384
387
  /** Default options for this SchemaType */
385
- defaultOptions: Record<string, any>;
388
+ static defaultOptions: Record<string, any>;
386
389
  }
387
390
 
388
391
  class Boolean extends SchemaType {
@@ -396,7 +399,7 @@ declare module 'mongoose' {
396
399
  static convertToFalse: Set<any>;
397
400
 
398
401
  /** Default options for this SchemaType */
399
- defaultOptions: Record<string, any>;
402
+ static defaultOptions: Record<string, any>;
400
403
  }
401
404
 
402
405
  class Buffer extends SchemaType {
@@ -410,7 +413,7 @@ declare module 'mongoose' {
410
413
  subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): this;
411
414
 
412
415
  /** Default options for this SchemaType */
413
- defaultOptions: Record<string, any>;
416
+ static defaultOptions: Record<string, any>;
414
417
  }
415
418
 
416
419
  class Date extends SchemaType {
@@ -427,7 +430,7 @@ declare module 'mongoose' {
427
430
  min(value: NativeDate, message?: string): this;
428
431
 
429
432
  /** Default options for this SchemaType */
430
- defaultOptions: Record<string, any>;
433
+ static defaultOptions: Record<string, any>;
431
434
  }
432
435
 
433
436
  class Decimal128 extends SchemaType {
@@ -435,7 +438,7 @@ declare module 'mongoose' {
435
438
  static schemaName: 'Decimal128';
436
439
 
437
440
  /** Default options for this SchemaType */
438
- defaultOptions: Record<string, any>;
441
+ static defaultOptions: Record<string, any>;
439
442
  }
440
443
 
441
444
  class Int32 extends SchemaType {
@@ -443,7 +446,7 @@ declare module 'mongoose' {
443
446
  static schemaName: 'Int32';
444
447
 
445
448
  /** Default options for this SchemaType */
446
- defaultOptions: Record<string, any>;
449
+ static defaultOptions: Record<string, any>;
447
450
  }
448
451
 
449
452
  class DocumentArray extends SchemaType implements AcceptsDiscriminator {
@@ -452,6 +455,8 @@ declare module 'mongoose' {
452
455
 
453
456
  static options: { castNonArrays: boolean; };
454
457
 
458
+ schemaOptions?: SchemaOptions;
459
+
455
460
  discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
456
461
  discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
457
462
 
@@ -462,7 +467,7 @@ declare module 'mongoose' {
462
467
  caster?: typeof Types.Subdocument;
463
468
 
464
469
  /** Default options for this SchemaType */
465
- defaultOptions: Record<string, any>;
470
+ static defaultOptions: Record<string, any>;
466
471
  }
467
472
 
468
473
  class Map extends SchemaType {
@@ -470,7 +475,7 @@ declare module 'mongoose' {
470
475
  static schemaName: 'Map';
471
476
 
472
477
  /** Default options for this SchemaType */
473
- defaultOptions: Record<string, any>;
478
+ static defaultOptions: Record<string, any>;
474
479
  }
475
480
 
476
481
  class Mixed extends SchemaType {
@@ -478,7 +483,7 @@ declare module 'mongoose' {
478
483
  static schemaName: 'Mixed';
479
484
 
480
485
  /** Default options for this SchemaType */
481
- defaultOptions: Record<string, any>;
486
+ static defaultOptions: Record<string, any>;
482
487
  }
483
488
 
484
489
  class Number extends SchemaType {
@@ -495,7 +500,7 @@ declare module 'mongoose' {
495
500
  min(value: number, message?: string): this;
496
501
 
497
502
  /** Default options for this SchemaType */
498
- defaultOptions: Record<string, any>;
503
+ static defaultOptions: Record<string, any>;
499
504
  }
500
505
 
501
506
  class Double extends SchemaType {
@@ -503,7 +508,7 @@ declare module 'mongoose' {
503
508
  static schemaName: 'Double';
504
509
 
505
510
  /** Default options for this SchemaType */
506
- defaultOptions: Record<string, any>;
511
+ static defaultOptions: Record<string, any>;
507
512
  }
508
513
 
509
514
  class ObjectId extends SchemaType {
@@ -514,7 +519,7 @@ declare module 'mongoose' {
514
519
  auto(turnOn: boolean): this;
515
520
 
516
521
  /** Default options for this SchemaType */
517
- defaultOptions: Record<string, any>;
522
+ static defaultOptions: Record<string, any>;
518
523
  }
519
524
 
520
525
  class Subdocument<DocType = unknown> extends SchemaType implements AcceptsDiscriminator {
@@ -525,7 +530,7 @@ declare module 'mongoose' {
525
530
  schema: Schema;
526
531
 
527
532
  /** Default options for this SchemaType */
528
- defaultOptions: Record<string, any>;
533
+ static defaultOptions: Record<string, any>;
529
534
 
530
535
  discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
531
536
  discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
@@ -559,7 +564,7 @@ declare module 'mongoose' {
559
564
  uppercase(shouldApply?: boolean): this;
560
565
 
561
566
  /** Default options for this SchemaType */
562
- defaultOptions: Record<string, any>;
567
+ static defaultOptions: Record<string, any>;
563
568
  }
564
569
 
565
570
  class UUID extends SchemaType {
@@ -567,7 +572,7 @@ declare module 'mongoose' {
567
572
  static schemaName: 'UUID';
568
573
 
569
574
  /** Default options for this SchemaType */
570
- defaultOptions: Record<string, any>;
575
+ static defaultOptions: Record<string, any>;
571
576
  }
572
577
  }
573
578
  }