mongoose 6.3.9 → 6.4.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.
Files changed (129) hide show
  1. package/.eslintrc.json +65 -36
  2. package/dist/browser.umd.js +68579 -2
  3. package/lib/cast/date.js +1 -1
  4. package/lib/cast/decimal128.js +1 -1
  5. package/lib/cast/objectid.js +1 -1
  6. package/lib/cast.js +1 -1
  7. package/lib/connection.js +47 -5
  8. package/lib/document.js +48 -8
  9. package/lib/drivers/node-mongodb-native/index.js +1 -1
  10. package/lib/error/parallelValidate.js +1 -1
  11. package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +1 -1
  12. package/lib/helpers/aggregate/stringifyFunctionOperators.js +1 -1
  13. package/lib/helpers/arrayDepth.js +1 -1
  14. package/lib/helpers/cursor/eachAsync.js +1 -1
  15. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
  16. package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +1 -1
  17. package/lib/helpers/discriminator/getConstructor.js +1 -1
  18. package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
  19. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
  20. package/lib/helpers/document/handleSpreadDoc.js +1 -1
  21. package/lib/helpers/each.js +1 -1
  22. package/lib/helpers/get.js +1 -1
  23. package/lib/helpers/getConstructorName.js +1 -1
  24. package/lib/helpers/getDefaultBulkwriteResult.js +1 -1
  25. package/lib/helpers/indexes/applySchemaCollation.js +1 -1
  26. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
  27. package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
  28. package/lib/helpers/indexes/isTextIndex.js +1 -1
  29. package/lib/helpers/isMongooseObject.js +1 -1
  30. package/lib/helpers/isObject.js +1 -1
  31. package/lib/helpers/isPromise.js +1 -1
  32. package/lib/helpers/model/applyHooks.js +1 -1
  33. package/lib/helpers/model/applyStaticHooks.js +1 -1
  34. package/lib/helpers/once.js +1 -1
  35. package/lib/helpers/path/flattenObjectWithDottedPaths.js +1 -1
  36. package/lib/helpers/path/parentPaths.js +1 -1
  37. package/lib/helpers/path/setDottedPath.js +1 -1
  38. package/lib/helpers/pluralize.js +1 -1
  39. package/lib/helpers/populate/SkipPopulateValue.js +1 -1
  40. package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -1
  41. package/lib/helpers/populate/assignVals.js +1 -1
  42. package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
  43. package/lib/helpers/populate/getModelsMapForPopulate.js +1 -1
  44. package/lib/helpers/populate/leanPopulateMap.js +1 -1
  45. package/lib/helpers/populate/lookupLocalFields.js +1 -1
  46. package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
  47. package/lib/helpers/populate/modelNamesFromRefPath.js +1 -1
  48. package/lib/helpers/populate/removeDeselectedForeignField.js +1 -1
  49. package/lib/helpers/populate/validateRef.js +1 -1
  50. package/lib/helpers/printJestWarning.js +1 -1
  51. package/lib/helpers/processConnectionOptions.js +1 -1
  52. package/lib/helpers/projection/applyProjection.js +1 -1
  53. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  54. package/lib/helpers/projection/parseProjection.js +1 -1
  55. package/lib/helpers/query/applyGlobalOption.js +1 -1
  56. package/lib/helpers/query/applyQueryMiddleware.js +1 -1
  57. package/lib/helpers/query/cast$expr.js +1 -1
  58. package/lib/helpers/query/castFilterPath.js +1 -1
  59. package/lib/helpers/query/sanitizeFilter.js +1 -1
  60. package/lib/helpers/query/sanitizeProjection.js +1 -1
  61. package/lib/helpers/query/trusted.js +1 -1
  62. package/lib/helpers/query/validOps.js +1 -1
  63. package/lib/helpers/query/wrapThunk.js +1 -1
  64. package/lib/helpers/schema/addAutoId.js +1 -1
  65. package/lib/helpers/schema/applyPlugins.js +1 -1
  66. package/lib/helpers/schema/cleanPositionalOperators.js +1 -1
  67. package/lib/helpers/schema/getKeysInSchemaOrder.js +1 -1
  68. package/lib/helpers/schema/getPath.js +1 -1
  69. package/lib/helpers/schema/handleIdOption.js +1 -1
  70. package/lib/helpers/schema/handleTimestampOption.js +1 -1
  71. package/lib/helpers/specialProperties.js +1 -1
  72. package/lib/helpers/symbols.js +1 -1
  73. package/lib/helpers/timers.js +1 -1
  74. package/lib/helpers/timestamps/setupTimestamps.js +5 -2
  75. package/lib/helpers/topology/allServersUnknown.js +1 -1
  76. package/lib/helpers/topology/isAtlas.js +1 -1
  77. package/lib/helpers/topology/isSSLError.js +1 -1
  78. package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
  79. package/lib/helpers/update/castArrayFilters.js +1 -1
  80. package/lib/helpers/update/moveImmutableProperties.js +1 -1
  81. package/lib/helpers/update/removeUnusedArrayFilters.js +1 -1
  82. package/lib/helpers/update/updatedPathsByArrayFilter.js +1 -1
  83. package/lib/helpers/updateValidators.js +11 -18
  84. package/lib/index.js +108 -29
  85. package/lib/model.js +1 -1
  86. package/lib/options/PopulateOptions.js +1 -1
  87. package/lib/options/SchemaArrayOptions.js +1 -1
  88. package/lib/options/SchemaBufferOptions.js +1 -1
  89. package/lib/options/SchemaDocumentArrayOptions.js +1 -1
  90. package/lib/options/SchemaMapOptions.js +1 -1
  91. package/lib/options/SchemaNumberOptions.js +1 -1
  92. package/lib/options/SchemaObjectIdOptions.js +1 -1
  93. package/lib/options/SchemaSubdocumentOptions.js +1 -1
  94. package/lib/options/SchemaTypeOptions.js +1 -1
  95. package/lib/options/VirtualOptions.js +1 -1
  96. package/lib/options/propertyOptions.js +1 -1
  97. package/lib/options/removeOptions.js +1 -1
  98. package/lib/options/saveOptions.js +1 -1
  99. package/lib/plugins/trackTransaction.js +1 -1
  100. package/lib/query.js +61 -6
  101. package/lib/queryhelpers.js +5 -0
  102. package/lib/schema/array.js +1 -1
  103. package/lib/schema/date.js +2 -2
  104. package/lib/schema/documentarray.js +10 -0
  105. package/lib/schema/number.js +2 -2
  106. package/lib/schema/string.js +2 -2
  107. package/lib/schema/symbols.js +1 -1
  108. package/lib/schema.js +6 -7
  109. package/lib/statemachine.js +13 -0
  110. package/lib/types/DocumentArray/methods/index.js +1 -1
  111. package/lib/utils.js +3 -0
  112. package/lib/validoptions.js +1 -0
  113. package/package.json +3 -3
  114. package/tsconfig.json +1 -0
  115. package/types/collection.d.ts +1 -1
  116. package/types/connection.d.ts +5 -0
  117. package/types/document.d.ts +15 -6
  118. package/types/expressions.d.ts +2882 -0
  119. package/types/index.d.ts +46 -17
  120. package/types/{indizes.d.ts → indexes.d.ts} +2 -2
  121. package/types/inferschematype.d.ts +173 -0
  122. package/types/middlewares.d.ts +1 -1
  123. package/types/models.d.ts +86 -71
  124. package/types/mongooseoptions.d.ts +9 -1
  125. package/types/pipelinestage.d.ts +76 -80
  126. package/types/query.d.ts +1 -1
  127. package/types/schemaoptions.d.ts +22 -4
  128. package/types/types.d.ts +3 -1
  129. package/types/utility.d.ts +9 -0
