mongoose 6.5.3 → 6.5.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.
Files changed (80) hide show
  1. package/.mocharc.yml +2 -0
  2. package/lib/aggregate.js +2 -1
  3. package/lib/cast/boolean.js +1 -1
  4. package/lib/cast/number.js +1 -1
  5. package/lib/cast/string.js +1 -1
  6. package/lib/cast.js +3 -0
  7. package/lib/connection.js +9 -1
  8. package/lib/cursor/AggregationCursor.js +14 -3
  9. package/lib/cursor/QueryCursor.js +14 -3
  10. package/lib/document.js +27 -14
  11. package/lib/drivers/browser/objectid.js +2 -1
  12. package/lib/drivers/node-mongodb-native/ReadPreference.js +1 -1
  13. package/lib/drivers/node-mongodb-native/collection.js +6 -2
  14. package/lib/error/browserMissingSchema.js +1 -1
  15. package/lib/error/cast.js +2 -1
  16. package/lib/error/divergentArray.js +2 -1
  17. package/lib/error/missingSchema.js +2 -1
  18. package/lib/error/notFound.js +2 -1
  19. package/lib/error/overwriteModel.js +2 -1
  20. package/lib/error/validation.js +9 -7
  21. package/lib/error/validator.js +8 -4
  22. package/lib/helpers/clone.js +1 -1
  23. package/lib/helpers/discriminator/getConstructor.js +2 -1
  24. package/lib/helpers/discriminator/getDiscriminatorByValue.js +7 -6
  25. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +7 -6
  26. package/lib/helpers/document/compile.js +14 -2
  27. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +5 -1
  28. package/lib/helpers/get.js +2 -1
  29. package/lib/helpers/getConstructorName.js +2 -1
  30. package/lib/helpers/isBsonType.js +2 -1
  31. package/lib/helpers/isMongooseObject.js +2 -2
  32. package/lib/helpers/isObject.js +1 -1
  33. package/lib/helpers/isSimpleValidator.js +1 -1
  34. package/lib/helpers/model/applyHooks.js +3 -1
  35. package/lib/helpers/model/applyMethods.js +2 -1
  36. package/lib/helpers/model/applyStatics.js +2 -1
  37. package/lib/helpers/model/castBulkWrite.js +7 -2
  38. package/lib/helpers/model/discriminator.js +3 -1
  39. package/lib/helpers/pluralize.js +1 -1
  40. package/lib/helpers/populate/assignRawDocsToIdStructure.js +5 -3
  41. package/lib/helpers/populate/assignVals.js +14 -3
  42. package/lib/helpers/populate/createPopulateQueryFilter.js +8 -2
  43. package/lib/helpers/populate/getModelsMapForPopulate.js +3 -1
  44. package/lib/helpers/populate/getSchemaTypes.js +3 -1
  45. package/lib/helpers/populate/markArraySubdocsPopulated.js +5 -1
  46. package/lib/helpers/projection/hasIncludedChildren.js +4 -1
  47. package/lib/helpers/projection/isPathExcluded.js +3 -2
  48. package/lib/helpers/projection/isSubpath.js +2 -1
  49. package/lib/helpers/query/applyQueryMiddleware.js +3 -2
  50. package/lib/helpers/query/castUpdate.js +14 -8
  51. package/lib/helpers/query/completeMany.js +3 -2
  52. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +7 -1
  53. package/lib/helpers/query/wrapThunk.js +3 -1
  54. package/lib/helpers/schema/getIndexes.js +7 -2
  55. package/lib/helpers/schema/getPath.js +4 -3
  56. package/lib/helpers/schema/idGetter.js +2 -1
  57. package/lib/index.js +10 -2
  58. package/lib/model.js +60 -26
  59. package/lib/query.js +124 -29
  60. package/lib/queryhelpers.js +4 -4
  61. package/lib/schema/SubdocumentPath.js +1 -1
  62. package/lib/schema/array.js +1 -1
  63. package/lib/schema/date.js +2 -1
  64. package/lib/schema/documentarray.js +3 -2
  65. package/lib/schema/operators/helpers.js +2 -2
  66. package/lib/schema/operators/text.js +1 -1
  67. package/lib/schema.js +11 -3
  68. package/lib/schematype.js +17 -4
  69. package/lib/statemachine.js +13 -12
  70. package/lib/types/ArraySubdocument.js +13 -3
  71. package/lib/types/DocumentArray/methods/index.js +7 -3
  72. package/lib/types/array/methods/index.js +1 -0
  73. package/lib/types/map.js +3 -1
  74. package/lib/types/objectid.js +2 -2
  75. package/lib/types/subdocument.js +30 -8
  76. package/lib/utils.js +58 -31
  77. package/package.json +1 -1
  78. package/types/document.d.ts +1 -1
  79. package/types/middlewares.d.ts +1 -1
  80. package/types/models.d.ts +1 -1
