mongoose 6.4.4 → 6.4.7

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.
@@ -1067,13 +1067,13 @@ declare module 'mongoose' {
1067
1067
  * - $case
1068
1068
  * - $then
1069
1069
  */
1070
- $branches: { $case: Expression, then: Expression }[];
1070
+ branches: { case: Expression, then: Expression }[];
1071
1071
  /**
1072
1072
  * The path to take if no branch case expression evaluates to true.
1073
1073
  *
1074
1074
  * Although optional, if default is unspecified and no branch case evaluates to true, $switch returns an error.
1075
1075
  */
1076
- $default: Expression;
1076
+ default: Expression;
1077
1077
  };
1078
1078
  }
1079
1079
 
@@ -1104,7 +1104,7 @@ declare module 'mongoose' {
1104
1104
  * @version 3.2
1105
1105
  * @see https://docs.mongodb.com/manual/reference/operator/aggregation/concatArrays/#mongodb-expression-exp.-concatArrays
1106
1106
  */
1107
- $concatArrays: ArrayExpression[];
1107
+ $concatArrays: Expression[];
1108
1108
  }
1109
1109
 
1110
1110
  export interface Filter {
@@ -2448,7 +2448,8 @@ declare module 'mongoose' {
2448
2448
  BinaryExpression |
2449
2449
  FunctionExpression |
2450
2450
  ObjectIdExpression |
2451
- ConditionalExpressionOperator;
2451
+ ConditionalExpressionOperator |
2452
+ any;
2452
2453
 
2453
2454
  export type ObjectIdExpression =
2454
2455
  TypeExpressionOperatorReturningObjectId;
package/types/index.d.ts CHANGED
@@ -109,9 +109,7 @@ declare module 'mongoose' {
109
109
  }
110
110
 
111
111
  export type Require_id<T> = T extends { _id?: infer U }
112
- ? U extends any
113
- ? (T & { _id: Types.ObjectId })
114
- : T & Required<{ _id: U }>
112
+ ? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
115
113
  : T & { _id: Types.ObjectId };
116
114
 
117
115
  export type RequireOnlyTypedId<T> = T extends { _id?: infer U; }
@@ -439,6 +437,10 @@ declare module 'mongoose' {
439
437
 
440
438
  export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
441
439
 
440
+ type Mutable<T> = {
441
+ -readonly [K in keyof T]: T[K];
442
+ };
443
+
442
444
  type _UpdateQuery<TSchema> = {
443
445
  /** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
444
446
  $currentDate?: AnyKeys<TSchema> & AnyObject;
@@ -452,10 +454,10 @@ declare module 'mongoose' {
452
454
  $unset?: AnyKeys<TSchema> & AnyObject;
453
455
 
454
456
  /** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
455
- $addToSet?: mongodb.SetFields<TSchema>;
457
+ $addToSet?: Mutable<mongodb.SetFields<TSchema>>;
456
458
  $pop?: AnyKeys<TSchema> & AnyObject;
457
- $pull?: mongodb.PullOperator<TSchema>;
458
- $push?: mongodb.PushOperator<TSchema>;
459
+ $pull?: Mutable<mongodb.PullOperator<TSchema>>;
460
+ $push?: Mutable<mongodb.PushOperator<TSchema>>;
459
461
  $pullAll?: mongodb.PullAllOperator<TSchema>;
460
462
 
461
463
  /** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
@@ -539,7 +541,7 @@ declare module 'mongoose' {
539
541
  export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
540
542
 
541
543
  // Helpers to simplify checks
542
- type IfAny<IFTYPE, THENTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : IFTYPE;
544
+ type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;
543
545
  type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
544
546
 
545
547
  // tests for these two types are located in test/types/lean.test.ts
@@ -51,8 +51,8 @@ declare module 'mongoose' {
51
51
  * the model's schema except the `_id` index, and build any indexes that
52
52
  * are in your schema but not in MongoDB.
53
53
  */
54
- syncIndexes(options: mongodb.CreateIndexesOptions | null, callback: Callback<Array<string>>): void;
55
- syncIndexes(options?: mongodb.CreateIndexesOptions): Promise<Array<string>>;
54
+ syncIndexes(options: SyncIndexesOptions | null, callback: Callback<Array<string>>): void;
55
+ syncIndexes(options?: SyncIndexesOptions): Promise<Array<string>>;
56
56
  }
57
57
 
58
58
  interface IndexesDiff {
@@ -1,4 +1,18 @@
1
- import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, NumberSchemaDefinition, StringSchemaDefinition, BooleanSchemaDefinition, DateSchemaDefinition } from 'mongoose';
1
+ import {
2
+ Schema,
3
+ InferSchemaType,
4
+ SchemaType,
5
+ SchemaTypeOptions,
6
+ TypeKeyBaseType,
7
+ Types,
8
+ NumberSchemaDefinition,
9
+ StringSchemaDefinition,
10
+ BooleanSchemaDefinition,
11
+ DateSchemaDefinition,
12
+ ObtainDocumentType,
13
+ DefaultTypeKey,
14
+ ObjectIdSchemaDefinition
15
+ } from 'mongoose';
2
16
 
3
17
  declare module 'mongoose' {
4
18
  /**
@@ -75,9 +89,9 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType> =
75
89
  ? P extends { default: undefined }
76
90
  ? false
77
91
  : true
78
- : P extends (Record<TypeKey, NumberSchemaDefinition | StringSchemaDefinition | BooleanSchemaDefinition | DateSchemaDefinition>)
79
- ? P extends { default: ResolvePathType<P[TypeKey]> }
80
- ? true
92
+ : P extends (Record<TypeKey, any>)
93
+ ? P extends { default: any }
94
+ ? IfEquals<P['default'], undefined, false, true>
81
95
  : false
82
96
  : false;
83
97
 
@@ -138,7 +152,8 @@ type ObtainDocumentPathType<PathValueType, TypeKey extends TypeKeyBaseType> = Pa
138
152
  ? InferSchemaType<PathValueType>
139
153
  : ResolvePathType<
140
154
  PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
141
- PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {}
155
+ PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {},
156
+ TypeKey
142
157
  >;
143
158
 
144
159
  /**
@@ -151,17 +166,18 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
151
166
  * @summary Resolve path type by returning the corresponding type.
152
167
  * @param {PathValueType} PathValueType Document definition path type.
153
168
  * @param {Options} Options Document definition path options except path type.
154
- * @returns Number, "Number" or "number" will be resolved to string type.
169
+ * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
170
+ * @returns Number, "Number" or "number" will be resolved to number type.
155
171
  */
156
- type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
172
+ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
157
173
  PathValueType extends Schema ? InferSchemaType<PathValueType> :
158
- PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] :
174
+ PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
159
175
  PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
160
176
  PathValueType extends NumberSchemaDefinition ? number :
161
177
  PathValueType extends DateSchemaDefinition ? Date :
162
178
  PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
163
179
  PathValueType extends BooleanSchemaDefinition ? boolean :
164
- PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId :
180
+ PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
165
181
  PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
166
182
  PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
167
183
  PathValueType extends ArrayConstructor ? any[] :
@@ -169,5 +185,5 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
169
185
  IfEquals<PathValueType, ObjectConstructor> extends true ? any:
170
186
  IfEquals<PathValueType, {}> extends true ? any:
171
187
  PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
172
- PathValueType extends {} ? PathValueType :
188
+ PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, TypeKey> :
173
189
  unknown;
package/types/models.d.ts CHANGED
@@ -306,11 +306,11 @@ declare module 'mongoose' {
306
306
  estimatedDocumentCount(options?: QueryOptions<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
307
307
 
308
308
  /**
309
- * Returns a document with its `_id` if at least one document exists in the database that matches
310
- * the given `filter`, and `null` otherwise.
311
- */
312
- exists(filter: FilterQuery<T>, callback: Callback<Pick<Document<T>, '_id'> | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
313
- exists(filter: FilterQuery<T>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
309
+ * Returns a document with its `_id` if at least one document exists in the database that matches
310
+ * the given `filter`, and `null` otherwise.
311
+ */
312
+ exists(filter: FilterQuery<T>, callback: Callback<{ _id: InferId<T> } | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
313
+ exists(filter: FilterQuery<T>): QueryWithHelpers<{ _id: InferId<T> } | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
314
314
 
315
315
  /** Creates a `find` query: gets a list of documents that match `filter`. */
316
316
  find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(