@@ -26,4 +26,4 @@ module.exports = function wrapThunk(fn) {
26
26
 
27
27
  fn.call(this, cb);
28
28
  };
29
- };
29
+ };
@@ -4,4 +4,4 @@ module.exports = function addAutoId(schema) {
4
4
  const _obj = { _id: { auto: true } };
5
5
  _obj._id[schema.options.typeKey] = 'ObjectId';
6
6
  schema.add(_obj);
7
- };
7
+ };
@@ -52,4 +52,4 @@ module.exports = function applyPlugins(schema, plugins, options, cacheKey) {
52
52
  applyPlugins(discriminatorSchema, plugins,
53
53
  { skipTopLevel: !applyPluginsToDiscriminators }, cacheKey);
54
54
  }
55
- };
55
+ };
@@ -9,4 +9,4 @@ module.exports = function cleanPositionalOperators(path) {
9
9
  return path.
10
10
  replace(/\.\$(\[[^\]]*\])?(?=\.)/g, '.0').
11
11
  replace(/\.\$(\[[^\]]*\])?$/g, '.0');
12
- };
12
+ };
@@ -25,4 +25,4 @@ module.exports = function getKeysInSchemaOrder(schema, val, path) {
25
25
  }
26
26
 
27
27
  return keys;
28
- };
28
+ };
@@ -34,4 +34,4 @@ module.exports = function getPath(schema, path) {
34
34
  }
