mongoose 6.5.2 → 6.5.5

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 (95) hide show
  1. package/.mocharc.yml +2 -0
  2. package/dist/browser.umd.js +1148 -68091
  3. package/lib/aggregate.js +7 -7
  4. package/lib/browserDocument.js +1 -1
  5. package/lib/cast/boolean.js +1 -1
  6. package/lib/cast/number.js +1 -1
  7. package/lib/cast/string.js +1 -1
  8. package/lib/cast.js +3 -0
  9. package/lib/connection.js +19 -11
  10. package/lib/cursor/AggregationCursor.js +16 -5
  11. package/lib/cursor/QueryCursor.js +16 -5
  12. package/lib/document.js +31 -18
  13. package/lib/drivers/browser/objectid.js +2 -1
  14. package/lib/drivers/node-mongodb-native/ReadPreference.js +1 -1
  15. package/lib/drivers/node-mongodb-native/collection.js +6 -2
  16. package/lib/error/browserMissingSchema.js +1 -1
  17. package/lib/error/cast.js +2 -1
  18. package/lib/error/divergentArray.js +2 -1
  19. package/lib/error/missingSchema.js +2 -1
  20. package/lib/error/notFound.js +2 -1
  21. package/lib/error/overwriteModel.js +2 -1
  22. package/lib/error/strictPopulate.js +29 -0
  23. package/lib/error/validation.js +9 -7
  24. package/lib/error/validator.js +8 -4
  25. package/lib/helpers/clone.js +1 -1
  26. package/lib/helpers/discriminator/getConstructor.js +2 -1
  27. package/lib/helpers/discriminator/getDiscriminatorByValue.js +7 -6
  28. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +7 -6
  29. package/lib/helpers/document/compile.js +14 -2
  30. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +5 -1
  31. package/lib/helpers/get.js +2 -1
  32. package/lib/helpers/getConstructorName.js +2 -1
  33. package/lib/helpers/indexes/isIndexEqual.js +6 -6
  34. package/lib/helpers/isBsonType.js +2 -1
  35. package/lib/helpers/isMongooseObject.js +2 -2
  36. package/lib/helpers/isObject.js +1 -1
  37. package/lib/helpers/isSimpleValidator.js +1 -1
  38. package/lib/helpers/model/applyHooks.js +11 -0
  39. package/lib/helpers/model/applyMethods.js +11 -1
  40. package/lib/helpers/model/applyStatics.js +2 -1
  41. package/lib/helpers/model/castBulkWrite.js +7 -2
  42. package/lib/helpers/model/discriminator.js +3 -1
  43. package/lib/helpers/path/flattenObjectWithDottedPaths.js +3 -2
  44. package/lib/helpers/pluralize.js +1 -1
  45. package/lib/helpers/populate/assignRawDocsToIdStructure.js +5 -3
  46. package/lib/helpers/populate/assignVals.js +14 -3
  47. package/lib/helpers/populate/createPopulateQueryFilter.js +8 -2
  48. package/lib/helpers/populate/getModelsMapForPopulate.js +5 -4
  49. package/lib/helpers/populate/getSchemaTypes.js +3 -1
  50. package/lib/helpers/populate/markArraySubdocsPopulated.js +5 -1
  51. package/lib/helpers/projection/hasIncludedChildren.js +4 -1
  52. package/lib/helpers/projection/isPathExcluded.js +3 -2
  53. package/lib/helpers/projection/isSubpath.js +2 -1
  54. package/lib/helpers/query/applyQueryMiddleware.js +3 -2
  55. package/lib/helpers/query/castUpdate.js +14 -8
  56. package/lib/helpers/query/completeMany.js +3 -2
  57. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +7 -1
  58. package/lib/helpers/query/wrapThunk.js +3 -1
  59. package/lib/helpers/schema/getIndexes.js +7 -2
  60. package/lib/helpers/schema/getPath.js +4 -3
  61. package/lib/helpers/schema/idGetter.js +2 -1
  62. package/lib/helpers/setDefaultsOnInsert.js +15 -0
  63. package/lib/index.js +20 -12
  64. package/lib/model.js +219 -145
  65. package/lib/query.js +160 -65
  66. package/lib/queryhelpers.js +4 -4
  67. package/lib/schema/SubdocumentPath.js +1 -1
  68. package/lib/schema/array.js +2 -2
  69. package/lib/schema/date.js +2 -1
  70. package/lib/schema/documentarray.js +4 -3
  71. package/lib/schema/operators/helpers.js +2 -2
  72. package/lib/schema/operators/text.js +1 -1
  73. package/lib/schema/string.js +1 -1
  74. package/lib/schema.js +14 -5
  75. package/lib/schematype.js +17 -4
  76. package/lib/statemachine.js +13 -12
  77. package/lib/types/ArraySubdocument.js +13 -3
  78. package/lib/types/DocumentArray/methods/index.js +9 -5
  79. package/lib/types/array/methods/index.js +5 -4
  80. package/lib/types/map.js +3 -1
  81. package/lib/types/objectid.js +2 -2
  82. package/lib/types/subdocument.js +30 -8
  83. package/lib/utils.js +58 -31
  84. package/package.json +11 -11
  85. package/scripts/create-tarball.js +7 -0
  86. package/types/aggregate.d.ts +3 -3
  87. package/types/connection.d.ts +4 -4
  88. package/types/cursor.d.ts +1 -1
  89. package/types/document.d.ts +2 -2
  90. package/types/index.d.ts +13 -27
  91. package/types/indexes.d.ts +2 -2
  92. package/types/middlewares.d.ts +1 -1
  93. package/types/models.d.ts +14 -11
  94. package/types/pipelinestage.d.ts +1 -1
  95. package/types/query.d.ts +33 -16
