mongoose 8.9.3 → 8.9.4

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/lib/document.js CHANGED
@@ -55,6 +55,7 @@ const documentIsModified = require('./helpers/symbols').documentIsModified;
55
55
  const documentModifiedPaths = require('./helpers/symbols').documentModifiedPaths;
56
56
  const documentSchemaSymbol = require('./helpers/symbols').documentSchemaSymbol;
57
57
  const getSymbol = require('./helpers/symbols').getSymbol;
58
+ const modelSymbol = require('./helpers/symbols').modelSymbol;
58
59
  const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
59
60
  const scopeSymbol = require('./helpers/symbols').scopeSymbol;
60
61
  const schemaMixedSymbol = require('./schema/symbols').schemaMixedSymbol;
@@ -1386,7 +1387,9 @@ Document.prototype.$set = function $set(path, val, type, options) {
1386
1387
  const model = val.constructor;
1387
1388
 
1388
1389
  // Check ref
1389
- const ref = schema.options.ref;
1390
+ const refOpt = typeof schema.options.ref === 'function' && !schema.options.ref[modelSymbol] ? schema.options.ref.call(this, this) : schema.options.ref;
1391
+
1392
+ const ref = refOpt?.modelName || refOpt;
1390
1393
  if (ref != null && (ref === model.modelName || ref === model.baseModelName)) {
1391
1394
  return true;
1392
1395
  }
package/lib/model.js CHANGED
@@ -3436,6 +3436,7 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3436
3436
  * @param {String|number} [options.w=1] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
3437
3437
  * @param {number} [options.wtimeout=null] The [write concern timeout](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout).
3438
3438
  * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
3439
+ * @param {Boolean} [options.validateBeforeSave=true] set to `false` to skip Mongoose validation on all documents
3439
3440
  * @return {BulkWriteResult} the return value from `bulkWrite()`
3440
3441
  */
3441
3442
  Model.bulkSave = async function bulkSave(documents, options) {
@@ -3455,15 +3456,13 @@ Model.bulkSave = async function bulkSave(documents, options) {
3455
3456
  }
3456
3457
  }
3457
3458
 
3458
- await Promise.all(documents.map(buildPreSavePromise));
3459
+ await Promise.all(documents.map(doc => buildPreSavePromise(doc, options)));
3459
3460
 
3460
3461
  const writeOperations = this.buildBulkWriteOperations(documents, { skipValidation: true, timestamps: options.timestamps });
3461
-
3462
- const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, options).then(
3462
+ const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, { skipValidation: true, ...options }).then(
3463
3463
  (res) => ({ bulkWriteResult: res, bulkWriteError: null }),
3464
3464
  (err) => ({ bulkWriteResult: null, bulkWriteError: err })
3465
3465
  );
3466
-
3467
3466
  // If not a MongoBulkWriteError, treat this as all documents failed to save.
3468
3467
  if (bulkWriteError != null && !(bulkWriteError instanceof MongoBulkWriteError)) {
3469
3468
  throw bulkWriteError;
@@ -3491,7 +3490,6 @@ Model.bulkSave = async function bulkSave(documents, options) {
3491
3490
  successfulDocuments.push(document);
3492
3491
  }
3493
3492
  }
3494
-
3495
3493
  await Promise.all(successfulDocuments.map(document => handleSuccessfulWrite(document)));
3496
3494
 
