mongoose 9.0.0 → 9.0.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.
Files changed (42) hide show
  1. package/lib/aggregate.js +1 -1
  2. package/lib/cast/double.js +1 -1
  3. package/lib/cast.js +1 -1
  4. package/lib/connection.js +10 -10
  5. package/lib/document.js +63 -19
  6. package/lib/drivers/node-mongodb-native/collection.js +38 -104
  7. package/lib/drivers/node-mongodb-native/connection.js +1 -1
  8. package/lib/helpers/common.js +1 -1
  9. package/lib/helpers/indexes/applySchemaCollation.js +1 -1
  10. package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
  11. package/lib/helpers/model/applyMethods.js +1 -1
  12. package/lib/helpers/model/castBulkWrite.js +13 -6
  13. package/lib/helpers/populate/getModelsMapForPopulate.js +3 -3
  14. package/lib/helpers/populate/modelNamesFromRefPath.js +1 -1
  15. package/lib/helpers/populate/removeDeselectedForeignField.js +1 -1
  16. package/lib/helpers/projection/applyProjection.js +2 -2
  17. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  18. package/lib/helpers/setDefaultsOnInsert.js +2 -2
  19. package/lib/helpers/timestamps/setupTimestamps.js +1 -1
  20. package/lib/helpers/update/applyTimestampsToUpdate.js +38 -25
  21. package/lib/helpers/update/decorateUpdateWithVersionKey.js +1 -1
  22. package/lib/model.js +17 -11
  23. package/lib/mongoose.js +3 -4
  24. package/lib/query.js +3 -3
  25. package/lib/schema/array.js +1 -1
  26. package/lib/schema/number.js +14 -2
  27. package/lib/schema/operators/text.js +1 -1
  28. package/lib/schema.js +21 -21
  29. package/lib/schemaType.js +8 -8
  30. package/lib/types/array/index.js +5 -5
  31. package/lib/types/documentArray/index.js +6 -6
  32. package/lib/types/objectid.js +1 -1
  33. package/lib/utils.js +12 -24
  34. package/lib/virtualType.js +1 -1
  35. package/package.json +8 -7
  36. package/types/index.d.ts +11 -3
  37. package/types/inferrawdoctype.d.ts +9 -3
  38. package/types/inferschematype.d.ts +20 -27
  39. package/types/models.d.ts +313 -85
  40. package/types/query.d.ts +91 -1
  41. package/types/utility.d.ts +1 -1
  42. package/types/virtuals.d.ts +3 -3
