mongoose 5.11.13 → 5.11.17

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.
@@ -261,12 +261,19 @@ SchemaArray.prototype.enum = function() {
261
261
  */
262
262
 
263
263
  SchemaArray.prototype.applyGetters = function(value, scope) {
264
- if (this.caster.options && this.caster.options.ref) {
264
+ if (scope != null && scope.populated(this.path)) {
265
265
  // means the object id was populated
266
266
  return value;
267
267
  }
268
268
 
269
- return SchemaType.prototype.applyGetters.call(this, value, scope);
269
+ const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
270
+ if (Array.isArray(ret)) {
271
+ const len = ret.length;
272
+ for (let i = 0; i < len; ++i) {
273
+ ret[i] = this.caster.applyGetters(ret[i], scope);
274
+ }
275
+ }
276
+ return ret;
270
277
  };
271
278
 
272
279
  SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
@@ -348,7 +355,7 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
348
355
  }
349
356
 
350
357
  const isPopulated = doc != null && doc.$__ != null && doc.populated(this.path);
351
- if (isPopulated) {
358
+ if (isPopulated && init) {
352
359
  return value;
353
360
  }
354
361
 
@@ -479,6 +479,14 @@ DocumentArrayPath.prototype.clone = function() {
479
479
  return schematype;
480
480
  };
481
481
 
482
+ /*!
483
+ * ignore
484
+ */
485
+
486
+ DocumentArrayPath.prototype.applyGetters = function(value, scope) {
487
+ return SchemaType.prototype.applyGetters.call(this, value, scope);
488
+ };
489
+
482
490
  /*!
483
491
  * Scopes paths selected in a query to this array.
484
492
  * Necessary for proper default application of subdocument values.
package/lib/schema.js CHANGED
@@ -694,18 +694,18 @@ Schema.prototype.path = function(path, obj) {
694
694
  }
695
695
 
696
696
  if (schemaType.$isSingleNested) {
697
- for (const key in schemaType.schema.paths) {
697
+ for (const key of Object.keys(schemaType.schema.paths)) {
698
698
  this.singleNestedPaths[path + '.' + key] = schemaType.schema.paths[key];
699
699
  }
700
- for (const key in schemaType.schema.singleNestedPaths) {
700
+ for (const key of Object.keys(schemaType.schema.singleNestedPaths)) {
701
701
  this.singleNestedPaths[path + '.' + key] =
702
702
  schemaType.schema.singleNestedPaths[key];
703
703
  }
704
- for (const key in schemaType.schema.subpaths) {
704
+ for (const key of Object.keys(schemaType.schema.subpaths)) {
705
705
  this.singleNestedPaths[path + '.' + key] =
706
706
  schemaType.schema.subpaths[key];
707
707
  }
708
- for (const key in schemaType.schema.nested) {
708
+ for (const key of Object.keys(schemaType.schema.nested)) {
709
709
  this.singleNestedPaths[path + '.' + key] = 'nested';
710
710
  }
711
711
 
@@ -1156,7 +1156,7 @@ Schema.prototype.hasMixedParent = function(path) {
1156
1156
  path = '';
1157
1157
  for (let i = 0; i < subpaths.length; ++i) {
1158
1158
  path = i > 0 ? path + '.' + subpaths[i] : subpaths[i];
1159
- if (path in this.paths &&
1159
+ if (this.paths.hasOwnProperty(path) &&
1160
1160
  this.paths[path] instanceof MongooseTypes.Mixed) {
1161
1161
  return this.paths[path];
1162
1162
  }
package/lib/schematype.js CHANGED
@@ -15,8 +15,6 @@ const schemaTypeSymbol = require('./helpers/symbols').schemaTypeSymbol;
15
15
  const util = require('util');
16
16
  const utils = require('./utils');
17
17
  const validatorErrorSymbol = require('./helpers/symbols').validatorErrorSymbol;
18
-
19
- const documentIsSelected = require('./helpers/symbols').documentIsSelected;
20
18
  const documentIsModified = require('./helpers/symbols').documentIsModified;
21
19
 
22
20
  const CastError = MongooseError.CastError;
@@ -953,7 +951,7 @@ SchemaType.prototype.required = function(required, message) {
953
951
  const cachedRequired = get(this, '$__.cachedRequired');
954
952
 
955
953
  // no validation when this path wasn't selected in the query.
956
- if (cachedRequired != null && !this[documentIsSelected](_this.path) && !this[documentIsModified](_this.path)) {
954
+ if (cachedRequired != null && !this.$__isSelected(_this.path) && !this[documentIsModified](_this.path)) {
957
955
  return true;
958
956
  }
959
957
 
@@ -1051,8 +1049,11 @@ SchemaType.prototype.getDefault = function(scope, init) {
1051
1049
  * @api private
1052
1050
  */
1053
1051
 
1054
- SchemaType.prototype._applySetters = function(value, scope) {
1052
+ SchemaType.prototype._applySetters = function(value, scope, init) {
1055
1053
  let v = value;
1054
+ if (init) {
1055
+ return v;
1056
+ }
1056
1057
  const setters = this.setters;
1057
1058
 
1058
1059
  for (const setter of utils.clone(setters).reverse()) {
@@ -284,7 +284,7 @@ class CoreMongooseArray extends Array {
284
284
  * @memberOf MongooseArray
285
285
  */
286
286
 
287
- _markModified(elem, embeddedPath) {
287
+ _markModified(elem) {
288
288
  const parent = this[arrayParentSymbol];
289
289
  let dirtyPath;
290
290
 
@@ -292,13 +292,7 @@ class CoreMongooseArray extends Array {
292
292
  dirtyPath = this[arrayPathSymbol];
293
293
 
294
294
  if (arguments.length) {
295
- if (embeddedPath != null) {
296
- // an embedded doc bubbled up the change
297
- dirtyPath = dirtyPath + '.' + this.indexOf(elem) + '.' + embeddedPath;
298
- } else {
299
- // directly set an index
300
- dirtyPath = dirtyPath + '.' + elem;
301
- }
295
+ dirtyPath = dirtyPath + '.' + elem;
302
296
  }
303
297
 
304
298
  if (dirtyPath != null && dirtyPath.endsWith('.$')) {
@@ -330,6 +330,34 @@ class CoreDocumentArray extends CoreMongooseArray {
330
330
  }
331
331
  };
332
332
  }
333
+
334
+ _markModified(elem, embeddedPath) {
335
+ const parent = this[arrayParentSymbol];
336
+ let dirtyPath;
337
+
338
+ if (parent) {
339
+ dirtyPath = this[arrayPathSymbol];
340
+
341
+ if (arguments.length) {
342
+ if (embeddedPath != null) {
343
+ // an embedded doc bubbled up the change
344
+ const index = elem.__index;
345
+ dirtyPath = dirtyPath + '.' + index + '.' + embeddedPath;
346
+ } else {
347
+ // directly set an index
348
+ dirtyPath = dirtyPath + '.' + elem;
349
+ }
350
+ }
351
+
352
+ if (dirtyPath != null && dirtyPath.endsWith('.$')) {
353
+ return this;
354
+ }
355
+
356
+ parent.markModified(dirtyPath, arguments.length > 0 ? elem : parent);
357
+ }
358
+
359
+ return this;
360
+ }
333
361
  }
334
362
 
335
363
  if (util.inspect.custom) {
@@ -106,7 +106,7 @@ EmbeddedDocument.prototype.markModified = function(path) {
106
106
  }
107
107
 
108
108
  const pathToCheck = this.__parentArray.$path() + '.0.' + path;
109
- if (this.isNew && this.ownerDocument().isSelected(pathToCheck)) {
109
+ if (this.isNew && this.ownerDocument().$__isSelected(pathToCheck)) {
110
110
  // Mark the WHOLE parent array as modified
111
111
  // if this is a new document (i.e., we are initializing
112
112
  // a document),
@@ -201,6 +201,9 @@ function registerRemoveListener(sub) {
201
201
  */
202
202
 
203
203
  EmbeddedDocument.prototype.$__remove = function(cb) {
204
+ if (cb == null) {
205
+ return;
206
+ }
204
207
  return cb(null, this);
205
208
  };
206
209
 
@@ -218,7 +221,7 @@ EmbeddedDocument.prototype.remove = function(options, fn) {
218
221
  options = undefined;
219
222
  }
220
223
  if (!this.__parentArray || (options && options.noop)) {
221
- fn && fn(null);
224
+ this.$__remove(fn);
222
225
  return this;
223
226
  }
224
227
 
@@ -234,9 +237,7 @@ EmbeddedDocument.prototype.remove = function(options, fn) {
234
237
  registerRemoveListener(this);
235
238
  }
236
239
 
237
- if (fn) {
238
- fn(null);
239
- }
240
+ this.$__remove(fn);
240
241
 
241
242
  return this;
242
243
  };
package/lib/types/map.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Mixed = require('../schema/mixed');
4
+ const ObjectId = require('./objectid');
4
5
  const deepEqual = require('../utils').deepEqual;
5
6
  const get = require('../helpers/get');
6
7
  const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
@@ -19,7 +20,6 @@ class MongooseMap extends Map {
19
20
  v = Object.keys(v).reduce((arr, key) => arr.concat([[key, v[key]]]), []);
20
21
  }
21
22
  super(v);
22
-
23
23
  this.$__parent = doc != null && doc.$__ != null ? doc : null;
24
24
  this.$__path = path;
25
25
  this.$__schemaType = schemaType == null ? new Mixed(path) : schemaType;
@@ -42,6 +42,10 @@ class MongooseMap extends Map {
42
42
  }
43
43
 
44
44
  get(key, options) {
45
+ if (key instanceof ObjectId) {
46
+ key = key.toString();
47
+ }
48
+
45
49
  options = options || {};
46
50
  if (options.getters === false) {
47
51
  return super.get(key);
@@ -50,6 +54,10 @@ class MongooseMap extends Map {
50
54
  }
51
55
 
52
56
  set(key, value) {
57
+ if (key instanceof ObjectId) {
58
+ key = key.toString();
59
+ }
60
+
53
61
  checkValidKey(key);
54
62
  value = handleSpreadDoc(value);
55
63
 
@@ -77,7 +85,7 @@ class MongooseMap extends Map {
77
85
  } else {
78
86
  try {
79
87
  value = this.$__schemaType.
80
- applySetters(value, this.$__parent, false, this.get(key));
88
+ applySetters(value, this.$__parent, false, this.get(key), { path: fullPath });
81
89
  } catch (error) {
82
90
  if (this.$__parent != null && this.$__parent.$__ != null) {
83
91
  this.$__parent.invalidate(fullPath, error);
@@ -108,6 +116,10 @@ class MongooseMap extends Map {
108
116
  }
109
117
 
110
118
  delete(key) {
119
+ if (key instanceof ObjectId) {
120
+ key = key.toString();
121
+ }
122
+
111
123
  this.set(key, undefined);
112
124
  super.delete(key);
113
125
  }
@@ -18,7 +18,9 @@ module.exports = Subdocument;
18
18
 
19
19
  function Subdocument(value, fields, parent, skipId, options) {
20
20
  this.$isSingleNested = true;
21
-
21
+ if (options != null && options.path != null) {
22
+ this.$basePath = options.path;
23
+ }
22
24
  const hasPriorDoc = options != null && options.priorDoc;
23
25
  let initedPaths = null;
24
26
  if (hasPriorDoc) {
@@ -253,7 +255,6 @@ Subdocument.prototype.remove = function(options, callback) {
253
255
  callback = options;
254
256
  options = null;
255
257
  }
256
-
257
258
  registerRemoveListener(this);
258
259
 
259
260
  // If removing entire doc, no need to remove subdoc
package/lib/utils.js CHANGED
@@ -332,7 +332,7 @@ exports.toObject = function toObject(obj) {
332
332
  if (exports.isPOJO(obj)) {
333
333
  ret = {};
334
334
 
335
- for (const k in obj) {
335
+ for (const k of Object.keys(obj)) {
336
336
  if (specialProperties.has(k)) {
337
337
  continue;
338
338
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "5.11.13",
4
+ "version": "5.11.17",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -22,10 +22,10 @@
22
22
  "@types/mongodb": "^3.5.27",
23
23
  "bson": "^1.1.4",
24
24
  "kareem": "2.3.2",
25
- "mongodb": "3.6.3",
25
+ "mongodb": "3.6.4",
26
26
  "mongoose-legacy-pluralize": "1.0.2",
27
27
  "mpath": "0.8.3",
28
- "mquery": "3.2.3",
28
+ "mquery": "3.2.4",
29
29
  "ms": "2.1.2",
30
30
  "regexp-clone": "1.0.0",
31
31
  "safe-buffer": "5.2.1",
@@ -120,7 +120,8 @@
120
120
  ],
121
121
  "rules": {
122
122
  "@typescript-eslint/no-explicit-any": "off",
123
- "@typescript-eslint/ban-types": "off"
123
+ "@typescript-eslint/ban-types": "off",
124
+ "@typescript-eslint/no-unused-vars": "off"
124
125
  }
125
126
  }
126
127
  ],