3497
3495
  if (bulkWriteError && bulkWriteError.writeErrors && bulkWriteError.writeErrors.length) {
@@ -3501,9 +3499,9 @@ Model.bulkSave = async function bulkSave(documents, options) {
3501
3499
  return bulkWriteResult;
3502
3500
  };
3503
3501
 
3504
- function buildPreSavePromise(document) {
3502
+ function buildPreSavePromise(document, options) {
3505
3503
  return new Promise((resolve, reject) => {
3506
- document.schema.s.hooks.execPre('save', document, (err) => {
3504
+ document.schema.s.hooks.execPre('save', document, [options], (err) => {
3507
3505
  if (err) {
3508
3506
  reject(err);
3509
3507
  return;
@@ -3704,8 +3702,9 @@ Model.castObject = function castObject(obj, options) {
3704
3702
  Model.castObject.call(schemaType.caster, val)
3705
3703
  ];
3706
3704
  }
3705
+
3706
+ continue;
3707
3707
  }
3708
- continue;
3709
3708
  }
3710
3709
  if (schemaType.$isSingleNested || schemaType.$isMongooseDocumentArrayElement) {
3711
3710
  try {
@@ -522,6 +522,7 @@ SchemaDocumentArray.prototype.clone = function() {
522
522
  }
523
523
  schematype.Constructor.discriminators = Object.assign({},
524
524
  this.Constructor.discriminators);
525
+ schematype._appliedDiscriminators = this._appliedDiscriminators;
525
526
  return schematype;
526
527
  };
527
528
 
@@ -393,5 +393,6 @@ SchemaSubdocument.prototype.clone = function() {
393
393
  schematype.requiredValidator = this.requiredValidator;
394
394
  }
395
395
  schematype.caster.discriminators = Object.assign({}, this.caster.discriminators);
396
+ schematype._appliedDiscriminators = this._appliedDiscriminators;
396
397
  return schematype;
397
398
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.9.3",
4
+ "version": "8.9.4",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -30,34 +30,32 @@
30
30
  "devDependencies": {
31
31
  "@babel/core": "7.26.0",
32
32
  "@babel/preset-env": "7.26.0",
33
- "@typescript-eslint/eslint-plugin": "^8.18.0",
34
- "@typescript-eslint/parser": "^8.18.0",
33
+ "@typescript-eslint/eslint-plugin": "^8.19.1",
34
+ "@typescript-eslint/parser": "^8.19.1",
35
35
  "acquit": "1.3.0",
36
36
  "acquit-ignore": "0.2.1",
37
37
  "acquit-require": "0.1.1",
38
38
  "assert-browserify": "2.0.0",
39
- "axios": "1.1.3",
40
39
  "babel-loader": "8.2.5",
41
40
  "broken-link-checker": "^0.7.8",
42
41
  "buffer": "^5.6.0",
43
42
  "cheerio": "1.0.0",
44
43
  "crypto-browserify": "3.12.1",
45
- "dotenv": "16.4.5",
46
44
  "dox": "1.0.0",
47
- "eslint": "8.57.0",
48
- "eslint-plugin-markdown": "^5.0.0",
45
+ "eslint": "8.57.1",
46
+ "eslint-plugin-markdown": "^5.1.0",
49
47
  "eslint-plugin-mocha-no-only": "1.2.0",
50
48
  "express": "^4.19.2",
51
49
  "fs-extra": "~11.2.0",
52
- "highlight.js": "11.10.0",
50
+ "highlight.js": "11.11.1",
53
51
  "lodash.isequal": "4.5.0",
54
52
  "lodash.isequalwith": "4.4.0",
55
- "markdownlint-cli2": "^0.15.0",
56
- "marked": "15.0.3",
53
+ "markdownlint-cli2": "^0.17.1",
54
+ "marked": "15.0.4",
57
55
  "mkdirp": "^3.0.1",
58
- "mocha": "10.8.2",
56
+ "mocha": "11.0.1",
59
57
  "moment": "2.30.1",
60
- "mongodb-memory-server": "10.1.2",
58
+ "mongodb-memory-server": "10.1.3",
61
59
  "ncp": "^2.0.0",
62
60
  "nyc": "15.1.0",
63
61
  "pug": "3.0.3",
@@ -67,7 +65,7 @@
67
65
  "tsd": "0.31.2",
68
66
  "typescript": "5.7.2",
69
67
  "uuid": "11.0.3",
70
- "webpack": "5.96.1"
68
+ "webpack": "5.97.1"
71
69
  },
72
70
  "directories": {
73
71
  "lib": "./lib/mongoose"
package/types/index.d.ts CHANGED
@@ -710,6 +710,14 @@ declare module 'mongoose' {
710
710
  [K in keyof T]: FlattenProperty<T[K]>;
711
711
  };
712
712
 
713
+ export type BufferToBinaryProperty<T> = T extends Buffer
714
+ ? mongodb.Binary
715
+ : T extends Types.DocumentArray<infer ItemType>
716
+ ? Types.DocumentArray<BufferToBinary<ItemType>>
717
+ : T extends Types.Subdocument<unknown, unknown, infer SubdocType>
718
+ ? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
719
+ : BufferToBinary<T>;
720
+
713
721
  /**
714
722
  * Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
715
723
  */
@@ -719,15 +727,11 @@ declare module 'mongoose' {
719
727
  ? T
720
728
  : T extends TreatAsPrimitives
721
729
  ? T
722
- : T extends Record<string, any> ? {
723
- [K in keyof T]: T[K] extends Buffer
724
- ? mongodb.Binary
725
- : T[K] extends Types.DocumentArray<infer ItemType>
726
- ? Types.DocumentArray<BufferToBinary<ItemType>>
727
- : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
728
- ? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
729
- : BufferToBinary<T[K]>;
730
- } : T;
730
+ : T extends Record<string, any>
731
+ ? {
732
+ [K in keyof T]: BufferToBinaryProperty<T[K]>
733
+ }
734
+ : T;
731
735
 
732
736
  /**
733
737
  * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
package/types/models.d.ts CHANGED
@@ -35,6 +35,7 @@ declare module 'mongoose' {
35
35
  interface MongooseBulkSaveOptions extends mongodb.BulkWriteOptions {
36
36
  timestamps?: boolean;
37
37
  session?: ClientSession;
38
+ validateBeforeSave?: boolean;
38
39
  }
39
40
 
40
41
  /**
package/types/query.d.ts CHANGED
@@ -223,7 +223,7 @@ declare module 'mongoose' {
223
223
  type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete';
224
224
 
225
225
  type GetLeanResultType<RawDocType, ResultType, QueryOp> = QueryOp extends QueryOpThatReturnsDocument
226
- ? (ResultType extends any[] ? Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>[] : Default__v<Require_id<BufferToBinary<FlattenMaps<RawDocType>>>>)
226
+ ? (ResultType extends any[] ? Default__v<Require_id<FlattenMaps<BufferToBinary<RawDocType>>>>[] : Default__v<Require_id<FlattenMaps<BufferToBinary<RawDocType>>>>)
227
227
  : ResultType;
228
228
 
229
229
  type MergePopulatePaths<RawDocType, ResultType, QueryOp, Paths, TQueryHelpers, TDocOverrides = Record<string, never>> = QueryOp extends QueryOpThatReturnsDocument