mongoose 8.5.1 → 8.5.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/lib/document.js CHANGED
@@ -1393,8 +1393,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
1393
1393
  })();
1394
1394
 
1395
1395
  let didPopulate = false;
1396
- if (refMatches && val instanceof Document && (!val.$__.wasPopulated || utils.deepEqual(val.$__.wasPopulated.value, val._id))) {
1397
- const unpopulatedValue = (schema && schema.$isSingleNested) ? schema.cast(val, this) : val._id;
1396
+ if (refMatches && val instanceof Document && (!val.$__.wasPopulated || utils.deepEqual(val.$__.wasPopulated.value, val._doc._id))) {
1397
+ const unpopulatedValue = (schema && schema.$isSingleNested) ? schema.cast(val, this) : val._doc._id;
1398
1398
  this.$populated(path, unpopulatedValue, { [populateModelSymbol]: val.constructor });
1399
1399
  val.$__.wasPopulated = { value: unpopulatedValue };
1400
1400
  didPopulate = true;
@@ -1409,10 +1409,10 @@ Document.prototype.$set = function $set(path, val, type, options) {
1409
1409
  schema.options[typeKey][0].ref &&
1410
1410
  _isManuallyPopulatedArray(val, schema.options[typeKey][0].ref)) {
1411
1411
  popOpts = { [populateModelSymbol]: val[0].constructor };
1412
- this.$populated(path, val.map(function(v) { return v._id; }), popOpts);
1412
+ this.$populated(path, val.map(function(v) { return v._doc._id; }), popOpts);
1413
1413
 
1414
1414
  for (const doc of val) {
1415
- doc.$__.wasPopulated = { value: doc._id };
1415
+ doc.$__.wasPopulated = { value: doc._doc._id };
1416
1416
  }
1417
1417
  didPopulate = true;
1418
1418
  }
@@ -1455,7 +1455,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
1455
1455
  if (Array.isArray(val) && this.$__.populated[path]) {
1456
1456
  for (let i = 0; i < val.length; ++i) {
1457
1457
  if (val[i] instanceof Document) {
1458
- val.set(i, val[i]._id, true);
1458
+ val.set(i, val[i]._doc._id, true);
1459
1459
  }
1460
1460
  }
1461
1461
  }
@@ -1628,7 +1628,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru
1628
1628
  // if they have the same _id
1629
1629
  if (this.$populated(path) &&
1630
1630
  val instanceof Document &&
1631
- deepEqual(val._id, priorVal)) {
1631
+ deepEqual(val._doc._id, priorVal)) {
1632
1632
  return false;
1633
1633
  }
1634
1634
 
@@ -3840,7 +3840,7 @@ Document.prototype.$toObject = function(options, json) {
3840
3840
  // _isNested will only be true if this is not the top level document, we
3841
3841
  // should never depopulate the top-level document
3842
3842
  if (depopulate && options._isNested && this.$__.wasPopulated) {
3843
- return clone(this.$__.wasPopulated.value || this._id, options);
3843
+ return clone(this.$__.wasPopulated.value || this._doc._id, options);
3844
3844
  }
3845
3845
 
3846
3846
  // merge default options with input options.
@@ -17,7 +17,7 @@ const trustedSymbol = require('./query/trusted').trustedSymbol;
17
17
  *
18
18
  * If options.minimize is true, creates a minimal data object. Empty objects and undefined values will not be cloned. This makes the data payload sent to MongoDB as small as possible.
19
19
  *
20
- * Functions are never cloned.
20
+ * Functions and primitives are never cloned.
21
21
  *
22
22
  * @param {Object} obj the object to clone
23
23
  * @param {Object} options
@@ -30,6 +30,9 @@ function clone(obj, options, isArrayChild) {
30
30
  if (obj == null) {
31
31
  return obj;
32
32
  }
33
+ if (typeof obj === 'number' || typeof obj === 'string' || typeof obj === 'boolean' || typeof obj === 'bigint') {
34
+ return obj;
35
+ }
33
36
 
34
37
  if (Array.isArray(obj)) {
35
38
  return cloneArray(isMongooseArray(obj) ? obj.__array : obj, options);
@@ -148,13 +151,12 @@ function cloneObject(obj, options, isArrayChild) {
148
151
  ret[trustedSymbol] = obj[trustedSymbol];
149
152
  }
150
153
 
151
- let i = 0;
152
- let key = '';
153
154
  const keys = Object.keys(obj);
154
155
  const len = keys.length;
155
156
 
156
- for (i = 0; i < len; ++i) {
157
- if (specialProperties.has(key = keys[i])) {
157
+ for (let i = 0; i < len; ++i) {
158
+ const key = keys[i];
159
+ if (specialProperties.has(key)) {
158
160
  continue;
159
161
  }
160
162
 
@@ -93,8 +93,12 @@ function _castExpression(val, schema, strictQuery) {
93
93
  } else if (val.$ifNull != null) {
94
94
  val.$ifNull.map(v => _castExpression(v, schema, strictQuery));
95
95
  } else if (val.$switch != null) {
96
- val.branches.map(v => _castExpression(v, schema, strictQuery));
97
- val.default = _castExpression(val.default, schema, strictQuery);
96
+ if (Array.isArray(val.$switch.branches)) {
97
+ val.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery));
98
+ }
99
+ if ('default' in val.$switch) {
100
+ val.$switch.default = _castExpression(val.$switch.default, schema, strictQuery);
101
+ }
98
102
  }
99
103
 
100
104
  const keys = Object.keys(val);
package/lib/model.js CHANGED
@@ -290,9 +290,10 @@ Model.prototype.$__handleSave = function(options, callback) {
290
290
 
291
291
  const session = this.$session();
292
292
  const asyncLocalStorage = this[modelDbSymbol].base.transactionAsyncLocalStorage?.getStore();
293
- if (!saveOptions.hasOwnProperty('session') && session != null) {
293
+ if (session != null) {
294
294
  saveOptions.session = session;
295
- } else if (asyncLocalStorage?.session != null) {
295
+ } else if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
296
+ // Only set session from asyncLocalStorage if `session` option wasn't originally passed in options
296
297
  saveOptions.session = asyncLocalStorage.session;
297
298
  }
298
299
  if (this.$isNew) {
@@ -3630,7 +3631,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3630
3631
  const len = paths.length;
3631
3632
 
3632
3633
  for (let i = 0; i < len; ++i) {
3633
- where[paths[i]] = shardKey[paths[i]];
3634
+ where[paths[i]] = document[paths[i]];
3634
3635
  }
3635
3636
  }
3636
3637
 
@@ -4458,7 +4459,7 @@ function _assign(model, vals, mod, assignmentOpts) {
4458
4459
  }
4459
4460
  } else {
4460
4461
  if (_val instanceof Document) {
4461
- _val = _val._id;
4462
+ _val = _val._doc._id;
4462
4463
  }
4463
4464
  key = String(_val);
4464
4465
  if (rawDocs[key]) {
@@ -4467,7 +4468,7 @@ function _assign(model, vals, mod, assignmentOpts) {
4467
4468
  rawOrder[key].push(i);
4468
4469
  } else if (isVirtual ||
4469
4470
  rawDocs[key].constructor !== val.constructor ||
4470
- String(rawDocs[key]._id) !== String(val._id)) {
4471
+ String(rawDocs[key]._doc._id) !== String(val._doc._id)) {
4471
4472
  // May need to store multiple docs with the same id if there's multiple models
4472
4473
  // if we have discriminators or a ref function. But avoid converting to an array
4473
4474
  // if we have multiple queries on the same model because of `perDocumentLimit` re: gh-9906
@@ -69,7 +69,7 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {
69
69
 
70
70
  const fn = this.defaultValue;
71
71
 
72
- if (!('defaultValue' in this) || fn !== void 0) {
72
+ if (!('defaultValue' in this) || fn != null) {
73
73
  this.default(function() {
74
74
  let arr = fn.call(this);
75
75
  if (arr != null && !Array.isArray(arr)) {
package/lib/schemaType.js CHANGED
@@ -1542,7 +1542,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1542
1542
  }
1543
1543
 
1544
1544
  if (value.$__ != null) {
1545
- value.$__.wasPopulated = value.$__.wasPopulated || { value: value._id };
1545
+ value.$__.wasPopulated = value.$__.wasPopulated || { value: value._doc._id };
1546
1546
  return value;
1547
1547
  }
1548
1548
 
@@ -1568,7 +1568,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1568
1568
  !doc.$__.populated[path].options.options ||
1569
1569
  !doc.$__.populated[path].options.options.lean) {
1570
1570
  ret = new pop.options[populateModelSymbol](value);
1571
- ret.$__.wasPopulated = { value: ret._id };
1571
+ ret.$__.wasPopulated = { value: ret._doc._id };
1572
1572
  }
1573
1573
 
1574
1574
  return ret;
package/lib/types/map.js CHANGED
@@ -116,7 +116,7 @@ class MongooseMap extends Map {
116
116
  v = new populated.options[populateModelSymbol](v);
117
117
  }
118
118
  // Doesn't support single nested "in-place" populate
119
- v.$__.wasPopulated = { value: v._id };
119
+ v.$__.wasPopulated = { value: v._doc._id };
120
120
  return v;
121
121
  });
122
122
  } else if (value != null) {
@@ -124,7 +124,7 @@ class MongooseMap extends Map {
124
124
  value = new populated.options[populateModelSymbol](value);
125
125
  }
126
126
  // Doesn't support single nested "in-place" populate
127
- value.$__.wasPopulated = { value: value._id };
127
+ value.$__.wasPopulated = { value: value._doc._id };
128
128
  }
129
129
  } else {
130
130
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.5.1",
4
+ "version": "8.5.2",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -57,7 +57,7 @@
57
57
  "mkdirp": "^3.0.1",
58
58
  "mocha": "10.6.0",
59
59
  "moment": "2.30.1",
60
- "mongodb-memory-server": "9.4.0",
60
+ "mongodb-memory-server": "10.0.0",
61
61
  "ncp": "^2.0.0",
62
62
  "nyc": "15.1.0",
63
63
  "pug": "3.0.3",
@@ -220,7 +220,8 @@ declare module 'mongoose' {
220
220
  OptionsConstructor: SchemaTypeOptions<T>;
221
221
 
222
222
  /** Cast `val` to this schema type. Each class that inherits from schema type should implement this function. */
223
- cast(val: any, doc: Document<any>, init: boolean, prev?: any, options?: any): any;
223
+ cast(val: any, doc?: Document<any>, init?: boolean, prev?: any, options?: any): any;
224
+ cast<ResultType>(val: any, doc?: Document<any>, init?: boolean, prev?: any, options?: any): ResultType;
224
225
 
225
226
  /** Sets a default value for this SchemaType. */
226
227
  default(val: any): any;
@@ -443,7 +444,7 @@ declare module 'mongoose' {
443
444
  defaultOptions: Record<string, any>;
444
445
  }
445
446
 
446
- class Subdocument extends SchemaType implements AcceptsDiscriminator {
447
+ class Subdocument<DocType = unknown> extends SchemaType implements AcceptsDiscriminator {
447
448
  /** This schema type's name, to defend against minifiers that mangle function names. */
448
449
  static schemaName: string;
449
450
 
@@ -455,6 +456,8 @@ declare module 'mongoose' {
455
456
 
456
457
  discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
457
458
  discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
459
+
460
+ cast(val: any, doc?: Document<any>, init?: boolean, prev?: any, options?: any): HydratedSingleSubdocument<DocType>;
458
461
  }
459
462
 
460
463
  class String extends SchemaType {