package/lib/aggregate.js CHANGED
@@ -43,7 +43,7 @@ const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
43
43
  * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
44
44
  *
45
45
  * @see MongoDB https://docs.mongodb.org/manual/applications/aggregation/
46
- * @see driver https://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
46
+ * @see driver https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#aggregate
47
47
  * @param {Array} [pipeline] aggregation pipeline as an array of objects
48
48
  * @param {Model} [model] the model to use with this aggregate.
49
49
  * @api public
@@ -623,7 +623,6 @@ Aggregate.prototype.unionWith = function(options) {
623
623
  * @return {Aggregate} this
624
624
  * @api public
625
625
  * @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
626
- * @see driver https://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
627
626
  */
628
627
 
629
628
  Aggregate.prototype.read = function(pref, tags) {
@@ -766,7 +765,7 @@ Aggregate.prototype.explain = function(verbosity, callback) {
766
765
  };
767
766
 
768
767
  /**
769
- * Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0)
768
+ * Sets the allowDiskUse option for the aggregation query
770
769
  *
771
770
  * #### Example:
772
771
  *
@@ -783,7 +782,7 @@ Aggregate.prototype.allowDiskUse = function(value) {
783
782
  };
784
783
 
785
784
  /**
786
- * Sets the hint option for the aggregation query (ignored for < 3.6.0)
785
+ * Sets the hint option for the aggregation query
787
786
  *
788
787
  * #### Example:
789
788
  *
@@ -863,7 +862,7 @@ Aggregate.prototype.option = function(value) {
863
862
  * @param {Boolean} [options.useMongooseAggCursor] use experimental mongoose-specific aggregation cursor (for `eachAsync()` and other query cursor semantics)
864
863
  * @return {AggregationCursor} cursor representing this aggregation
865
864
  * @api public
866
- * @see mongodb https://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
865
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html
867
866
  */
868
867
 
869
868
  Aggregate.prototype.cursor = function(options) {
@@ -881,7 +880,7 @@ Aggregate.prototype.cursor = function(options) {
881
880
  * @param {Object} collation options
882
881
  * @return {Aggregate} this
883
882
  * @api public
884
- * @see mongodb https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#aggregate
883
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/interfaces/CollationOptions.html
885
884
  */
886
885
 
887
886
  Aggregate.prototype.collation = function(collation) {
@@ -1104,12 +1103,13 @@ function isOperator(obj) {
1104
1103
  return k.length === 1 && k[0][0] === '$';
1105
1104
  }
1106
1105
 
1107
- /*!
1106
+ /**
1108
1107
  * Adds the appropriate `$match` pipeline step to the top of an aggregate's
1109
1108
  * pipeline, should it's model is a non-root discriminator type. This is
1110
1109
  * analogous to the `prepareDiscriminatorCriteria` function in `lib/query.js`.
1111
1110
  *
1112
1111
  * @param {Aggregate} aggregate Aggregate to prepare
1112
+ * @api private
1113
1113
  */
1114
1114
 
1115
1115
  Aggregate._prepareDiscriminatorPipeline = prepareDiscriminatorPipeline;
@@ -20,7 +20,7 @@ const isObject = require('./helpers/isObject');
20
20
  * @param {Object} schema
21
21
  * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
22
22
  * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
23
- * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
23
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#class-eventemitter
24
24
  * @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
25
25
  * @event `save`: Emitted when the document is successfully saved
26
26
  * @api private
@@ -2,7 +2,7 @@
2
2
 
3
3
  const CastError = require('../error/cast');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a boolean, or throw a `CastError` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
@@ -2,7 +2,7 @@
2
2
 
3
3
  const assert = require('assert');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a number, or throw an `Error` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
@@ -2,7 +2,7 @@
2
2
 
3
3
  const CastError = require('../error/cast');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a string, or throw a `CastError` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
package/lib/cast.js CHANGED
@@ -25,6 +25,9 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
25
25
  * @param {Schema} schema
26
26
  * @param {Object} obj Object to cast
27
27
  * @param {Object} [options] the query options
28
+ * @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
29
+ * @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
30
+ * @param {Boolean} [options.upsert]
28
31
  * @param {Query} [context] passed to setters
29
32
  * @api private
30
33
  */
package/lib/connection.js CHANGED
@@ -26,7 +26,7 @@ const processConnectionOptions = require('./helpers/processConnectionOptions');
26
26
  const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
27
27
  const sessionNewDocuments = require('./helpers/symbols').sessionNewDocuments;
28
28
 
29
- /*!
29
+ /**
30
30
  * A list of authentication mechanisms that don't require a password for authentication.
31
31
  * This is used by the authMechanismDoesNotRequirePassword method.
32
32
  *
@@ -42,7 +42,7 @@ const noPasswordAuthMechanisms = [
42
42
  * For practical reasons, a Connection equals a Db.
43
43
  *
44
44
  * @param {Mongoose} base a mongoose instance
45
- * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
45
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#class-eventemitter
46
46
  * @event `connecting`: Emitted when `connection.openUri()` is executed on this connection.
47
47
  * @event `connected`: Emitted when this connection successfully connects to the db. May be emitted _multiple_ times in `reconnected` scenarios.
48
48
  * @event `open`: Emitted after we `connected` and `onOpen` is executed on all of this connection's models.
@@ -387,11 +387,11 @@ Connection.prototype.config;
387
387
  * with specified options. Used to create [capped collections](https://docs.mongodb.com/manual/core/capped-collections/)
388
388
  * and [views](https://docs.mongodb.com/manual/core/views/) from mongoose.
389
389
  *
390
- * Options are passed down without modification to the [MongoDB driver's `createCollection()` function](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
390
+ * Options are passed down without modification to the [MongoDB driver's `createCollection()` function](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createCollection)
391
391
  *
392
392
  * @method createCollection
393
393
  * @param {string} collection The collection to create
394
- * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
394
+ * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createCollection)
395
395
  * @param {Function} [callback]
396
396
  * @return {Promise} Returns a Promise if no `callback` is given.
397
397
  * @api public
@@ -423,7 +423,7 @@ Connection.prototype.createCollection = _wrapConnHelper(function createCollectio
423
423
  *
424
424
  *
425
425
  * @method startSession
426
- * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
426
+ * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#startSession)
427
427
  * @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
428
428
  * @param {Function} [callback]
429
429
  * @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
@@ -445,7 +445,7 @@ Connection.prototype.startSession = _wrapConnHelper(function startSession(option
445
445
  * async function executes successfully and attempt to retry if
446
446
  * there was a retriable error.
447
447
  *
448
- * Calls the MongoDB driver's [`session.withTransaction()`](https://mongodb.github.io/node-mongodb-native/3.5/api/ClientSession.html#withTransaction),
448
+ * Calls the MongoDB driver's [`session.withTransaction()`](https://mongodb.github.io/node-mongodb-native/4.9/classes/ClientSession.html#withTransaction),
449
449
  * but also handles resetting Mongoose document state as shown below.
450
450
  *
451
451
  * #### Example:
@@ -656,7 +656,7 @@ Connection.prototype.onOpen = function() {
656
656
  * Opens the connection with a URI using `MongoClient.connect()`.
657
657
  *
658
658
  * @param {String} uri The URI to connect with.
659
- * @param {Object} [options] Passed on to https://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
659
+ * @param {Object} [options] Passed on to [`MongoClient.connect`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#connect-1)
660
660
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
661
661
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
662
662
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
@@ -667,7 +667,7 @@ Connection.prototype.onOpen = function() {
667
667
  * @param {Number} [options.serverSelectionTimeoutMS] If `useUnifiedTopology = true`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds before erroring out. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
668
668
  * @param {Number} [options.heartbeatFrequencyMS] If `useUnifiedTopology = true`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
669
669
  * @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
670
- * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
670
+ * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
671
671
  * @param {Number} [options.connectTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
672
672
  * @param {Number} [options.socketTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. A socket may be inactive because of either no activity or a long-running operation. This is set to `30000` by default, you should set this to 2-3x your longest running operation if you expect some of your database operations to run longer than 20 seconds. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
673
673
  * @param {Number} [options.family=0] Passed transparently to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. May be either `0, `4`, or `6`. `4` means use IPv4 only, `6` means use IPv6 only, `0` means try both.
@@ -1230,6 +1230,14 @@ Connection.prototype.model = function(name, schema, collection, options) {
1230
1230
  return sub;
1231
1231
  }
1232
1232
 
1233
+ if (arguments.length === 1) {
1234
+ model = this.models[name];
1235
+ if (!model) {
1236
+ throw new MongooseError.MissingSchemaError(name);
1237
+ }
1238
+ return model;
1239
+ }
1240
+
1233
1241
  if (!model) {
1234
1242
  throw new MongooseError.MissingSchemaError(name);
1235
1243
  }
@@ -1324,7 +1332,7 @@ Connection.prototype.deleteModel = function(name) {
1324
1332
  *
1325
1333
  * @api public
1326
1334
  * @param {Array} [pipeline]
1327
- * @param {Object} [options] passed without changes to [the MongoDB driver's `Db#watch()` function](https://mongodb.github.io/node-mongodb-native/3.4/api/Db.html#watch)
1335
+ * @param {Object} [options] passed without changes to [the MongoDB driver's `Db#watch()` function](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#watch)
1328
1336
  * @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
1329
1337
  */
1330
1338
 
@@ -1426,7 +1434,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) {
1426
1434
  };
1427
1435
 
1428
1436
  /**
1429
- * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1437
+ * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
1430
1438
  * that this connection uses to talk to MongoDB.
1431
1439
  *
1432
1440
  * #### Example:
@@ -1445,7 +1453,7 @@ Connection.prototype.getClient = function getClient() {
1445
1453
  };
1446
1454
 
1447
1455
  /**
1448
- * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1456
+ * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
1449
1457
  * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
1450
1458
  * reuse it.
1451
1459
  *
@@ -71,8 +71,12 @@ function _init(model, c, agg) {
71
71
  }
72
72
  }
73
73
 
74
- /*!
74
+ /**
75
75
  * Necessary to satisfy the Readable API
76
+ * @method _read
77
+ * @memberOf AggregationCursor
78
+ * @instance
79
+ * @api private
76
80
  */
77
81
 
78
82
  AggregationCursor.prototype._read = function() {
@@ -147,8 +151,12 @@ Object.defineProperty(AggregationCursor.prototype, 'map', {
147
151
  writable: true
148
152
  });
149
153
 
150
- /*!
154
+ /**
151
155
  * Marks this cursor as errored
156
+ * @method _markError
157
+ * @instance
158
+ * @memberOf AggregationCursor
159
+ * @api private
152
160
  */
153
161
 
154
162
  AggregationCursor.prototype._markError = function(error) {
@@ -165,7 +173,7 @@ AggregationCursor.prototype._markError = function(error) {
165
173
  * @api public
166
174
  * @method close
167
175
  * @emits close
168
- * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
176
+ * @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
169
177
  */
170
178
 
171
179
  AggregationCursor.prototype.close = function(callback) {
@@ -293,7 +301,7 @@ function _transformForAsyncIterator(doc) {
293
301
  }
294
302
 
295
303
  /**
296
- * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
304
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#addCursorFlag).
297
305
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
298
306
  *
299
307
  * @param {String} flag
@@ -324,9 +332,12 @@ function _waitForCursor(ctx, cb) {
324
332
  });
325
333
  }
326
334
 
327
- /*!
335
+ /**
328
336
  * Get the next doc from the underlying cursor and mongooseify it
329
337
  * (populate, etc.)
338
+ * @param {Any} ctx
339
+ * @param {Function} cb
340
+ * @api private
330
341
  */
331
342
 
332
343
  function _next(ctx, cb) {
@@ -85,8 +85,12 @@ function QueryCursor(query, options) {
85
85
 
86
86
  util.inherits(QueryCursor, Readable);
87
87
 
88
- /*!
88
+ /**
89
89
  * Necessary to satisfy the Readable API
90
+ * @method _read
91
+ * @memberOf QueryCursor
92
+ * @instance
93
+ * @api private
90
94
  */
91
95
 
92
96
  QueryCursor.prototype._read = function() {
@@ -152,8 +156,12 @@ Object.defineProperty(QueryCursor.prototype, 'map', {
152
156
  writable: true
153
157
  });
154
158
 
155
- /*!
159
+ /**
156
160
  * Marks this cursor as errored
161
+ * @method _markError
162
+ * @memberOf QueryCursor
163
+ * @instance
164
+ * @api private
157
165
  */
158
166
 
159
167
  QueryCursor.prototype._markError = function(error) {
@@ -170,7 +178,7 @@ QueryCursor.prototype._markError = function(error) {
170
178
  * @api public
171
179
  * @method close
172
180
  * @emits close
173
- * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
181
+ * @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
174
182
  */
175
183
 
176
184
  QueryCursor.prototype.close = function(callback) {
@@ -255,7 +263,7 @@ QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
255
263
  QueryCursor.prototype.options;
256
264
 
257
265
  /**
258
- * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
266
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html#addCursorFlag).
259
267
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
260
268
  *
261
269
  * @param {String} flag
@@ -340,9 +348,12 @@ function _transformForAsyncIterator(doc) {
340
348
  return doc == null ? { done: true } : { value: doc, done: false };
341
349
  }
342
350
 
343
- /*!
351
+ /**
344
352
  * Get the next doc from the underlying cursor and mongooseify it
345
353
  * (populate, etc.)
354
+ * @param {Any} ctx
355
+ * @param {Function} cb
356
+ * @api private
346
357
  */
347
358
 
348
359
  function _next(ctx, cb) {
package/lib/document.js CHANGED
@@ -70,7 +70,7 @@ const specialProperties = utils.specialProperties;
70
70
  * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
71
71
  * @param {Object} [options] various configuration options for the document
72
72
  * @param {Boolean} [options.defaults=true] if `false`, skip applying default values to this document.
73
- * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
73
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#class-eventemitter
74
74
  * @event `init`: Emitted on a document after it has been retrieved from the db and fully hydrated by Mongoose.
75
75
  * @event `save`: Emitted when the document is successfully saved
76
76
  * @api private
@@ -696,12 +696,15 @@ Document.prototype.$__init = function(doc, opts) {
696
696
  return this;
697
697
  };
698
698
 
699
- /*!
699
+ /**
700
700
  * Init helper.
701
701
  *
702
702
  * @param {Object} self document instance
703
703
  * @param {Object} obj raw mongodb doc
704
704
  * @param {Object} doc object we are initializing
705
+ * @param {Object} [opts] Optional Options
706
+ * @param {Boolean} [opts.setters] Call `applySetters` instead of `cast`
707
+ * @param {String} [prefix] Prefix to add to each path
705
708
  * @api private
706
709
  */
707
710
 
@@ -3678,7 +3681,7 @@ Document.prototype.$toObject = function(options, json) {
3678
3681
  /**
3679
3682
  * Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)).
3680
3683
  *
3681
- * Buffers are converted to instances of [mongodb.Binary](https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html) for proper storage.
3684
+ * Buffers are converted to instances of [mongodb.Binary](https://mongodb.github.io/node-mongodb-native/4.9/classes/Binary.html) for proper storage.
3682
3685
  *
3683
3686
  * #### Getters/Virtuals
3684
3687
  *
@@ -3805,7 +3808,7 @@ Document.prototype.$toObject = function(options, json) {
3805
3808
  * @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
3806
3809
  * @param {Boolean} [options.useProjection=false] - If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
3807
3810
  * @return {Object} js object (not a POJO)
3808
- * @see mongodb.Binary https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html
3811
+ * @see mongodb.Binary https://mongodb.github.io/node-mongodb-native/4.9/classes/Binary.html
3809
3812
  * @api public
3810
3813
  * @memberOf Document
3811
3814
  * @instance
@@ -3815,11 +3818,12 @@ Document.prototype.toObject = function(options) {
3815
3818
  return this.$toObject(options);
3816
3819
  };
3817
3820
 
3818
- /*!
3821
+ /**
3819
3822
  * Minimizes an object, removing undefined values and empty objects
3820
3823
  *
3821
3824
  * @param {Object} object to minimize
3822
3825
  * @return {Object}
3826
+ * @api private
3823
3827
  */
3824
3828
 
3825
3829
  function minimize(obj) {
@@ -3925,12 +3929,14 @@ function applyVirtuals(self, json, options, toObjectOptions) {
3925
3929
  }
3926
3930
 
3927
3931
 
3928
- /*!
3932
+ /**
3929
3933
  * Applies virtuals properties to `json`.
3930
3934
  *
3931
3935
  * @param {Document} self
3932
3936
  * @param {Object} json
3937
+ * @param {Object} [options]
3933
3938
  * @return {Object} `json`
3939
+ * @api private
3934
3940
  */
3935
3941
 
3936
3942
  function applyGetters(self, json, options) {
@@ -3980,12 +3986,13 @@ function applyGetters(self, json, options) {
3980
3986
  return json;
3981
3987
  }
3982
3988
 
3983
- /*!
3989
+ /**
3984
3990
  * Applies schema type transforms to `json`.
3985
3991
  *
3986
3992
  * @param {Document} self
3987
3993
  * @param {Object} json
3988
3994
  * @return {Object} `json`
3995
+ * @api private
3989
3996
  */
3990
3997
 
3991
3998
  function applySchemaTypeTransforms(self, json) {
@@ -4158,10 +4165,7 @@ Document.prototype.inspect = function(options) {
4158
4165
  };
4159
4166
 
4160
4167
  if (inspect.custom) {
4161
- /*!
4162
- * Avoid Node deprecation warning DEP0079
4163
- */
4164
-
4168
+ // Avoid Node deprecation warning DEP0079
4165
4169
  Document.prototype[inspect.custom] = Document.prototype.inspect;
4166
4170
  }
4167
4171
 
@@ -4417,8 +4421,13 @@ Document.prototype.$populated = Document.prototype.populated;
4417
4421
  * doc.$assertPopulated('author'); // does not throw
4418
4422
  * doc.$assertPopulated('other path'); // throws an error
4419
4423
  *
4424
+ * // Manually populate and assert in one call. The following does
4425
+ * // `doc.$set({ likes })` before asserting.
4426
+ * doc.$assertPopulated('likes', { likes });
4420
4427
  *
4421
- * @param {String|String[]} paths
4428
+ *
4429
+ * @param {String|String[]} path path or array of paths to check. `$assertPopulated` throws if any of the given paths is not populated.
4430
+ * @param {Object} [values] optional values to `$set()`. Convenient if you want to manually populate a path and assert that the path was populated in 1 call.
4422
4431
  * @return {Document} this
4423
4432
  * @memberOf Document
4424
4433
  * @method $assertPopulated
@@ -4426,14 +4435,18 @@ Document.prototype.$populated = Document.prototype.populated;
4426
4435
  * @api public
4427
4436
  */
4428
4437
 
4429
- Document.prototype.$assertPopulated = function $assertPopulated(paths) {
4430
- if (Array.isArray(paths)) {
4431
- paths.forEach(path => this.$assertPopulated(path));
4438
+ Document.prototype.$assertPopulated = function $assertPopulated(path, values) {
4439
+ if (Array.isArray(path)) {
4440
+ path.forEach(p => this.$assertPopulated(p, values));
4432
4441
  return this;
4433
4442
  }
4434
4443
 
4435
- if (!this.$populated(paths)) {
4436
- throw new MongooseError(`Expected path "${paths}" to be populated`);
4444
+ if (arguments.length > 1) {
4445
+ this.$set(values);
4446
+ }
4447
+
4448
+ if (!this.$populated(path)) {
4449
+ throw new MongooseError(`Expected path "${path}" to be populated`);
4437
4450
  }
4438
4451
 
4439
4452
  return this;
@@ -4452,7 +4465,7 @@ Document.prototype.$assertPopulated = function $assertPopulated(paths) {
4452
4465
  *
4453
4466
  * If the path was not provided, then all populated fields are returned to their unpopulated state.
4454
4467
  *
4455
- * @param {String} [path] Specific Path to depopulate. If unset, will depopulate all paths on the Document.
4468
+ * @param {String|String[]} [path] Specific Path to depopulate. If unset, will depopulate all paths on the Document. Or multiple space-delimited paths.
4456
4469
  * @return {Document} this
4457
4470
  * @see Document.populate #document_Document-populate
4458
4471
  * @api public
@@ -9,8 +9,9 @@
9
9
 
10
10
  const ObjectId = require('bson').ObjectID;
11
11
 
12
- /*!
12
+ /**
13
13
  * Getter for convenience with populate, see gh-6115
14
+ * @api private
14
15
  */
15
16
 
16
17
  Object.defineProperty(ObjectId.prototype, '_id', {
@@ -7,7 +7,7 @@
7
7
  const mongodb = require('mongodb');
8
8
  const ReadPref = mongodb.ReadPreference;
9
9
 
10
- /*!
10
+ /**
11
11
  * Converts arguments to ReadPrefs the driver
12
12
  * can understand.
13
13
  *
@@ -66,8 +66,10 @@ NativeCollection.prototype.onClose = function(force) {
66
66
 
67
67
  const syncCollectionMethods = { watch: true, find: true, aggregate: true };
68
68
 
69
- /*!
69
+ /**
70
70
  * Copy the collection methods and make them subject to queues
71
+ * @param {Number|String} I
72
+ * @api private
71
73
  */
72
74
 
73
75
  function iter(i) {
@@ -296,8 +298,10 @@ NativeCollection.prototype.$format = function(arg, color, shell) {
296
298
  return format(arg, false, color, shell);
297
299
  };
298
300
 
299
- /*!
301
+ /**
300
302
  * Debug print helper
303
+ * @param {Any} representation
304
+ * @api private
301
305
  */
302
306
 
303
307
  function inspectable(representation) {
@@ -8,7 +8,7 @@ const MongooseError = require('./');
8
8
 
9
9
 
10
10
  class MissingSchemaError extends MongooseError {
11
- /*!
11
+ /**
12
12
  * MissingSchema Error constructor.
13
13
  */
14
14
  constructor() {
package/lib/error/cast.js CHANGED
@@ -56,9 +56,10 @@ class CastError extends MongooseError {
56
56
  this.valueType = getValueType(value);
57
57
  }
58
58
 
59
- /*!
59
+ /**
60
60
  * ignore
61
61
  * @param {Readonly<CastError>} other
62
+ * @api private
62
63
  */
63
64
  copy(other) {
64
65
  this.messageFormat = other.messageFormat;
@@ -8,9 +8,10 @@
8
8
  const MongooseError = require('./');
9
9
 
10
10
  class DivergentArrayError extends MongooseError {
11
- /*!
11
+ /**
12
12
  * DivergentArrayError constructor.
13
13
  * @param {Array<String>} paths
14
+ * @api private
14
15
  */
15
16
  constructor(paths) {
16
17
  const msg = 'For your own good, using `document.save()` to update an array '
@@ -8,9 +8,10 @@
8
8
  const MongooseError = require('./');
9
9
 
10
10
  class MissingSchemaError extends MongooseError {
11
- /*!
11
+ /**
12
12
  * MissingSchema Error constructor.
13
13
  * @param {String} name
14
+ * @api private
14
15
  */
15
16
  constructor(name) {
16
17
  const msg = 'Schema hasn\'t been registered for model "' + name + '".\n'
@@ -8,8 +8,9 @@ const MongooseError = require('./');
8
8
  const util = require('util');
9
9
 
10
10
  class DocumentNotFoundError extends MongooseError {
11
- /*!
11
+ /**
12
12
  * OverwriteModel Error constructor.
13
+ * @api private
13
14
  */
14
15
  constructor(filter, model, numAffected, result) {
15
16
  let msg;
@@ -9,9 +9,10 @@ const MongooseError = require('./');
9
9
 
10
10
 
11
11
  class OverwriteModelError extends MongooseError {
12
- /*!
12
+ /**
13
13
  * OverwriteModel Error constructor.
14
14
  * @param {String} name
15
+ * @api private
15
16
  */
16
17
  constructor(name) {
17
18
  super('Cannot overwrite `' + name + '` model once compiled.');
@@ -0,0 +1,29 @@
1
+ /*!
2
+ * Module dependencies.
3
+ */
4
+
5
+ 'use strict';
6
+
7
+ const MongooseError = require('./');
8
+
9
+ class StrictPopulateError extends MongooseError {
10
+ /**
11
+ * Strict mode error constructor
12
+ *
13
+ * @param {String} path
14
+ * @param {String} [msg]
15
+ * @inherits MongooseError
16
+ * @api private
17
+ */
18
+ constructor(path, msg) {
19
+ msg = msg || 'Cannot populate path `' + path + '` because it is not in your schema. ' + 'Set the `strictPopulate` option to false to override.';
20
+ super(msg);
21
+ this.path = path;
22
+ }
23
+ }
24
+
25
+ Object.defineProperty(StrictPopulateError.prototype, 'name', {
26
+ value: 'StrictPopulateError'
27
+ });
28
+
29
+ module.exports = StrictPopulateError;