mongoose 7.5.3 → 7.6.0

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/cast.js CHANGED
@@ -68,7 +68,13 @@ module.exports = function cast(schema, obj, options, context) {
68
68
  if (val[k] == null || typeof val[k] !== 'object') {
69
69
  throw new CastError('Object', val[k], path + '.' + k);
70
70
  }
71
- val[k] = cast(schema, val[k], options, context);
71
+ const discriminatorValue = val[k][schema.options.discriminatorKey];
72
+ if (discriminatorValue == null) {
73
+ val[k] = cast(schema, val[k], options, context);
74
+ } else {
75
+ const discriminatorSchema = getSchemaDiscriminatorByValue(context.schema, discriminatorValue);
76
+ val[k] = cast(discriminatorSchema ? discriminatorSchema : schema, val[k], options, context);
77
+ }
72
78
  }
73
79
  } else if (path === '$where') {
74
80
  type = typeof val;
@@ -303,7 +309,6 @@ module.exports = function cast(schema, obj, options, context) {
303
309
  } else {
304
310
  const ks = Object.keys(val);
305
311
  let $cond;
306
-
307
312
  let k = ks.length;
308
313
 
309
314
  while (k--) {
@@ -34,7 +34,7 @@ const util = require('util');
34
34
  * @api public
35
35
  */
36
36
 
37
- function QueryCursor(query, options) {
37
+ function QueryCursor(query) {
38
38
  // set autoDestroy=true because on node 12 it's by default false
39
39
  // gh-10902 need autoDestroy to destroy correctly and emit 'close' event
40
40
  Readable.call(this, { autoDestroy: true, objectMode: true });
@@ -46,7 +46,7 @@ function QueryCursor(query, options) {
46
46
  this._mongooseOptions = {};
47
47
  this._transforms = [];
48
48
  this.model = model;
49
- this.options = options || {};
49
+ this.options = {};
50
50
  model.hooks.execPre('find', query, (err) => {
51
51
  if (err != null) {
52
52
  if (err instanceof kareem.skipWrappedFunction) {
@@ -70,20 +70,18 @@ function QueryCursor(query, options) {
70
70
  this.listeners('error').length > 0 && this.emit('error', err);
71
71
  return;
72
72
  }
73
+ Object.assign(this.options, query._optionsForExec());
73
74
  this._transforms = this._transforms.concat(query._transforms.slice());
74
75
  if (this.options.transform) {
75
- this._transforms.push(options.transform);
76
+ this._transforms.push(this.options.transform);
76
77
  }
77
78
  // Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
78
79
  // `batchSize` option doesn't work.
79
80
  if (this.options.batchSize) {
80
- this.options.cursor = options.cursor || {};
81
- this.options.cursor.batchSize = options.batchSize;
82
-
83
81
  // Max out the number of documents we'll populate in parallel at 5000.
84
82
  this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
85
83
  }
86
- Object.assign(this.options, query._optionsForExec());
84
+
87
85
  if (model.collection._shouldBufferCommands() && model.collection.buffer) {
88
86
  model.collection.queue.push([
89
87
  () => _getRawCursor(query, this)
package/lib/document.js CHANGED
@@ -1060,10 +1060,6 @@ Document.prototype.$set = function $set(path, val, type, options) {
1060
1060
  [path, val] = [val, path];
1061
1061
  }
1062
1062
 
1063
- if ('_id' in path && 'id' in path) {
1064
- delete path.id;
1065
- }
1066
-
1067
1063
  prefix = val ? val + '.' : '';
1068
1064
  keys = getKeysInSchemaOrder(this.$__schema, path);
1069
1065
 
@@ -1694,6 +1690,13 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
1694
1690
  if (last) {
1695
1691
  if (obj instanceof Map) {
1696
1692
  obj.set(parts[i], val);
1693
+ } else if (obj.$isSingleNested) {
1694
+ if (!(parts[i] in obj)) {
1695
+ obj[parts[i]] = val;
1696
+ obj._doc[parts[i]] = val;
1697
+ } else {
1698
+ obj._doc[parts[i]] = val;
1699
+ }
1697
1700
  } else {
1698
1701
  obj[parts[i]] = val;
1699
1702
  }
@@ -1705,7 +1708,7 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
1705
1708
  } else if (value && value instanceof Embedded) {
1706
1709
  obj = value;
1707
1710
  } else if (value && !Array.isArray(value) && value.$isSingleNested) {
1708
- obj = value._doc;
1711
+ obj = value;
1709
1712
  } else if (value && Array.isArray(value)) {
1710
1713
  obj = value;
1711
1714
  } else if (value == null) {
@@ -2650,7 +2653,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
2650
2653
  const modifiedPaths = doc.modifiedPaths();
2651
2654
  for (const subdoc of subdocs) {
2652
2655
  if (subdoc.$basePath) {
2653
- const fullPathToSubdoc = subdoc.$__fullPathWithIndexes();
2656
+ const fullPathToSubdoc = subdoc.$isSingleNested ? subdoc.$__pathRelativeToParent() : subdoc.$__fullPathWithIndexes();
2654
2657
 
2655
2658
  // Remove child paths for now, because we'll be validating the whole
2656
2659
  // subdoc.
@@ -29,7 +29,6 @@ const arithmeticOperatorNumber = new Set([
29
29
  '$floor',
30
30
  '$ln',
31
31
  '$log10',
32
- '$round',
33
32
  '$sqrt',
34
33
  '$sin',
35
34
  '$cos',
@@ -118,6 +117,13 @@ function _castExpression(val, schema, strictQuery) {
118
117
  if (val.$size) {
119
118
  val.$size = castNumberOperator(val.$size, schema, strictQuery);
120
119
  }
120
+ if (val.$round) {
121
+ const $round = val.$round;
122
+ if (!Array.isArray($round) || $round.length < 1 || $round.length > 2) {
123
+ throw new CastError('Array', $round, '$round');
124
+ }
125
+ val.$round = $round.map(v => castNumberOperator(v, schema, strictQuery));
126
+ }
121
127
 
122
128
  _omitUndefined(val);
123
129
 
package/lib/index.js CHANGED
@@ -694,7 +694,9 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
694
694
 
695
695
  options = options || {};
696
696
  options.applyPluginsToDiscriminators = _mongoose.options && _mongoose.options.applyPluginsToDiscriminators || false;
697
- options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToDiscriminators) === 'boolean' ? _mongoose.options.applyPluginsToDiscriminators : true;
697
+ options.applyPluginsToChildSchemas = typeof (_mongoose.options && _mongoose.options.applyPluginsToChildSchemas) === 'boolean' ?
698
+ _mongoose.options.applyPluginsToChildSchemas :
699
+ true;
698
700
  applyPlugins(schema, _mongoose.plugins, options, '$globalPluginsApplied');
699
701
  };
700
702
 
package/lib/query.js CHANGED
@@ -4889,7 +4889,6 @@ function _getPopulatedPaths(list, arr, prefix) {
4889
4889
 
4890
4890
  Query.prototype.cast = function(model, obj) {
4891
4891
  obj || (obj = this._conditions);
4892
-
4893
4892
  model = model || this.model;
4894
4893
  const discriminatorKey = model.schema.options.discriminatorKey;
4895
4894
  if (obj != null &&
@@ -5043,15 +5042,13 @@ Query.prototype.cursor = function cursor(opts) {
5043
5042
  this.setOptions(opts);
5044
5043
  }
5045
5044
 
5046
- const options = this._optionsForExec();
5047
-
5048
5045
  try {
5049
5046
  this.cast(this.model);
5050
5047
  } catch (err) {
5051
- return (new QueryCursor(this, options))._markError(err);
5048
+ return (new QueryCursor(this))._markError(err);
5052
5049
  }
5053
5050
 
5054
- return new QueryCursor(this, options);
5051
+ return new QueryCursor(this);
5055
5052
  };
5056
5053
 
5057
5054
  // the rest of these are basically to support older Mongoose syntax with mquery
@@ -199,6 +199,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
199
199
  // projection if `name` has schema-level `select: true`.
200
200
  if ((!type || !type.selected) || exclude !== false) {
201
201
  fields[path] = 0;
202
+ exclude = true;
202
203
  } else if (type && type.selected && exclude === false) {
203
204
  // Make a note of minus paths that are overwriting paths that are
204
205
  // included by default.
package/lib/schematype.js CHANGED
@@ -1215,7 +1215,6 @@ SchemaType.prototype.applySetters = function(value, scope, init, priorVal, optio
1215
1215
  if (v == null) {
1216
1216
  return this._castNullish(v);
1217
1217
  }
1218
-
1219
1218
  // do not cast until all setters are applied #665
1220
1219
  v = this.cast(v, scope, init, priorVal, options);
1221
1220
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "7.5.3",
4
+ "version": "7.6.0",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -21,16 +21,16 @@
21
21
  "dependencies": {
22
22
  "bson": "^5.4.0",
23
23
  "kareem": "2.5.1",
24
- "mongodb": "5.8.1",
24
+ "mongodb": "5.9.0",
25
25
  "mpath": "0.9.0",
26
26
  "mquery": "5.0.0",
27
27
  "ms": "2.1.3",
28
28
  "sift": "16.0.1"
29
29
  },
30
30
  "devDependencies": {
31
- "@babel/core": "7.22.11",
32
- "@babel/preset-env": "7.22.14",
33
- "@typescript-eslint/eslint-plugin": "5.61.0",
31
+ "@babel/core": "7.23.0",
32
+ "@babel/preset-env": "7.22.20",
33
+ "@typescript-eslint/eslint-plugin": "5.62.0",
34
34
  "@typescript-eslint/parser": "5.62.0",
35
35
  "acquit": "1.3.0",
36
36
  "acquit-ignore": "0.2.1",
@@ -46,7 +46,7 @@
46
46
  "crypto-browserify": "3.12.0",
47
47
  "dotenv": "16.3.1",
48
48
  "dox": "1.0.0",
49
- "eslint": "8.48.0",
49
+ "eslint": "8.50.0",
50
50
  "eslint-plugin-markdown": "^3.0.0",
51
51
  "eslint-plugin-mocha-no-only": "1.1.1",
52
52
  "express": "^4.18.1",
@@ -54,7 +54,7 @@
54
54
  "highlight.js": "11.8.0",
55
55
  "lodash.isequal": "4.5.0",
56
56
  "lodash.isequalwith": "4.4.0",
57
- "markdownlint-cli2": "^0.9.2",
57
+ "markdownlint-cli2": "^0.10.0",
58
58
  "marked": "4.3.0",
59
59
  "mkdirp": "^3.0.1",
60
60
  "mocha": "10.2.0",
@@ -64,11 +64,11 @@
64
64
  "nyc": "15.1.0",
65
65
  "pug": "3.0.2",
66
66
  "q": "1.5.1",
67
- "sinon": "15.2.0",
67
+ "sinon": "16.0.0",
68
68
  "stream-browserify": "3.0.0",
69
69
  "tsd": "0.29.0",
70
70
  "typescript": "5.2.2",
71
- "uuid": "9.0.0",
71
+ "uuid": "9.0.1",
72
72
  "webpack": "5.88.2"
73
73
  },
74
74
  "directories": {
@@ -132,6 +132,7 @@ async function run() {
132
132
  await Content.init();
133
133
 
134
134
  await Content.deleteMany({ version });
135
+ let count = 0;
135
136
  for (const content of contents) {
136
137
  if (version === '7.x') {
137
138
  let url = content.url.startsWith('/') ? content.url : `/${content.url}`;
@@ -143,6 +144,7 @@ async function run() {
143
144
  const url = content.url.startsWith('/') ? content.url : `/${content.url}`;
144
145
  content.url = `/docs/${version}/docs${url}`;
145
146
  }
147
+ console.log(`${++count} / ${contents.length}`);
146
148
  await content.save();
147
149
  }
148
150
 
package/types/models.d.ts CHANGED
@@ -400,6 +400,10 @@ declare module 'mongoose' {
400
400
  doc: DocContents,
401
401
  options: InsertManyOptions
402
402
  ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
403
+ insertMany<DocContents = TRawDocType>(
404
+ docs: Array<DocContents | TRawDocType>,
405
+ options: InsertManyOptions
406
+ ): Promise<Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>>;
403
407
  insertMany<DocContents = TRawDocType>(
404
408
  doc: DocContents
405
409
  ): Promise<