@@ -30,7 +30,7 @@ Object.defineProperty(ObjectId.prototype, '_id', {
30
30
  * Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
31
31
  */
32
32
 
33
- if (!ObjectId.prototype.hasOwnProperty('valueOf')) {
33
+ if (!Object.hasOwn(ObjectId.prototype, 'valueOf')) {
34
34
  ObjectId.prototype.valueOf = function objectIdValueOf() {
35
35
  return this.toString();
36
36
  };
package/lib/utils.js CHANGED
@@ -190,6 +190,9 @@ exports.deepEqual = function deepEqual(a, b) {
190
190
  */
191
191
 
192
192
  exports.last = function(arr) {
193
+ if (arr == null) {
194
+ return void 0;
195
+ }
193
196
  if (arr.length > 0) {
194
197
  return arr[arr.length - 1];
195
198
  }
@@ -272,6 +275,10 @@ exports.clonePOJOsAndArrays = function clonePOJOsAndArrays(val) {
272
275
  exports.merge = function merge(to, from, options, path) {
273
276
  options = options || {};
274
277
 
278
+ if (from == null) {
279
+ return to;
280
+ }
281
+
275
282
  const keys = Object.keys(from);
276
283
  let i = 0;
277
284
  const len = keys.length;
@@ -690,6 +697,9 @@ exports.setValue = function(path, val, obj, map, _copying) {
690
697
 
691
698
  exports.object = {};
692
699
  exports.object.vals = function vals(o) {
700
+ if (o == null) {
701
+ return [];
702
+ }
693
703
  const keys = Object.keys(o);
694
704
  let i = keys.length;
695
705
  const ret = [];
@@ -701,18 +711,6 @@ exports.object.vals = function vals(o) {
701
711
  return ret;
702
712
  };
703
713
 
704
- const hop = Object.prototype.hasOwnProperty;
705
-
706
- /**
707
- * Safer helper for hasOwnProperty checks
708
- *
709
- * @param {Object} obj
710
- * @param {String} prop
711
- */
712
-
713
- exports.object.hasOwnProperty = function(obj, prop) {
714
- return hop.call(obj, prop);
715
- };
716
714
 
717
715
  /**
718
716
  * Determine if `val` is null or undefined
@@ -763,8 +761,6 @@ exports.array.flatten = function flatten(arr, filter, ret) {
763
761
  * ignore
764
762
  */
765
763
 
766
- const _hasOwnProperty = Object.prototype.hasOwnProperty;
767
-
768
764
  exports.hasUserDefinedProperty = function(obj, key) {
769
765
  if (obj == null) {
770
766
  return false;
@@ -779,7 +775,7 @@ exports.hasUserDefinedProperty = function(obj, key) {
779
775
  return false;
780
776
  }
781
777
 
782
- if (_hasOwnProperty.call(obj, key)) {
778
+ if (Object.hasOwn(obj, key)) {
783
779
  return true;
784
780
  }
785
781
  if (typeof obj === 'object' && key in obj) {
@@ -865,15 +861,7 @@ exports.buffer.areEqual = function(a, b) {
865
861
  if (!Buffer.isBuffer(b)) {
866
862
  return false;
867
863
  }
868
- if (a.length !== b.length) {
869
- return false;
870
- }
871
- for (let i = 0, len = a.length; i < len; ++i) {
872
- if (a[i] !== b[i]) {
873
- return false;
874
- }
875
- }
876
- return true;
864
+ return a.equals(b);
877
865
  };
878
866
 
879
867
  exports.getFunctionName = getFunctionName;
@@ -143,7 +143,7 @@ VirtualType.prototype.set = function(fn) {
143
143
  VirtualType.prototype.applyGetters = function(value, doc) {
144
144
  if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
145
145
  doc.$$populatedVirtuals &&
146
- doc.$$populatedVirtuals.hasOwnProperty(this.path)) {
146
+ Object.hasOwn(doc.$$populatedVirtuals, this.path)) {
147
147
  value = doc.$$populatedVirtuals[this.path];
148
148
  }
149
149
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "9.0.0",
4
+ "version": "9.0.2",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -28,7 +28,7 @@
28
28
  "sift": "17.1.3"
29
29
  },
30
30
  "devDependencies": {
31
- "@ark/attest": "0.53.0",
31
+ "@ark/attest": "0.55.0",
32
32
  "@mongodb-js/mongodb-downloader": "^1.0.0",
33
33
  "acquit": "1.4.0",
34
34
  "acquit-ignore": "0.2.1",
@@ -37,7 +37,7 @@
37
37
  "broken-link-checker": "^0.7.8",
38
38
  "cheerio": "1.1.2",
39
39
  "dox": "1.0.0",
40
- "eslint": "9.25.1",
40
+ "eslint": "9.39.1",
41
41
  "eslint-plugin-markdown": "^5.1.0",
42
42
  "eslint-plugin-mocha-no-only": "1.2.0",
43
43
  "express": "^4.19.2",
@@ -45,10 +45,10 @@
45
45
  "highlight.js": "11.11.1",
46
46
  "lodash.isequal": "4.5.0",
47
47
  "lodash.isequalwith": "4.4.0",
48
- "markdownlint-cli2": "^0.18.1",
48
+ "markdownlint-cli2": "^0.19.1",
49
49
  "marked": "15.x",
50
50
  "mkdirp": "^3.0.1",
51
- "mocha": "11.7.4",
51
+ "mocha": "11.7.5",
52
52
  "moment": "2.30.1",
53
53
  "mongodb-memory-server": "10.3.0",
54
54
  "mongodb-runner": "^6.0.0",
@@ -81,7 +81,7 @@
81
81
  "docs:prepare:publish:5x": "git checkout 5.x && git merge 5.x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && git checkout gh-pages && npm run docs:copy:tmp:5x",
82
82
  "docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x",
83
83
  "docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
84
- "docs:prepare:publish:8x": "git checkout gh-pages && git merge 8.x && npm run docs:generate",
84
+ "docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/8.x && mv ./tmp ./docs/8.x",
85
85
  "docs:check-links": "blc http://127.0.0.1:8089 -ro",
86
86
  "lint": "eslint .",
87
87
  "lint-js": "eslint . --ext .js --ext .cjs",
@@ -99,9 +99,10 @@
99
99
  "test-tsd": "node ./test/types/check-types-filename && tsd --full",
100
100
  "setup-test-encryption": "node scripts/setup-encryption-tests.js",
101
101
  "test-encryption": "mocha --exit ./test/encryption/*.test.js",
102
- "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
102
+ "tdd": "mocha --watch --inspect --recursive ./test/*.test.js --watch-files lib/**/*.js test/**/*.js",
103
103
  "test-coverage": "nyc --reporter=html --reporter=text npm test",
104
104
  "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
105
+ "ts-benchmark:local": "node ./scripts/create-tarball && cd ./benchmarks/typescript/simple && rm -rf ./node_modules && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
105
106
  "attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts"
106
107
  },
107
108
  "main": "./index.js",
package/types/index.d.ts CHANGED
@@ -345,7 +345,8 @@ declare module 'mongoose' {
345
345
  TSchemaOptions extends { methods: infer M } ? M : {},
346
346
  TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
347
347
  TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
348
- RawDocType
348
+ RawDocType,
349
+ ResolveSchemaOptions<TSchemaOptions>
349
350
  >
350
351
  >(def: TSchemaDefinition): Schema<
351
352
  RawDocType,
@@ -374,7 +375,14 @@ declare module 'mongoose' {
374
375
  InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
375
376
  ResolveSchemaOptions<TSchemaOptions>
376
377
  >,
377
- THydratedDocumentType extends AnyObject = HydratedDocument<InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>>
378
+ THydratedDocumentType extends AnyObject = HydratedDocument<
379
+ InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
380
+ TSchemaOptions extends { methods: infer M } ? M : {},
381
+ TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
382
+ TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
383
+ RawDocType,
384
+ ResolveSchemaOptions<TSchemaOptions>
385
+ >
378
386
  >(def: TSchemaDefinition, options: TSchemaOptions): Schema<
379
387
  RawDocType,
380
388
  Model<RawDocType, any, any, any>,
@@ -623,7 +631,7 @@ declare module 'mongoose' {
623
631
 
624
632
  /** Adds static "class" methods to Models compiled from this schema. */
625
633
  static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
626
- static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
634
+ static(obj: Partial<TStaticMethods> & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
627
635
  static(name: string, fn: (this: TModelType, ...args: any[]) => any): this;
628
636
 
629
637
  /** Object of currently defined statics on this schema. */
@@ -12,18 +12,24 @@ declare module 'mongoose' {
12
12
  ? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
13
13
  : FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
14
14
 
15
- export type InferRawDocType<
15
+ export type InferRawDocTypeWithout_id<
16
16
  SchemaDefinition,
17
17
  TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
18
18
  TTransformOptions = { bufferToBinary: false }
19
- > = Require_id<ApplySchemaOptions<{
19
+ > = ApplySchemaOptions<{
20
20
  [
21
21
  K in keyof (RequiredPaths<SchemaDefinition, TSchemaOptions['typeKey']> &
22
22
  OptionalPaths<SchemaDefinition, TSchemaOptions['typeKey']>)
23
23
  ]: IsPathRequired<SchemaDefinition[K], TSchemaOptions['typeKey']> extends true
24
24
  ? ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions>
25
25
  : ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
26
- }, TSchemaOptions>>;
26
+ }, TSchemaOptions>;
27
+
28
+ export type InferRawDocType<
29
+ SchemaDefinition,
30
+ TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
31
+ TTransformOptions = { bufferToBinary: false }
32
+ > = Require_id<InferRawDocTypeWithout_id<SchemaDefinition, TSchemaOptions, TTransformOptions>>;
27
33
 
28
34
  /**
29
35
  * @summary Allows users to optionally choose their own type for a schema field for stronger typing.
@@ -100,10 +100,10 @@ declare module 'mongoose' {
100
100
  {
101
101
  EnforcedDocType: EnforcedDocType;
102
102
  M: M;
103
- TInstanceMethods: TInstanceMethods;
104
- TQueryHelpers: TQueryHelpers;
105
- TVirtuals: AddDefaultId<DocType, TVirtuals, TSchemaOptions>;
106
- TStaticMethods: TStaticMethods;
103
+ TInstanceMethods: IfEquals<TInstanceMethods, {}, TSchemaOptions extends { methods: infer M } ? M : {}, TInstanceMethods>;
104
+ TQueryHelpers: IfEquals<TQueryHelpers, {}, TSchemaOptions extends { query: infer Q } ? Q : {}, TQueryHelpers>;
105
+ TVirtuals: AddDefaultId<DocType, IfEquals<TVirtuals, {}, TSchemaOptions extends { virtuals: infer V } ? V : {}, TVirtuals>, TSchemaOptions>;
106
+ TStaticMethods: IfEquals<TStaticMethods, {}, TSchemaOptions extends { statics: infer S } ? S : {}, TStaticMethods>;
107
107
  TSchemaOptions: TSchemaOptions;
108
108
  DocType: DocType;
109
109
  THydratedDocumentType: THydratedDocumentType;
@@ -136,11 +136,6 @@ declare module 'mongoose' {
136
136
  : T;
137
137
  }
138
138
 
139
- type IsPathDefaultUndefined<PathType> =
140
- PathType extends { default: undefined } ? true
141
- : PathType extends { default: (...args: any[]) => undefined } ? true
142
- : false;
143
-
144
139
  type RequiredPropertyDefinition =
145
140
  | {
146
141
  required: true | string | [true, string | undefined] | { isRequired: true };
@@ -159,16 +154,17 @@ type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
159
154
  P extends { required: false } ?
160
155
  false
161
156
  : true
162
- : P extends Record<TypeKey, ArrayConstructor | any[]> ?
163
- IsPathDefaultUndefined<P> extends true ?
164
- false
165
- : true
166
- : P extends Record<TypeKey, any> ?
167
- P extends { default: any } ?
168
- IfEquals<P['default'], undefined, false, true>
169
- : false
157
+ : P extends { default: undefined | null | ((...args: any[]) => undefined) | ((...args: any[]) => null) } ? false
158
+ : P extends { default: any } ? true
159
+ : P extends Record<TypeKey, ArrayConstructor | any[]> ? true
170
160
  : false;
171
161
 
162
+ // Internal type used to efficiently check for never or any types
163
+ // can be efficiently checked like:
164
+ // `[T] extends [neverOrAny] ? T : ...`
165
+ // to avoid edge cases
166
+ type neverOrAny = ' ~neverOrAny~';
167
+
172
168
  /**
173
169
  * @summary A Utility to obtain schema's required path keys.
174
170
  * @param {T} T A generic refers to document definition.
@@ -251,6 +247,7 @@ type UnionToType<T extends readonly any[]> = T[number] extends infer U
251
247
 
252
248
  type IsSchemaTypeFromBuiltinClass<T> =
253
249
  T extends typeof String ? true
250
+ : unknown extends Buffer ? false
254
251
  : T extends typeof Number ? true
255
252
  : T extends typeof Boolean ? true
256
253
  : T extends typeof Buffer ? true
@@ -268,7 +265,6 @@ type IsSchemaTypeFromBuiltinClass<T> =
268
265
  : T extends NativeDate ? true
269
266
  : T extends typeof Schema.Types.Mixed ? true
270
267
  : T extends Types.UUID ? true
271
- : unknown extends Buffer ? false
272
268
  : T extends Buffer ? true
273
269
  : false;
274
270
 
@@ -284,12 +280,10 @@ type ResolvePathType<
284
280
  Options extends SchemaTypeOptions<PathValueType> = {},
285
281
  TypeKey extends string = DefaultSchemaOptions['typeKey'],
286
282
  TypeHint = never
287
- > = IfEquals<
288
- TypeHint,
289
- never,
290
- PathValueType extends Schema ? InferSchemaType<PathValueType>
283
+ > = [TypeHint] extends [never]
284
+ ? PathValueType extends Schema ? InferSchemaType<PathValueType>
291
285
  : PathValueType extends AnyArray<infer Item> ?
292
- IfEquals<Item, never> extends true
286
+ [Item] extends [never]
293
287
  ? any[]
294
288
  : Item extends Schema ?
295
289
  // If Item is a schema, infer its type.
@@ -328,7 +322,7 @@ type ResolvePathType<
328
322
  : never
329
323
  : PathValueType extends ArrayConstructor ? any[]
330
324
  : PathValueType extends typeof Schema.Types.Mixed ? any
331
- : IfEquals<PathValueType, ObjectConstructor> extends true ? any
325
+ : PathValueType extends ObjectConstructor ? any
332
326
  : IfEquals<PathValueType, {}> extends true ? any
333
327
  : PathValueType extends typeof SchemaType ? PathValueType['prototype']
334
328
  : PathValueType extends Record<string, any> ?
@@ -339,6 +333,5 @@ type ResolvePathType<
339
333
  typeKey: TypeKey;
340
334
  }
341
335
  >
342
- : unknown,
343
- TypeHint
344
- >;
336
+ : unknown
337
+ : TypeHint;