@@ -12,7 +12,7 @@ const getDiscriminatorByValue =
12
12
  const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
13
13
  const clone = require('./helpers/clone');
14
14
 
15
- /*!
15
+ /**
16
16
  * Prepare a set of path options for query population.
17
17
  *
18
18
  * @param {Query} query
@@ -38,7 +38,7 @@ exports.preparePopulationOptions = function preparePopulationOptions(query, opti
38
38
  return pop;
39
39
  };
40
40
 
41
- /*!
41
+ /**
42
42
  * Prepare a set of path options for query population. This is the MongooseQuery
43
43
  * version
44
44
  *
@@ -82,7 +82,7 @@ exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query,
82
82
  return pop;
83
83
  };
84
84
 
85
- /*!
85
+ /**
86
86
  * If the document is a mapped discriminator type, it returns a model instance for that type, otherwise,
87
87
  * it returns an instance of the given model.
88
88
  *
@@ -321,7 +321,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
321
321
  }
322
322
  };
323
323
 
324
- /*!
324
+ /**
325
325
  * Set each path query option to lean
326
326
  *
327
327
  * @param {Object} option
@@ -107,7 +107,7 @@ function _createConstructor(schema, baseClass) {
107
107
  return _embedded;
108
108
  }
109
109
 
110
- /*!
110
+ /**
111
111
  * Special case for when users use a common location schema to represent
112
112
  * locations for use with $geoWithin.
113
113
  * https://docs.mongodb.org/manual/reference/operator/query/geoWithin/
@@ -448,7 +448,7 @@ SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
448
448
  SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
449
449
 
450
450
  /*!
451
- * Ignore
451
+ * ignore
452
452
  */
453
453
 
454
454
  SchemaArray.prototype.discriminator = function(name, schema) {
@@ -355,9 +355,10 @@ SchemaDate.prototype.cast = function(value) {
355
355
  }
356
356
  };
357
357
 
358
- /*!
358
+ /**
359
359
  * Date Query casting.
360
360
  *
361
+ * @param {Any} val
361
362
  * @api private
362
363
  */
363
364
 
@@ -123,7 +123,7 @@ DocumentArrayPath.prototype.constructor = DocumentArrayPath;
123
123
  DocumentArrayPath.prototype.OptionsConstructor = SchemaDocumentArrayOptions;
124
124
 
125
125
  /*!
126
- * Ignore
126
+ * ignore
127
127
  */
128
128
 
