mongoose 7.1.0 → 7.1.2

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/query.d.ts CHANGED
@@ -21,7 +21,7 @@ declare module 'mongoose' {
21
21
 
22
22
  type ProjectionFields<DocType> = { [Key in keyof DocType]?: any } & Record<string, any>;
23
23
 
24
- type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;
24
+ type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType, QueryOp = 'find'> = Query<ResultType, DocType, THelpers, RawDocType, QueryOp> & THelpers;
25
25
 
26
26
  type QuerySelector<T> = {
27
27
  // Comparison
@@ -168,7 +168,9 @@ declare module 'mongoose' {
168
168
  [other: string]: any;
169
169
  }
170
170
 
171
- class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> implements SessionOperation {
171
+ type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
172
+
173
+ class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType, QueryOp = 'find'> implements SessionOperation {
172
174
  _mongooseOptions: MongooseQueryOptions<DocType>;
173
175
 
174
176
  /**
@@ -181,7 +183,13 @@ declare module 'mongoose' {
181
183
  /** Executes the query */
182
184
  exec(): Promise<ResultType>;
183
185
 
184
- $where(argument: string | Function): QueryWithHelpers<DocType[], DocType, THelpers, RawDocType>;
186
+ $where(argument: string | Function): QueryWithHelpers<
187
+ DocType[],
188
+ DocType,
189
+ THelpers,
190
+ RawDocType,
191
+ QueryOp
192
+ >;
185
193
 
186
194
  /** Specifies an `$all` query condition. When called with one argument, the most recent path passed to `where()` is used. */
187
195
  all(path: string, val: Array<any>): this;
@@ -239,13 +247,19 @@ declare module 'mongoose' {
239
247
  comment(val: string): this;
240
248
 
241
249
  /** Specifies this query as a `count` query. */
242
- count(criteria?: FilterQuery<DocType>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
250
+ count(criteria?: FilterQuery<DocType>): QueryWithHelpers<
251
+ number,
252
+ DocType,
253
+ THelpers,
254
+ RawDocType,
255
+ 'count'
256
+ >;
243
257
 
244
258
  /** Specifies this query as a `countDocuments` query. */
245
259
  countDocuments(
246
260
  criteria?: FilterQuery<DocType>,
247
261
  options?: QueryOptions<DocType>
248
- ): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
262
+ ): QueryWithHelpers<number, DocType, THelpers, RawDocType, 'countDocuments'>;
249
263
 
250
264
  /**
251
265
  * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
@@ -258,21 +272,42 @@ declare module 'mongoose' {
258
272
  * remove, except it deletes _every_ document that matches `filter` in the
259
273
  * collection, regardless of the value of `single`.
260
274
  */
261
- deleteMany(filter?: FilterQuery<DocType>, options?: QueryOptions<DocType>): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
262
- deleteMany(filter: FilterQuery<DocType>): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
263
- deleteMany(): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
275
+ deleteMany(
276
+ filter?: FilterQuery<DocType>,
277
+ options?: QueryOptions<DocType>
278
+ ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteMany'>;
279
+ deleteMany(filter: FilterQuery<DocType>): QueryWithHelpers<
280
+ any,
281
+ DocType,
282
+ THelpers,
283
+ RawDocType,
284
+ 'deleteMany'
285
+ >;
286
+ deleteMany(): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteMany'>;
264
287
 
265
288
  /**
266
289
  * Declare and/or execute this query as a `deleteOne()` operation. Works like
267
290
  * remove, except it deletes at most one document regardless of the `single`
268
291
  * option.
269
292
  */
270
- deleteOne(filter?: FilterQuery<DocType>, options?: QueryOptions<DocType>): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
271
- deleteOne(filter: FilterQuery<DocType>): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
272
- deleteOne(): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
293
+ deleteOne(
294
+ filter?: FilterQuery<DocType>,
295
+ options?: QueryOptions<DocType>
296
+ ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteOne'>;
297
+ deleteOne(filter: FilterQuery<DocType>): QueryWithHelpers<
298
+ any,
299
+ DocType,
300
+ THelpers,
301
+ RawDocType,
302
+ 'deleteOne'
303
+ >;
304
+ deleteOne(): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'deleteOne'>;
273
305
 
274
306
  /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
275
- distinct<ReturnType = any>(field: string, filter?: FilterQuery<DocType>): QueryWithHelpers<Array<ReturnType>, DocType, THelpers, RawDocType>;
307
+ distinct<ReturnType = any>(
308
+ field: string,
309
+ filter?: FilterQuery<DocType>
310
+ ): QueryWithHelpers<Array<ReturnType>, DocType, THelpers, RawDocType, 'distinct'>;
276
311
 
277
312
  /** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
278
313
  elemMatch<K = string>(path: K, val: any): this;
@@ -289,7 +324,13 @@ declare module 'mongoose' {
289
324
  equals(val: any): this;
290
325
 
291
326
  /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
292
- estimatedDocumentCount(options?: QueryOptions<DocType>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
327
+ estimatedDocumentCount(options?: QueryOptions<DocType>): QueryWithHelpers<
328
+ number,
329
+ DocType,
330
+ THelpers,
331
+ RawDocType,
332
+ 'estimatedDocumentCount'
333
+ >;
293
334
 
294
335
  /** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
295
336
  exists<K = string>(path: K, val: boolean): this;
@@ -308,81 +349,99 @@ declare module 'mongoose' {
308
349
  filter: FilterQuery<DocType>,
309
350
  projection?: ProjectionType<DocType> | null,
310
351
  options?: QueryOptions<DocType> | null
311
- ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
352
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find'>;
312
353
  find(
313
354
  filter: FilterQuery<DocType>,
314
355
  projection?: ProjectionType<DocType> | null
315
- ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
356
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find'>;
316
357
  find(
317
358
  filter: FilterQuery<DocType>
318
- ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
319
- find(): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType>;
359
+ ): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find'>;
360
+ find(): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find'>;
320
361
 
321
362
  /** Declares the query a findOne operation. When executed, returns the first found document. */
322
363
  findOne(
323
364
  filter?: FilterQuery<DocType>,
324
365
  projection?: ProjectionType<DocType> | null,
325
366
  options?: QueryOptions<DocType> | null
326
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
367
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
327
368
  findOne(
328
369
  filter?: FilterQuery<DocType>,
329
370
  projection?: ProjectionType<DocType> | null
330
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
371
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
331
372
  findOne(
332
373
  filter?: FilterQuery<DocType>
333
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
374
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
334
375
 
335
376
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
336
377
  findOneAndDelete(
337
378
  filter?: FilterQuery<DocType>,
338
379
  options?: QueryOptions<DocType> | null
339
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
380
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndDelete'>;
340
381
 
341
382
  /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */
342
383
  findOneAndRemove(
343
384
  filter?: FilterQuery<DocType>,
344
385
  options?: QueryOptions<DocType> | null
345
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
386
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndRemove'>;
346
387
 
347
388
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
348
389
  findOneAndUpdate(
349
390
  filter: FilterQuery<DocType>,
350
391
  update: UpdateQuery<DocType>,
351
392
  options: QueryOptions<DocType> & { rawResult: true }
352
- ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType>;
393
+ ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
353
394
  findOneAndUpdate(
354
395
  filter: FilterQuery<DocType>,
355
396
  update: UpdateQuery<DocType>,
356
397
  options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc
357
- ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
398
+ ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
358
399
  findOneAndUpdate(
359
400
  filter?: FilterQuery<DocType>,
360
401
  update?: UpdateQuery<DocType>,
361
402
  options?: QueryOptions<DocType> | null
362
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
403
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
363
404
 
364
405
  /** Declares the query a findById operation. When executed, returns the document with the given `_id`. */
365
406
  findById(
366
407
  id: mongodb.ObjectId | any,
367
408
  projection?: ProjectionType<DocType> | null,
368
409
  options?: QueryOptions<DocType> | null
369
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
410
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
370
411
  findById(
371
412
  id: mongodb.ObjectId | any,
372
413
  projection?: ProjectionType<DocType> | null
373
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
414
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
374
415
  findById(
375
416
  id: mongodb.ObjectId | any
376
- ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
417
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne'>;
377
418
 
378
419
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
379
- findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions<DocType> | null): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
420
+ findByIdAndDelete(
421
+ id?: mongodb.ObjectId | any,
422
+ options?: QueryOptions<DocType> | null
423
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndDelete'>;
380
424
 
381
425
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
382
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions<DocType> & { rawResult: true }): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
383
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>, options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
384
- findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<DocType>, options?: QueryOptions<DocType> | null): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
385
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<DocType>): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
426
+ findByIdAndUpdate(
427
+ id: mongodb.ObjectId | any,
428
+ update: UpdateQuery<DocType>,
429
+ options: QueryOptions<DocType> & { rawResult: true }
430
+ ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
431
+ findByIdAndUpdate(
432
+ id: mongodb.ObjectId | any,
433
+ update: UpdateQuery<DocType>,
434
+ options: QueryOptions<DocType> & { upsert: true } & ReturnsNewDoc
435
+ ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
436
+ findByIdAndUpdate(
437
+ id?: mongodb.ObjectId | any,
438
+ update?: UpdateQuery<DocType>,
439
+ options?: QueryOptions<DocType> | null
440
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
441
+ findByIdAndUpdate(
442
+ id: mongodb.ObjectId | any,
443
+ update: UpdateQuery<DocType>
444
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOneAndUpdate'>;
386
445
 
387
446
  /** Specifies a `$geometry` condition */
388
447
  geometry(object: { type: string, coordinates: any[] }): this;
@@ -431,7 +490,9 @@ declare module 'mongoose' {
431
490
  j(val: boolean | null): this;
432
491
 
433
492
  /** Sets the lean option. */
434
- lean<LeanResultType = ResultType extends any[] ? Require_id<FlattenMaps<RawDocType>>[] : Require_id<FlattenMaps<RawDocType>>>(val?: boolean | any): QueryWithHelpers<ResultType extends null ? LeanResultType | null : LeanResultType, DocType, THelpers, RawDocType>;
493
+ lean<LeanResultType = QueryOp extends QueryOpThatReturnsDocument ? (ResultType extends any[] ? Require_id<FlattenMaps<RawDocType>>[] : Require_id<FlattenMaps<RawDocType>>) : ResultType>(
494
+ val?: boolean | any
495
+ ): QueryWithHelpers<ResultType extends null ? LeanResultType | null : LeanResultType, DocType, THelpers, RawDocType, QueryOp>;
435
496
 
436
497
  /** Specifies the maximum number of documents the query will return. */
437
498
  limit(val: number): this;
@@ -448,7 +509,7 @@ declare module 'mongoose' {
448
509
  * Runs a function `fn` and treats the return value of `fn` as the new value
449
510
  * for the query to resolve to.
450
511
  */
451
- transform<MappedType>(fn: (doc: ResultType) => MappedType): QueryWithHelpers<MappedType, DocType, THelpers, RawDocType>;
512
+ transform<MappedType>(fn: (doc: ResultType) => MappedType): QueryWithHelpers<MappedType, DocType, THelpers, RawDocType, QueryOp>;
452
513
 
453
514
  /** Specifies an `$maxDistance` query condition. When called with one argument, the most recent path passed to `where()` is used. */
454
515
  maxDistance(path: string, val: number): this;
@@ -500,15 +561,34 @@ declare module 'mongoose' {
500
561
  * This is handy for integrating with async/await, because `orFail()` saves you
501
562
  * an extra `if` statement to check if no document was found.
502
563
  */
503
- orFail(err?: NativeError | (() => NativeError)): QueryWithHelpers<NonNullable<ResultType>, DocType, THelpers, RawDocType>;
564
+ orFail(err?: NativeError | (() => NativeError)): QueryWithHelpers<NonNullable<ResultType>, DocType, THelpers, RawDocType, QueryOp>;
504
565
 
505
566
  /** Specifies a `$polygon` condition */
506
567
  polygon(path: string, ...coordinatePairs: number[][]): this;
507
568
  polygon(...coordinatePairs: number[][]): this;
508
569
 
509
570
  /** Specifies paths which should be populated with other documents. */
510
- populate<Paths = {}>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, UnpackedIntersection<RawDocType, Paths>>;
511
- populate<Paths = {}>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, UnpackedIntersection<RawDocType, Paths>>;
571
+ populate<Paths = {}>(
572
+ path: string | string[],
573
+ select?: string | any,
574
+ model?: string | Model<any, THelpers>,
575
+ match?: any
576
+ ): QueryWithHelpers<
577
+ UnpackedIntersection<ResultType, Paths>,
578
+ DocType,
579
+ THelpers,
580
+ UnpackedIntersection<RawDocType, Paths>,
581
+ QueryOp
582
+ >;
583
+ populate<Paths = {}>(
584
+ options: PopulateOptions | (PopulateOptions | string)[]
585
+ ): QueryWithHelpers<
586
+ UnpackedIntersection<ResultType, Paths>,
587
+ DocType,
588
+ THelpers,
589
+ UnpackedIntersection<RawDocType, Paths>,
590
+ QueryOp
591
+ >;
512
592
 
513
593
  /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
514
594
  projection(fields?: ProjectionFields<DocType> | string): ProjectionFields<DocType>;
@@ -530,7 +610,11 @@ declare module 'mongoose' {
530
610
  * `update()`, except MongoDB will replace the existing document and will
531
611
  * not accept any [atomic](https://www.mongodb.com/docs/manual/tutorial/model-data-for-atomic-operations/#pattern) operators (`$set`, etc.)
532
612
  */
533
- replaceOne(filter?: FilterQuery<DocType>, replacement?: DocType | AnyObject, options?: QueryOptions<DocType> | null): QueryWithHelpers<any, DocType, THelpers, RawDocType>;
613
+ replaceOne(
614
+ filter?: FilterQuery<DocType>,
615
+ replacement?: DocType | AnyObject,
616
+ options?: QueryOptions<DocType> | null
617
+ ): QueryWithHelpers<any, DocType, THelpers, RawDocType, 'replaceOne'>;
534
618
 
535
619
  /** Specifies which document fields to include or exclude (also known as the query "projection") */
536
620
  select(arg: string | string[] | Record<string, number | boolean | object>): this;
@@ -601,13 +685,21 @@ declare module 'mongoose' {
601
685
  * `filter` (as opposed to just the first one) regardless of the value of
602
686
  * the `multi` option.
603
687
  */
604
- updateMany(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
688
+ updateMany(
689
+ filter?: FilterQuery<DocType>,
690
+ update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline,
691
+ options?: QueryOptions<DocType> | null
692
+ ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany'>;
605
693
 
606
694
  /**
607
695
  * Declare and/or execute this query as an updateOne() operation. Same as
608
696
  * `update()`, except it does not support the `multi` or `overwrite` options.
609
697
  */
610
- updateOne(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
698
+ updateOne(
699
+ filter?: FilterQuery<DocType>,
700
+ update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline,
701
+ options?: QueryOptions<DocType> | null
702
+ ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne'>;
611
703
 
612
704
  /**
613
705
  * Sets the specified number of `mongod` servers, or tag set of `mongod` servers,