mongoose 8.2.0 → 8.2.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/models.d.ts CHANGED
@@ -156,6 +156,85 @@ declare module 'mongoose' {
156
156
 
157
157
  const Model: Model<any>;
158
158
 
159
+ export type AnyBulkWriteOperation<TSchema = AnyObject> = {
160
+ insertOne: InsertOneModel<TSchema>;
161
+ } | {
162
+ replaceOne: ReplaceOneModel<TSchema>;
163
+ } | {
164
+ updateOne: UpdateOneModel<TSchema>;
165
+ } | {
166
+ updateMany: UpdateManyModel<TSchema>;
167
+ } | {
168
+ deleteOne: DeleteOneModel<TSchema>;
169
+ } | {
170
+ deleteMany: DeleteManyModel<TSchema>;
171
+ };
172
+
173
+ export interface InsertOneModel<TSchema> {
174
+ document: mongodb.OptionalId<TSchema>
175
+ }
176
+
177
+ export interface ReplaceOneModel<TSchema = AnyObject> {
178
+ /** The filter to limit the replaced document. */
179
+ filter: FilterQuery<TSchema>;
180
+ /** The document with which to replace the matched document. */
181
+ replacement: mongodb.WithoutId<TSchema>;
182
+ /** Specifies a collation. */
183
+ collation?: mongodb.CollationOptions;
184
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
185
+ hint?: mongodb.Hint;
186
+ /** When true, creates a new document if no document matches the query. */
187
+ upsert?: boolean;
188
+ }
189
+
190
+ export interface UpdateOneModel<TSchema = AnyObject> {
191
+ /** The filter to limit the updated documents. */
192
+ filter: FilterQuery<TSchema>;
193
+ /** A document or pipeline containing update operators. */
194
+ update: UpdateQuery<TSchema>;
195
+ /** A set of filters specifying to which array elements an update should apply. */
196
+ arrayFilters?: AnyObject[];
197
+ /** Specifies a collation. */
198
+ collation?: mongodb.CollationOptions;
199
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
200
+ hint?: mongodb.Hint;
201
+ /** When true, creates a new document if no document matches the query. */
202
+ upsert?: boolean;
203
+ }
204
+
205
+ export interface UpdateManyModel<TSchema = AnyObject> {
206
+ /** The filter to limit the updated documents. */
207
+ filter: FilterQuery<TSchema>;
208
+ /** A document or pipeline containing update operators. */
209
+ update: UpdateQuery<TSchema>;
210
+ /** A set of filters specifying to which array elements an update should apply. */
211
+ arrayFilters?: AnyObject[];
212
+ /** Specifies a collation. */
213
+ collation?: mongodb.CollationOptions;
214
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
215
+ hint?: mongodb.Hint;
216
+ /** When true, creates a new document if no document matches the query. */
217
+ upsert?: boolean;
218
+ }
219
+
220
+ export interface DeleteOneModel<TSchema = AnyObject> {
221
+ /** The filter to limit the deleted documents. */
222
+ filter: FilterQuery<TSchema>;
223
+ /** Specifies a collation. */
224
+ collation?: mongodb.CollationOptions;
225
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
226
+ hint?: mongodb.Hint;
227
+ }
228
+
229
+ export interface DeleteManyModel<TSchema = AnyObject> {
230
+ /** The filter to limit the deleted documents. */
231
+ filter: FilterQuery<TSchema>;
232
+ /** Specifies a collation. */
233
+ collation?: mongodb.CollationOptions;
234
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
235
+ hint?: mongodb.Hint;
236
+ }
237
+
159
238
  /**
160
239
  * Models are fancy constructors compiled from `Schema` definitions.
161
240
  * An instance of a model is called a document.
@@ -201,17 +280,11 @@ declare module 'mongoose' {
201
280
  * round trip to the MongoDB server.
202
281
  */
203
282
  bulkWrite<DocContents = TRawDocType>(
204
- writes: Array<
205
- mongodb.AnyBulkWriteOperation<
206
- DocContents extends mongodb.Document ? DocContents : any
207
- > & MongooseBulkWritePerWriteOptions>,
283
+ writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
208
284
  options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
209
285
  ): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
210
286
  bulkWrite<DocContents = TRawDocType>(
211
- writes: Array<
212
- mongodb.AnyBulkWriteOperation<
213
- DocContents extends mongodb.Document ? DocContents : any
214
- > & MongooseBulkWritePerWriteOptions>,
287
+ writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
215
288
  options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
216
289
  ): Promise<mongodb.BulkWriteResult>;
217
290
 
@@ -228,7 +301,7 @@ declare module 'mongoose' {
228
301
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
229
302
  countDocuments(
230
303
  filter?: FilterQuery<TRawDocType>,
231
- options?: (mongodb.CountOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
304
+ options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType>) | null
232
305
  ): QueryWithHelpers<
233
306
  number,
234
307
  THydratedDocumentType,
@@ -266,7 +339,7 @@ declare module 'mongoose' {
266
339
  */
267
340
  deleteMany(
268
341
  filter?: FilterQuery<TRawDocType>,
269
- options?: (mongodb.DeleteOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
342
+ options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
270
343
  ): QueryWithHelpers<
271
344
  mongodb.DeleteResult,
272
345
  THydratedDocumentType,
@@ -291,7 +364,7 @@ declare module 'mongoose' {
291
364
  */
292
365
  deleteOne(
293
366
  filter?: FilterQuery<TRawDocType>,
294
- options?: (mongodb.DeleteOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
367
+ options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
295
368
  ): QueryWithHelpers<
296
369
  mongodb.DeleteResult,
297
370
  THydratedDocumentType,
@@ -743,14 +816,14 @@ declare module 'mongoose' {
743
816
  updateMany<ResultDoc = THydratedDocumentType>(
744
817
  filter?: FilterQuery<TRawDocType>,
745
818
  update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
746
- options?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'>) | null
819
+ options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
747
820
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany'>;
748
821
 
749
822
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
750
823
  updateOne<ResultDoc = THydratedDocumentType>(
751
824
  filter?: FilterQuery<TRawDocType>,
752
825
  update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
753
- options?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'>) | null
826
+ options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
754
827
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne'>;
755
828
 
756
829
  /** Creates a Query, applies the passed conditions, and returns the Query. */
@@ -36,7 +36,8 @@ declare module 'mongoose' {
36
36
  | PipelineStage.SortByCount
37
37
  | PipelineStage.UnionWith
38
38
  | PipelineStage.Unset
39
- | PipelineStage.Unwind;
39
+ | PipelineStage.Unwind
40
+ | PipelineStage.VectorSearch;
40
41
 
41
42
  export namespace PipelineStage {
42
43
  export interface AddFields {
@@ -308,5 +309,17 @@ declare module 'mongoose' {
308
309
  /** [`$unwind` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind/) */
309
310
  $unwind: string | { path: string; includeArrayIndex?: string; preserveNullAndEmptyArrays?: boolean }
310
311
  }
312
+ export interface VectorSearch {
313
+ /** [`$vectorSearch` reference](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/) */
314
+ $vectorSearch: {
315
+ index: string,
316
+ path: string,
317
+ queryVector: number[],
318
+ numCandidates: number,
319
+ limit: number,
320
+ filter?: Expression,
321
+ }
322
+ }
323
+
311
324
  }
312
325
  }
package/types/query.d.ts CHANGED
@@ -17,25 +17,33 @@ declare module 'mongoose' {
17
17
  */
18
18
  type FilterQuery<T> = _FilterQuery<T>;
19
19
 
20
- type MongooseQueryOptions<DocType = unknown> = Pick<
21
- QueryOptions<DocType>,
22
- 'context' |
23
- 'lean' |
24
- 'multipleCastError' |
25
- 'overwriteDiscriminatorKey' |
26
- 'populate' |
27
- 'runValidators' |
28
- 'sanitizeProjection' |
29
- 'sanitizeFilter' |
30
- 'setDefaultsOnInsert' |
31
- 'strict' |
32
- 'strictQuery' |
33
- 'timestamps' |
34
- 'translateAliases'
35
- > & {
20
+ type MongooseBaseQueryOptionKeys =
21
+ | 'context'
22
+ | 'multipleCastError'
23
+ | 'overwriteDiscriminatorKey'
24
+ | 'populate'
25
+ | 'runValidators'
26
+ | 'sanitizeProjection'
27
+ | 'sanitizeFilter'
28
+ | 'setDefaultsOnInsert'
29
+ | 'strict'
30
+ | 'strictQuery'
31
+ | 'translateAliases';
32
+
33
+ type MongooseQueryOptions<
34
+ DocType = unknown,
35
+ Keys extends keyof QueryOptions<DocType> = MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'
36
+ > = Pick<QueryOptions<DocType>, Keys> & {
36
37
  [other: string]: any;
37
38
  };
38
39
 
40
+ type MongooseBaseQueryOptions<DocType = unknown> = MongooseQueryOptions<DocType, MongooseBaseQueryOptionKeys>;
41
+
42
+ type MongooseUpdateQueryOptions<DocType = unknown> = MongooseQueryOptions<
43
+ DocType,
44
+ MongooseBaseQueryOptionKeys | 'timestamps'
45
+ >;
46
+
39
47
  type ProjectionFields<DocType> = { [Key in keyof DocType]?: any } & Record<string, any>;
40
48
 
41
49
  type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType, QueryOp = 'find'> = Query<ResultType, DocType, THelpers, RawDocType, QueryOp> & THelpers;
@@ -208,7 +216,7 @@ declare module 'mongoose' {
208
216
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
209
217
  * This is equivalent to calling `.cursor()` with no arguments.
210
218
  */
211
- [Symbol.asyncIterator](): AsyncIterableIterator<DocType>;
219
+ [Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<ResultType>>;
212
220
 
213
221
  /** Executes the query */
214
222
  exec(): Promise<ResultType>;
@@ -286,7 +294,7 @@ declare module 'mongoose' {
286
294
  * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
287
295
  * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
288
296
  */
289
- cursor(options?: QueryOptions<DocType>): Cursor<DocType, QueryOptions<DocType>>;
297
+ cursor(options?: QueryOptions<DocType>): Cursor<Unpacked<ResultType>, QueryOptions<DocType>>;
290
298
 
291
299
  /**
292
300
  * Declare and/or execute this query as a `deleteMany()` operation. Works like
@@ -619,6 +627,12 @@ declare module 'mongoose' {
619
627
  QueryOp
620
628
  >;
621
629
 
630
+ /** Add pre middleware to this query instance. Doesn't affect other queries. */
631
+ pre(fn: Function): this;
632
+
633
+ /** Add post middleware to this query instance. Doesn't affect other queries. */
634
+ post(fn: Function): this;
635
+
622
636
  /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
623
637
  projection(fields?: ProjectionFields<DocType> | string): ProjectionFields<DocType>;
624
638
  projection(fields: null): null;
@@ -647,7 +661,7 @@ declare module 'mongoose' {
647
661
 
648
662
  /** Specifies which document fields to include or exclude (also known as the query "projection") */
649
663
  select<RawDocTypeOverride extends { [P in keyof RawDocType]?: any } = {}>(
650
- arg: string | string[] | Record<string, number | boolean | object>
664
+ arg: string | string[] | Record<string, number | boolean | string | object>
651
665
  ): QueryWithHelpers<
652
666
  IfEquals<
653
667
  RawDocTypeOverride,
@@ -2,6 +2,17 @@ declare module 'mongoose' {
2
2
  type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;
3
3
  type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
4
4
 
5
+ /**
6
+ * @summary Removes keys from a type
7
+ * @description It helps to exclude keys from a type
8
+ * @param {T} T A generic type to be checked.
9
+ * @param {K} K Keys from T that are to be excluded from the generic type
10
+ * @returns T with the keys in K excluded
11
+ */
12
+ type ExcludeKeys<T, K extends keyof T> = {
13
+ [P in keyof T as P extends K ? never : P]: T[P];
14
+ };
15
+
5
16
  type Unpacked<T> = T extends (infer U)[] ?
6
17
  U :
7
18
  T extends ReadonlyArray<infer U> ? U : T;