35
35
 
36
36
  return schematype;
37
- };
37
+ };
@@ -17,4 +17,4 @@ module.exports = function handleIdOption(schema, options) {
17
17
  }
18
18
 
19
19
  return schema;
20
- };
20
+ };
@@ -21,4 +21,4 @@ function handleTimestampOption(arg, prop) {
21
21
  return prop;
22
22
  }
23
23
  return arg[prop];
24
- }
24
+ }
@@ -1,3 +1,3 @@
1
1
  'use strict';
2
2
 
3
- module.exports = new Set(['__proto__', 'constructor', 'prototype']);
3
+ module.exports = new Set(['__proto__', 'constructor', 'prototype']);
@@ -17,4 +17,4 @@ exports.populateModelSymbol = Symbol('mongoose.PopulateOptions#Model');
17
17
  exports.schemaTypeSymbol = Symbol('mongoose#schemaType');
18
18
  exports.sessionNewDocuments = Symbol('mongoose:ClientSession#newDocuments');
19
19
  exports.scopeSymbol = Symbol('mongoose#Document#scope');
20
- exports.validatorErrorSymbol = Symbol('mongoose:validatorError');
20
+ exports.validatorErrorSymbol = Symbol('mongoose:validatorError');
@@ -1,3 +1,3 @@
1
1
  'use strict';
2
2
 
3
- exports.setTimeout = setTimeout;
3
+ exports.setTimeout = setTimeout;
@@ -31,8 +31,11 @@ module.exports = function setupTimestamps(schema, timestamps) {
31
31
  }
32
32
 
33
33
  if (createdAt && !schema.paths[createdAt]) {
34
- schemaAdditions[createdAt] = { [schema.options.typeKey || 'type']: Date, immutable: true };
34
+ const baseImmutableCreatedAt = schema.base.get('timestamps.createdAt.immutable');
35
+ const immutable = baseImmutableCreatedAt != null ? baseImmutableCreatedAt : true;
36
+ schemaAdditions[createdAt] = { [schema.options.typeKey || 'type']: Date, immutable };
35
37
  }
38
+
36
39
  schema.add(schemaAdditions);
37
40
 
38
41
  schema.pre('save', function(next) {
@@ -99,4 +102,4 @@ module.exports = function setupTimestamps(schema, timestamps) {
99
102
  applyTimestampsToChildren(now, this.getUpdate(), this.model.schema);
100
103
  next();
101
104
  }
102
- };
105
+ };
@@ -9,4 +9,4 @@ module.exports = function allServersUnknown(topologyDescription) {
9
9
 
10
10
  const servers = Array.from(topologyDescription.servers.values());
11
11
  return servers.length > 0 && servers.every(server => server.type === 'Unknown');
12
- };
12
+ };
@@ -23,4 +23,4 @@ module.exports = function isAtlas(topologyDescription) {
23
23
  }
24
24
  }
25
25
  return true;
26
- };
26
+ };
@@ -13,4 +13,4 @@ module.exports = function isSSLError(topologyDescription) {
13
13
  const descriptions = Array.from(topologyDescription.servers.values());
14
14
  return descriptions.length > 0 &&
15
15
  descriptions.every(descr => descr.error && descr.error.message.indexOf(nonSSLMessage) !== -1);
16
- };
16
+ };
@@ -186,4 +186,4 @@ function applyTimestampsToUpdateKey(schema, key, update, now) {
186
186
  update[key][createdAt] = now;
187
187
  }
188
188
  }
189
- }
189
+ }
@@ -80,4 +80,4 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
80
80
  }
81
81
  }
82
82
  }