129
129
  function _createConstructor(schema, options, baseClass) {
@@ -545,13 +545,14 @@ DocumentArrayPath.prototype.applyGetters = function(value, scope) {
545
545
  return SchemaType.prototype.applyGetters.call(this, value, scope);
546
546
  };
547
547
 
548
- /*!
548
+ /**
549
549
  * Scopes paths selected in a query to this array.
550
550
  * Necessary for proper default application of subdocument values.
551
551
  *
552
552
  * @param {DocumentArrayPath} array the array to scope `fields` paths
553
553
  * @param {Object|undefined} fields the root fields selected in the query
554
554
  * @param {Boolean|undefined} init if we are being created part of a query result
555
+ * @api private
555
556
  */
556
557
 
557
558
  function scopePaths(array, fields, init) {
@@ -7,14 +7,14 @@
7
7
  const SchemaNumber = require('../number');
8
8
 
9
9
  /*!
10
- * @ignore
10
+ * ignore
11
11
  */
12
12
 
13
13
  exports.castToNumber = castToNumber;
14
14
  exports.castArraysOfNumbers = castArraysOfNumbers;
15
15
 
16
16
  /*!
17
- * @ignore
17
+ * ignore
18
18
  */
19
19
 
20
20
  function castToNumber(val) {
@@ -4,7 +4,7 @@ const CastError = require('../../error/cast');
4
4
  const castBoolean = require('../../cast/boolean');
5
5
  const castString = require('../../cast/string');
6
6
 
7
- /*!
7
+ /**
8
8
  * Casts val to an object suitable for `$text`. Throws an error if the object
9
9
  * can't be casted.
10
10
  *
package/lib/schema.js CHANGED
@@ -165,8 +165,9 @@ function Schema(obj, options) {
165
165
  this.setupTimestamp(this.options.timestamps);
166
166
  }
167
167
 
168
- /*!
168
+ /**
169
169
  * Create virtual properties with alias field
170
+ * @api private
170
171
  */
171
172
  function aliasFields(schema, paths) {
172
173
  paths = paths || Object.keys(schema.paths);
@@ -1482,8 +1483,11 @@ Schema.prototype.setupTimestamp = function(timestamps) {
1482
1483
  return setupTimestamps(this, timestamps);
1483
1484
  };
1484
1485
 
1485
- /*!
1486
+ /**
1486
1487
  * ignore. Deprecated re: #6405
1488
+ * @param {Any} self
1489
+ * @param {String} path
1490
+ * @api private
1487
1491
  */
1488
1492
 
1489
1493
  function getPositionalPathType(self, path) {
@@ -2427,9 +2431,13 @@ function isArrayFilter(piece) {
2427
2431
  return piece.startsWith('$[') && piece.endsWith(']');
2428
2432
  }
2429
2433
 
2430
- /*!
2434
+ /**
2431
2435
  * Called by `compile()` _right before_ compiling. Good for making any changes to
2432
2436
  * the schema that should respect options set by plugins, like `id`
2437
+ * @method _preCompile
2438
+ * @memberOf Schema
2439
+ * @instance
2440
+ * @api private
2433
2441
  */
2434
2442
 
2435
2443
  Schema.prototype._preCompile = function _preCompile() {
package/lib/schematype.js CHANGED
@@ -124,8 +124,11 @@ function SchemaType(path, options, instance) {
124
124
  });
125
125
  }
126
126
 
127
- /*!
127
+ /**
128
128
  * The class that Mongoose uses internally to instantiate this SchemaType's `options` property.
129
+ * @memberOf SchemaType
130
+ * @instance
131
+ * @api private
129
132
  */
130
133
 
131
134
  SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
@@ -1144,9 +1147,15 @@ SchemaType.prototype.getDefault = function(scope, init, options) {
1144
1147
  return ret;
1145
1148
  };
1146
1149
 
1147
- /*!
1150
+ /**
1148
1151
  * Applies setters without casting
1149
1152
  *
1153
+ * @param {Any} value
1154
+ * @param {Any} scope
1155
+ * @param {Boolean} init
1156
+ * @param {Any} priorVal
1157
+ * @param {Object} [options]
1158
+ * @instance
1150
1159
  * @api private
1151
1160
  */
1152
1161
 
@@ -1547,9 +1556,10 @@ function handleArray(val) {
1547
1556
  });
1548
1557
  }
1549
1558
 
1550
- /*!
1559
+ /**
1551
1560
  * Just like handleArray, except also allows `[]` because surprisingly
1552
1561
  * `$in: [1, []]` works fine
1562
+ * @api private
1553
1563
  */
1554
1564
 
1555
1565
  function handle$in(val) {
@@ -1579,8 +1589,11 @@ SchemaType.prototype.$conditionalHandlers = {
1579
1589
  $type: $type
1580
1590
  };
1581
1591
 
1582
- /*!
1592
+ /**
1583
1593
  * Wraps `castForQuery` to handle context
1594
+ * @param {Object} params
1595
+ * @instance
1596
+ * @api private
1584
1597
  */
1585
1598
 
1586
1599
  SchemaType.prototype.castForQueryWrapper = function(params) {
@@ -7,7 +7,7 @@
7
7
 
8
8
  const utils = require('./utils'); // eslint-disable-line no-unused-vars
9
9
 
10
- /*!
10
+ /**
11
11
  * StateMachine represents a minimal `interface` for the
12
12
  * constructors it builds via StateMachine.ctor(...).
13
13
  *
@@ -17,7 +17,7 @@ const utils = require('./utils'); // eslint-disable-line no-unused-vars
17
17
  const StateMachine = module.exports = exports = function StateMachine() {
18
18
  };
19
19
 
20
- /*!
20
+ /**
21
21
  * StateMachine.ctor('state1', 'state2', ...)
22
22
  * A factory method for subclassing StateMachine.
23
23
  * The arguments are a list of states. For each state,
@@ -28,7 +28,7 @@ const StateMachine = module.exports = exports = function StateMachine() {
28
28
  * @param {String} state
29
29
  * @param {String} [state]
30
30
  * @return {Function} subclass constructor
31
- * @private
31
+ * @api private
32
32
  */
33
33
 
34
34
  StateMachine.ctor = function() {
@@ -54,7 +54,7 @@ StateMachine.ctor = function() {
54
54
  return ctor;
55
55
  };
56
56
 
57
- /*!
57
+ /**
58
58
  * This function is wrapped by the state change functions:
59
59
  *
60
60
  * - `require(path)`
@@ -105,8 +105,9 @@ StateMachine.prototype.clearPath = function clearPath(path) {
105
105
  delete this.states[state][path];
106
106
  };
107
107
 
108
- /*!
108
+ /**
109
109
  * Gets the paths for the given state, or empty object `{}` if none.
110
+ * @api private
110
111
  */
111
112
 
112
113
  StateMachine.prototype.getStatePaths = function getStatePaths(state) {
@@ -116,12 +117,12 @@ StateMachine.prototype.getStatePaths = function getStatePaths(state) {
116
117
  return {};
117
118
  };
118
119
 
119
- /*!
120
+ /**
120
121
  * Checks to see if at least one path is in the states passed in via `arguments`
121
122
  * e.g., this.some('required', 'inited')
122
123
  *
123
124
  * @param {String} state that we want to check for.
124
- * @private
125
+ * @api private
125
126
  */
126
127
 
127
128
  StateMachine.prototype.some = function some() {
@@ -135,7 +136,7 @@ StateMachine.prototype.some = function some() {
135
136
  });
136
137
  };
137
138
 
138
- /*!
139
+ /**
139
140
  * This function builds the functions that get assigned to `forEach` and `map`,
140
141
  * since both of those methods share a lot of the same logic.
141
142
  *
@@ -166,7 +167,7 @@ StateMachine.prototype._iter = function _iter(iterMethod) {
166
167
  };
167
168
  };
168
169
 
169
- /*!
170
+ /**
170
171
  * Iterates over the paths that belong to one of the parameter states.
171
172
  *
172
173
  * The function profile can look like:
@@ -177,7 +178,7 @@ StateMachine.prototype._iter = function _iter(iterMethod) {
177
178
  * @param {String} [state]
178
179
  * @param {String} [state]
179
180
  * @param {Function} callback
180
- * @private
181
+ * @api private
181
182
  */
182
183
 
183
184
  StateMachine.prototype.forEach = function forEach() {
@@ -185,7 +186,7 @@ StateMachine.prototype.forEach = function forEach() {
185
186
  return this.forEach.apply(this, arguments);
186
187
  };
187
188
 
188
- /*!
189
+ /**
189
190
  * Maps over the paths that belong to one of the parameter states.
190
191
  *
191
192
  * The function profile can look like:
@@ -197,7 +198,7 @@ StateMachine.prototype.forEach = function forEach() {
197
198
  * @param {String} [state]
198
199
  * @param {Function} callback
199
200
  * @return {Array}
200
- * @private
201
+ * @api private
201
202
  */
202
203
 
203
204
  StateMachine.prototype.map = function map() {
@@ -10,12 +10,14 @@ const utils = require('../utils');
10
10
 
11
11
  const documentArrayParent = require('../helpers/symbols').documentArrayParent;
12
12
 
13
- /*!
13
+ /**
14
14
  * A constructor.
15
15
  *
16
16
  * @param {Object} obj js object returned from the db
17
17
  * @param {MongooseDocumentArray} parentArr the parent array of this document
18
18
  * @param {Boolean} skipId
19
+ * @param {Object} fields
20
+ * @param {Number} index
19
21
  * @inherits Document
20
22
  * @api private
21
23
  */
@@ -125,9 +127,13 @@ ArraySubdocument.prototype.$__fullPath = function(path, skipIndex) {
125
127
  this.$__.fullPath + '.' + this.__index;
126
128
  };
127
129
 
128
- /*!
130
+ /**
129
131
  * Given a path relative to this document, return the path relative
130
132
  * to the top-level document.
133
+ * @method $__pathRelativeToParent
134
+ * @memberOf ArraySubdocument
135
+ * @instance
136
+ * @api private
131
137
  */
132
138
 
133
139
  ArraySubdocument.prototype.$__pathRelativeToParent = function(path, skipIndex) {
@@ -143,8 +149,12 @@ ArraySubdocument.prototype.$__pathRelativeToParent = function(path, skipIndex) {
143
149
  return this.__parentArray.$path() + '.' + this.__index + '.' + path;
144
150
  };
145
151
 
146
- /*!
152
+ /**
147
153
  * Returns this sub-documents parent document.
154
+ * @method $parent
155
+ * @memberOf ArraySubdocument
156
+ * @instance
157
+ * @api public
148
158
  */
149
159
 
150
160
  ArraySubdocument.prototype.$parent = function() {
@@ -212,8 +212,9 @@ const methods = {
212
212
  return ret;
213
213
  },
214
214
 
215
- /*!
215
+ /**
216
216
  * Wraps [`Array#shift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
217
+ * @api private
217
218
  */
218
219
 
219
220
  shift() {
@@ -224,8 +225,9 @@ const methods = {
224
225
  return ret;
225
226
  },
226
227
 
227
- /*!
228
+ /**
228
229
  * Wraps [`Array#splice`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) with proper change tracking and casting.
230
+ * @api private
229
231
  */
230
232
 
231
233
  splice() {
@@ -355,10 +357,12 @@ const methods = {
355
357
 
356
358
  module.exports = methods;
357
359
 
358
- /*!
360
+ /**
359
361
  * If this is a document array, each element may contain single
360
362
  * populated paths, so we need to modify the top-level document's
361
363
  * populated cache. See gh-8247, gh-8265.
364
+ * @param {Array} arr
365
+ * @api private
362
366
  */
363
367
 
364
368
  function _updateParentPopulated(arr) {
@@ -805,6 +805,7 @@ const methods = {
805
805
  let ret;
806
806
  const arr = utils.isMongooseArray(this) ? this.__array : this;
807
807
 
808
+ this._markModified();
808
809
  _checkManualPopulation(this, Array.prototype.slice.call(arguments, 2));
809
810
 
810
811
  if (arguments.length) {
package/lib/types/map.js CHANGED
@@ -295,9 +295,11 @@ Object.defineProperty(MongooseMap.prototype, '$__deferredCalls', {
295
295
  value: true
296
296
  });
297
297
 
298
- /*!
298
+ /**
299
299
  * Since maps are stored as objects under the hood, keys must be strings
300
300
  * and can't contain any invalid characters
301
+ * @param {String} key
302
+ * @api private
301
303
  */
302
304
 
303
305
  function checkValidKey(key) {
@@ -13,8 +13,9 @@
13
13
  const ObjectId = require('../driver').get().ObjectId;
14
14
  const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;
15
15
 
16
- /*!
16
+ /**
17
17
  * Getter for convenience with populate, see gh-6115
18
+ * @api private
18
19
  */
19
20
 
20
21
  Object.defineProperty(ObjectId.prototype, '_id', {
@@ -29,7 +30,6 @@ Object.defineProperty(ObjectId.prototype, '_id', {
29
30
  * Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
30
31
  */
31
32
 
32
-
33
33
  if (!ObjectId.prototype.hasOwnProperty('valueOf')) {
34
34
  ObjectId.prototype.valueOf = function objectIdValueOf() {
35
35
  return this.toString();
@@ -86,9 +86,15 @@ Subdocument.prototype.save = function(options, fn) {
86
86
  });
87
87
  };
88
88
 
89
- /*!
89
+ /**
90
90
  * Given a path relative to this document, return the path relative
91
91
  * to the top-level document.
92
+ * @param {String} path
93
+ * @method $__fullPath
94
+ * @memberOf Subdocument
95
+ * @instance
96
+ * @returns {String}
97
+ * @api private
92
98
  */
93
99
 
94
100
  Subdocument.prototype.$__fullPath = function(path) {
@@ -101,9 +107,15 @@ Subdocument.prototype.$__fullPath = function(path) {
101
107
  this.$__.fullPath;
102
108
  };
103
109
 
104
- /*!
110
+ /**
105
111
  * Given a path relative to this document, return the path relative
106
112
  * to the top-level document.
113
+ * @param {String} p
114
+ * @returns {String}
115
+ * @method $__pathRelativeToParent
116
+ * @memberOf Subdocument
117
+ * @instance
118
+ * @api private
107
119
  */
108
120
 
109
121
  Subdocument.prototype.$__pathRelativeToParent = function(p) {
@@ -317,8 +329,13 @@ Subdocument.prototype.parent = function() {
317
329
 
318
330
  Subdocument.prototype.$parent = Subdocument.prototype.parent;
319
331
 
320
- /*!
332
+ /**
321
333
  * no-op for hooks
334
+ * @param {Function} cb
335
+ * @method $__remove
336
+ * @memberOf Subdocument
337
+ * @instance
338
+ * @api private
322
339
  */
323
340
 
324
341
  Subdocument.prototype.$__remove = function(cb) {
@@ -328,6 +345,14 @@ Subdocument.prototype.$__remove = function(cb) {
328
345
  return cb(null, this);
329
346
  };
330
347
 
348
+ /**
349
+ * ignore
350
+ * @method $__removeFromParent
351
+ * @memberOf Subdocument
352
+ * @instance
353
+ * @api private
354
+ */
355
+
331
356
  Subdocument.prototype.$__removeFromParent = function() {
332
357
  this.$__parent.set(this.$basePath, null);
333
358
  };
@@ -379,14 +404,11 @@ Subdocument.prototype.inspect = function() {
379
404
  };
380
405
 
381
406
  if (util.inspect.custom) {
382
- /*!
383
- * Avoid Node deprecation warning DEP0079
384
- */
385
-
407
+ // Avoid Node deprecation warning DEP0079
386
408
  Subdocument.prototype[util.inspect.custom] = Subdocument.prototype.inspect;
387
409
  }
388
410
 
389
- /*!
411
+ /**
390
412
  * Registers remove event listeners for triggering
391
413
  * on subdocuments.
392
414
  *