mongoose 6.4.3 → 6.4.6

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.
Files changed (52) hide show
  1. package/lib/aggregate.js +3 -2
  2. package/lib/connection.js +9 -9
  3. package/lib/cursor/AggregationCursor.js +3 -2
  4. package/lib/cursor/QueryCursor.js +5 -4
  5. package/lib/document.js +44 -16
  6. package/lib/error/index.js +12 -12
  7. package/lib/error/messages.js +2 -2
  8. package/lib/helpers/query/castUpdate.js +4 -4
  9. package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
  10. package/lib/helpers/timestamps/setupTimestamps.js +14 -18
  11. package/lib/helpers/topology/isAtlas.js +14 -9
  12. package/lib/index.js +6 -2
  13. package/lib/model.js +129 -170
  14. package/lib/options/SchemaArrayOptions.js +2 -2
  15. package/lib/options/SchemaBufferOptions.js +1 -1
  16. package/lib/options/SchemaDateOptions.js +3 -3
  17. package/lib/options/SchemaDocumentArrayOptions.js +2 -2
  18. package/lib/options/SchemaMapOptions.js +1 -1
  19. package/lib/options/SchemaNumberOptions.js +4 -4
  20. package/lib/options/SchemaObjectIdOptions.js +2 -2
  21. package/lib/options/SchemaStringOptions.js +8 -8
  22. package/lib/options/SchemaSubdocumentOptions.js +1 -1
  23. package/lib/options/SchemaTypeOptions.js +14 -14
  24. package/lib/options/VirtualOptions.js +11 -11
  25. package/lib/query.js +82 -73
  26. package/lib/schema/SubdocumentPath.js +10 -6
  27. package/lib/schema/array.js +3 -3
  28. package/lib/schema/boolean.js +5 -5
  29. package/lib/schema/buffer.js +2 -2
  30. package/lib/schema/date.js +2 -2
  31. package/lib/schema/decimal128.js +2 -2
  32. package/lib/schema/documentarray.js +12 -8
  33. package/lib/schema/mixed.js +2 -2
  34. package/lib/schema/number.js +2 -2
  35. package/lib/schema/objectid.js +2 -2
  36. package/lib/schema/string.js +2 -2
  37. package/lib/schema.js +76 -34
  38. package/lib/schematype.js +7 -9
  39. package/lib/types/DocumentArray/methods/index.js +1 -1
  40. package/lib/types/buffer.js +8 -4
  41. package/lib/types/decimal128.js +1 -1
  42. package/lib/types/map.js +1 -1
  43. package/lib/types/objectid.js +1 -1
  44. package/lib/types/subdocument.js +1 -1
  45. package/lib/virtualtype.js +5 -5
  46. package/package.json +7 -7
  47. package/types/expressions.d.ts +5 -4
  48. package/types/index.d.ts +9 -7
  49. package/types/indexes.d.ts +2 -2
  50. package/types/inferschematype.d.ts +26 -10
  51. package/types/models.d.ts +2 -1
  52. package/types/query.d.ts +6 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.4.3",
4
+ "version": "6.4.6",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -42,7 +42,7 @@
42
42
  "buffer": "^5.6.0",
43
43
  "cheerio": "1.0.0-rc.12",
44
44
  "crypto-browserify": "3.12.0",
45
- "dox": "0.3.1",
45
+ "dox": "0.9.1",
46
46
  "eslint": "8.19.0",
47
47
  "eslint-plugin-mocha-no-only": "1.1.1",
48
48
  "highlight.js": "11.5.1",
@@ -60,8 +60,8 @@
60
60
  "serve-handler": "6.1.3",
61
61
  "sinon": "14.0.0",
62
62
  "stream-browserify": "3.0.0",
63
- "ts-benchmark": "^1.0.2",
64
- "tsd": "0.22.0",
63
+ "ts-benchmark": "^1.1.5",
64
+ "tsd": "0.20.0",
65
65
  "typescript": "4.7.4",
66
66
  "uuid": "8.3.2",
67
67
  "webpack": "5.73.0"
@@ -98,8 +98,8 @@
98
98
  "test-tsd": "node ./test/types/check-types-filename && tsd",
99
99
  "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
100
100
  "test-coverage": "nyc --reporter=html --reporter=text npm test",
101
- "ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17 18 29 32",
102
- "ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17 18 29 32 -b master"
101
+ "ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17/100000 18 29 32",
102
+ "ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17/100000 18 29 32 -b master"
103
103
  },
104
104
  "main": "./index.js",
105
105
  "types": "./types/index.d.ts",
@@ -145,4 +145,4 @@
145
145
  "target": "ES2017"
146
146
  }
147
147
  }
148
- }
148
+ }
@@ -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
@@ -106,7 +106,7 @@ declare module 'mongoose' {
106
106
  checkKeys?: boolean;
107
107
  j?: boolean;
108
108
  safe?: boolean | WriteConcern;
109
- timestamps?: boolean;
109
+ timestamps?: boolean | QueryTimestampsConfig;
110
110
  validateBeforeSave?: boolean;
111
111
  validateModifiedOnly?: boolean;
112
112
  w?: number | string;
@@ -351,6 +351,7 @@ declare module 'mongoose' {
351
351
  findOneAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
352
352
 
353
353
  /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
354
+ findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions<T> & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
354
355
  findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions<T> & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
355
356
  findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions<T> | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
356
357
 
package/types/query.d.ts CHANGED
@@ -90,6 +90,11 @@ declare module 'mongoose' {
90
90
  [key: string]: any;
91
91
  };
92
92
 
93
+ interface QueryTimestampsConfig {
94
+ createdAt?: boolean;
95
+ updatedAt?: boolean;
96
+ }
97
+
93
98
  interface QueryOptions<DocType = unknown> extends
94
99
  PopulateOption,
95
100
  SessionOption {
@@ -157,7 +162,7 @@ declare module 'mongoose' {
157
162
  * skip timestamps for this update. Note that this allows you to overwrite
158
163
  * timestamps. Does nothing if schema-level timestamps are not set.
159
164
  */
160
- timestamps?: boolean;
165
+ timestamps?: boolean | QueryTimestampsConfig;
161
166
  upsert?: boolean;
162
167
  writeConcern?: mongodb.WriteConcern;
163
168