83
- }
83
+ }
@@ -50,4 +50,4 @@ function _walkUpdatePath(schema, op, path, update, ctx) {
50
50
  update.$setOnInsert = update.$setOnInsert || {};
51
51
  update.$setOnInsert[path] = op[path];
52
52
  delete op[path];
53
- }
53
+ }
@@ -29,4 +29,4 @@ function _checkSingleFilterKey(arrayFilter, updateKeys) {
29
29
  const arrayFilterKey = firstDot === -1 ? firstKey : firstKey.slice(0, firstDot);
30
30
 
31
31
  return updateKeys.find(key => key.includes('$[' + arrayFilterKey + ']')) != null;
32
- }
32
+ }
@@ -24,4 +24,4 @@ module.exports = function updatedPathsByArrayFilter(update) {
24
24
  }
25
25
  return cur;
26
26
  }, {});
27
- };
27
+ };
@@ -125,26 +125,19 @@ module.exports = function(query, schema, castedDoc, options, callback) {
125
125
  validatorsToExecute.push(function(callback) {
126
126
  schemaPath.doValidate(v, function(err) {
127
127
  if (err) {
128
- err.path = updates[i];
129
- validationErrors.push(err);
130
- return callback(null);
131
- }
132
-
133
- v.validate(function(err) {
134
- if (err) {
135
- if (err.errors) {
136
- for (const key of Object.keys(err.errors)) {
137
- const _err = err.errors[key];
138
- _err.path = updates[i] + '.' + key;
139
- validationErrors.push(_err);
140
- }
141
- } else {
142
- err.path = updates[i];
143
- validationErrors.push(err);
128
+ if (err.errors) {
129
+ for (const key of Object.keys(err.errors)) {
130
+ const _err = err.errors[key];
131
+ _err.path = updates[i] + '.' + key;
132
+ validationErrors.push(_err);
144
133
  }
134
+ } else {
135
+ err.path = updates[i];
136
+ validationErrors.push(err);
145
137
  }
146
- callback(null);
147
- });
138
+ }
139
+
140
+ return callback(null);
148
141
  }, context, { updateValidator: true });
149
142
  });
150
143
  } else {
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ require('./driver').set(require('./drivers/node-mongodb-native'));
8
8
 
9
9
  const Document = require('./document');
10
10
  const EventEmitter = require('events').EventEmitter;
11
+ const Kareem = require('kareem');
11
12
  const Schema = require('./schema');
12
13
  const SchemaType = require('./schematype');
13
14
  const SchemaTypes = require('./schema/index');
@@ -35,6 +36,7 @@ const shardingPlugin = require('./plugins/sharding');
35
36
  const trusted = require('./helpers/query/trusted').trusted;
36
37
  const sanitizeFilter = require('./helpers/query/sanitizeFilter');
37
38
  const isBsonType = require('./helpers/isBsonType');
39
+ const MongooseError = require('./error/mongooseError');
38
40
 
39
41
  const defaultMongooseSymbol = Symbol.for('mongoose:default');
40
42
 
@@ -62,6 +64,7 @@ function Mongoose(options) {
62
64
  this.connections = [];
63
65
  this.models = {};
64
66
  this.events = new EventEmitter();
67
+ this.__driver = driver.get();
65
68
  // default global options
66
69
  this.options = Object.assign({
67
70
  pluralization: true,
@@ -135,6 +138,7 @@ Mongoose.prototype.ConnectionStates = STATES;
135
138
  * uses to communicate with the database. A driver is a Mongoose-specific interface that defines functions
136
139
  * like `find()`.
137
140
  *
141
+ * @deprecated
138
142
  * @memberOf Mongoose
139
143
  * @property driver
140
144
  * @api public
@@ -142,6 +146,36 @@ Mongoose.prototype.ConnectionStates = STATES;
142
146
 
143
147
  Mongoose.prototype.driver = driver;
144
148
 
149
+ /**
150
+ * Overwrites the current driver used by this Mongoose instance. A driver is a
151
+ * Mongoose-specific interface that defines functions like `find()`.
152
+ *
153
+ * @memberOf Mongoose
154
+ * @method setDriver
155
+ * @api public
156
+ */
157
+
158
+ Mongoose.prototype.setDriver = function setDriver(driver) {
159
+ const _mongoose = this instanceof Mongoose ? this : mongoose;
160
+
161
+ if (_mongoose.__driver === driver) {
162
+ return _mongoose;
163
+ }
164
+
165
+ const openConnection = _mongoose.connections && _mongoose.connections.find(conn => conn.readyState !== STATES.disconnected);
166
+ if (openConnection) {
167
+ const msg = 'Cannot modify Mongoose driver if a connection is already open. ' +
168
+ 'Call `mongoose.disconnect()` before modifying the driver';
169
+ throw new MongooseError(msg);
170
+ }
171
+ _mongoose.__driver = driver;
172
+
173
+ const Connection = driver.getConnection();
174
+ _mongoose.connections = [new Connection(_mongoose)];
175
+
176
+ return _mongoose;
177
+ };
178
+
145
179
  /**
146
180
  * Sets mongoose options
147
181
  *
@@ -154,23 +188,26 @@ Mongoose.prototype.driver = driver;
154
188
  * mongoose.set('debug', function(collectionName, methodName, ...methodArgs) {}); // use custom function to log collection methods + arguments
155
189
  *
156
190
  * Currently supported options are:
191
+ * - 'applyPluginsToChildSchemas': `true` by default. Set to false to skip applying global plugins to child schemas
192
+ * - 'applyPluginsToDiscriminators': `false` by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
193
+ * - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model.createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist.
194
+ * - 'autoIndex': `true` by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
157
195
  * - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arguments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
158
196
  * - 'returnOriginal': If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`, `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to setting the `new` option to `true` for `findOneAndX()` calls by default. Read our [`findOneAndUpdate()` tutorial](/docs/tutorials/findoneandupdate.html) for more information.
159
197
  * - 'bufferCommands': enable/disable mongoose's buffering mechanism for all connections and models
160
- * - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model.
161
- * - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
162
- * - 'applyPluginsToChildSchemas': true by default. Set to false to skip applying global plugins to child schemas
163
- * - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
164
- * - 'runValidators': false by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default.
165
- * - 'toObject': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toObject()`](/docs/api.html#document_Document-toObject)
166
- * - 'toJSON': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toJSON()`](/docs/api.html#document_Document-toJSON), for determining how Mongoose documents get serialized by `JSON.stringify()`
167
- * - 'strict': true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas.
168
- * - 'strictQuery': same value as 'strict' by default (`true`), may be `false`, `true`, or `'throw'`. Sets the default [strictQuery](/docs/guide.html#strictQuery) mode for schemas.
169
- * - 'selectPopulatedPaths': true by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
198
+ * - 'cloneSchemas': `false` by default. Set to `true` to `clone()` all schemas before compiling into a model.
199
+ * - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arugments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
200
+ * - 'timestamps.createdAt.immutable': `true` by default. If `false`, it will change the `createdAt` field to be [`immutable: false`](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-immutable) which means you can update the `createdAt`
170
201
  * - 'maxTimeMS': If set, attaches [maxTimeMS](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/) to every query
171
- * - 'autoIndex': true by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
172
- * - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model.createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist.
202
+ * - 'objectIdGetter': `true` by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
173
203
  * - 'overwriteModels': Set to `true` to default to overwriting models with the same name when calling `mongoose.model()`, as opposed to throwing an `OverwriteModelError`.
204
+ * - 'returnOriginal': If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`, `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to setting the `new` option to `true` for `findOneAndX()` calls by default. Read our [`findOneAndUpdate()` tutorial](/docs/tutorials/findoneandupdate.html) for more information.
205
+ * - 'runValidators': `false` by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default.
206
+ * - 'selectPopulatedPaths': `true` by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
207
+ * - 'strict': `true` by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas.
208
+ * - 'strictQuery': same value as 'strict' by default (`true`), may be `false`, `true`, or `'throw'`. Sets the default [strictQuery](/docs/guide.html#strictQuery) mode for schemas.
209
+ * - 'toJSON': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toJSON()`](/docs/api.html#document_Document-toJSON), for determining how Mongoose documents get serialized by `JSON.stringify()`
210
+ * - 'toObject': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toObject()`](/docs/api.html#document_Document-toObject)
174
211
  *
175
212
  * @param {String} key
176
213
  * @param {String|Function|Boolean} value
@@ -273,7 +310,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
273
310
  Mongoose.prototype.createConnection = function(uri, options, callback) {
274
311
  const _mongoose = this instanceof Mongoose ? this : mongoose;
275
312
 
276
- const Connection = driver.get().getConnection();
313
+ const Connection = _mongoose.__driver.getConnection();
277
314
  const conn = new Connection(_mongoose);
278
315
  if (typeof options === 'function') {
279
316
  callback = options;
@@ -467,7 +504,6 @@ Mongoose.prototype.pluralize = function(fn) {
467
504
  */
468
505
 
469
506
  Mongoose.prototype.model = function(name, schema, collection, options) {
470
-
471
507
  const _mongoose = this instanceof Mongoose ? this : mongoose;
472
508
 
473
509
  if (typeof schema === 'string') {
@@ -684,7 +720,7 @@ Mongoose.prototype.__defineGetter__('connection', function() {
684
720
  });
685
721
 
686
722
  Mongoose.prototype.__defineSetter__('connection', function(v) {
687
- if (v instanceof Connection) {
723
+ if (v instanceof this.__driver.getConnection()) {
688
724
  this.connections[0] = v;
689
725
  this.models = v.models;
690
726
  }
@@ -713,18 +749,6 @@ Mongoose.prototype.__defineSetter__('connection', function(v) {
713
749
 
714
750
  Mongoose.prototype.connections;
715
751
 
716
- /*!
717
- * Connection
718
- */
719
-
720
- const Connection = driver.get().getConnection();
721
-
722
- /*!
723
- * Collection
724
- */
725
-
726
- const Collection = driver.get().Collection;
727
-
728
752
  /**
729
753
  * The Mongoose Aggregate constructor
730
754
  *
@@ -741,7 +765,14 @@ Mongoose.prototype.Aggregate = Aggregate;
741
765
  * @api public
742
766
  */
743
767
 
744
- Mongoose.prototype.Collection = Collection;
768
+ Object.defineProperty(Mongoose.prototype, 'Collection', {
769
+ get: function() {
770
+ return this.__driver.Collection;
771
+ },
772
+ set: function(Collection) {
773
+ this.__driver.Collection = Collection;
774
+ }
775
+ });
745
776
 
746
777
  /**
747
778
  * The Mongoose [Connection](#connection_Connection) constructor
@@ -752,7 +783,18 @@ Mongoose.prototype.Collection = Collection;
752
783
  * @api public
753
784
  */
754
785
 
755
- Mongoose.prototype.Connection = Connection;
786
+ Object.defineProperty(Mongoose.prototype, 'Connection', {
787
+ get: function() {
788
+ return this.__driver.getConnection();
789
+ },
790
+ set: function(Connection) {
791
+ if (Connection === this.__driver.getConnection()) {
792
+ return;
793
+ }
794
+
795
+ this.__driver.getConnection = () => Connection;
796
+ }
797
+ });
756
798
 
757
799
  /**
758
800
  * The Mongoose version
@@ -1178,6 +1220,43 @@ Mongoose.prototype._promiseOrCallback = function(callback, fn, ee) {
1178
1220
  return promiseOrCallback(callback, fn, ee, this.Promise);
1179
1221
  };
1180
1222
 
1223
+ /**
1224
+ * Use this function in `pre()` middleware to skip calling the wrapped function.
1225
+ *
1226
+ * #### Example:
1227
+ *
1228
+ * schema.pre('save', function() {
1229
+ * // Will skip executing `save()`, but will execute post hooks as if
1230
+ * // `save()` had executed with the result `{ matchedCount: 0 }`
1231
+ * return mongoose.skipMiddlewareFunction({ matchedCount: 0 });
1232
+ * });
1233
+ *
1234
+ * @method skipMiddlewareFunction
1235
+ * @param {any} result
1236
+ * @api public
1237
+ */
1238
+
1239
+ Mongoose.prototype.skipMiddlewareFunction = Kareem.skipWrappedFunction;
1240
+
1241
+ /**
1242
+ * Use this function in `post()` middleware to replace the result
1243
+ *
1244
+ * #### Example:
1245
+ *
1246
+ * schema.post('find', function(res) {
1247
+ * // Normally you have to modify `res` in place. But with
1248
+ * // `overwriteMiddlewarResult()`, you can make `find()` return a
1249
+ * // completely different value.
1250
+ * return mongoose.overwriteMiddlewareResult(res.filter(doc => !doc.isDeleted));
1251
+ * });
1252
+ *
1253
+ * @method overwriteMiddlewareResult
1254
+ * @param {any} result
1255
+ * @api public
1256
+ */
1257
+
1258
+ Mongoose.prototype.overwriteMiddlewareResult = Kareem.overwriteResult;
1259
+
1181
1260
  /*!
1182
1261
  * The exports object is an instance of Mongoose.
1183
1262
  *
package/lib/model.js CHANGED
@@ -4281,7 +4281,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
4281
4281
 
4282
4282
  for (const path of paths) {
4283
4283
  const schemaType = schema.path(path);
4284
- if (!schemaType || !schemaType.$isMongooseArray) {
4284
+ if (!schemaType || !schemaType.$isMongooseArray || schemaType.$isMongooseDocumentArray) {
4285
4285
  continue;
4286
4286
  }
4287
4287
 
@@ -33,4 +33,4 @@ class PopulateOptions {
33
33
  * @api public
34
34
  */
35
35
 
36
- module.exports = PopulateOptions;
36
+ module.exports = PopulateOptions;
@@ -56,4 +56,4 @@ Object.defineProperty(SchemaArrayOptions.prototype, 'of', opts);
56
56
  * ignore
57
57
  */
58
58
 
59
- module.exports = SchemaArrayOptions;
59
+ module.exports = SchemaArrayOptions;
@@ -35,4 +35,4 @@ Object.defineProperty(SchemaBufferOptions.prototype, 'subtype', opts);
35
35
  * ignore
36
36
  */
37
37
 
38
- module.exports = SchemaBufferOptions;
38
+ module.exports = SchemaBufferOptions;
@@ -65,4 +65,4 @@ Object.defineProperty(SchemaDocumentArrayOptions.prototype, '_id', opts);
65
65
  * ignore
66
66
  */
67
67
 
68
- module.exports = SchemaDocumentArrayOptions;
68
+ module.exports = SchemaDocumentArrayOptions;
@@ -40,4 +40,4 @@ const opts = require('./propertyOptions');
40
40
 
41
41
  Object.defineProperty(SchemaMapOptions.prototype, 'of', opts);
42
42
 
43
- module.exports = SchemaMapOptions;
43
+ module.exports = SchemaMapOptions;
@@ -96,4 +96,4 @@ Object.defineProperty(SchemaNumberOptions.prototype, 'populate', opts);
96
96
  * ignore
97
97
  */
98
98
 
99
- module.exports = SchemaNumberOptions;
99
+ module.exports = SchemaNumberOptions;
@@ -60,4 +60,4 @@ Object.defineProperty(SchemaObjectIdOptions.prototype, 'populate', opts);
60
60
  * ignore
61
61
  */
62
62
 
63
- module.exports = SchemaObjectIdOptions;
63
+ module.exports = SchemaObjectIdOptions;
@@ -39,4 +39,4 @@ const opts = require('./propertyOptions');
39
39
 
40
40
  Object.defineProperty(SchemaSubdocumentOptions.prototype, '_id', opts);
41
41
 
42
- module.exports = SchemaSubdocumentOptions;
42
+ module.exports = SchemaSubdocumentOptions;
@@ -241,4 +241,4 @@ Object.defineProperty(SchemaTypeOptions.prototype, 'text', opts);
241
241
 
242
242
  Object.defineProperty(SchemaTypeOptions.prototype, 'transform', opts);
243
243
 
244
- module.exports = SchemaTypeOptions;
244
+ module.exports = SchemaTypeOptions;
@@ -161,4 +161,4 @@ Object.defineProperty(VirtualOptions.prototype, 'limit', opts);
161
161
 
162
162
  Object.defineProperty(VirtualOptions.prototype, 'perDocumentLimit', opts);
163
163
 
164
- module.exports = VirtualOptions;
164
+ module.exports = VirtualOptions;
@@ -5,4 +5,4 @@ module.exports = Object.freeze({
5
5
  configurable: true,
6
6
  writable: true,
7
7
  value: void 0
8
- });
8
+ });
@@ -11,4 +11,4 @@ class RemoveOptions {
11
11
  }
12
12
  }
13
13
 
14
- module.exports = RemoveOptions;
14
+ module.exports = RemoveOptions;
@@ -11,4 +11,4 @@ class SaveOptions {
11
11
  }
12
12
  }
13
13
 
14
- module.exports = SaveOptions;
14
+ module.exports = SaveOptions;
@@ -89,4 +89,4 @@ function mergeAtomics(destination, source) {
89
89
  }
90
90
 
91
91
  return destination;
92
- }
92
+ }