Changes
This commit is contained in:
168
node_modules/mongoose/lib/model.js
generated
vendored
168
node_modules/mongoose/lib/model.js
generated
vendored
@@ -436,7 +436,7 @@ function generateVersionError(doc, modifiedPaths) {
|
||||
* @param {Session} [options.session=null] the [session](https://docs.mongodb.com/manual/reference/server-sessions/) associated with this save operation. If not specified, defaults to the [document's associated session](api.html#document_Document-$session).
|
||||
* @param {Object} [options.safe] (DEPRECATED) overrides [schema's safe option](http://mongoosejs.com//docs/guide.html#safe). Use the `w` option instead.
|
||||
* @param {Boolean} [options.validateBeforeSave] set to false to save without validating.
|
||||
* @param {Boolean} [options.validateModifiedOnly=false] if `true` mongoose validates only modified paths.
|
||||
* @param {Boolean} [options.validateModifiedOnly=false] if `true`, Mongoose will only validate modified paths, as opposed to modified paths and `required` paths.
|
||||
* @param {Number|String} [options.w] set the [write concern](https://docs.mongodb.com/manual/reference/write-concern/#w-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern)
|
||||
* @param {Boolean} [options.j] set to true for MongoDB to wait until this `save()` has been [journaled before resolving the returned promise](https://docs.mongodb.com/manual/reference/write-concern/#j-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern)
|
||||
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern).
|
||||
@@ -542,6 +542,11 @@ function operand(self, where, delta, data, val, op) {
|
||||
// already marked for versioning?
|
||||
if (VERSION_ALL === (VERSION_ALL & self.$__.version)) return;
|
||||
|
||||
if (self.schema.options.optimisticConcurrency) {
|
||||
self.$__.version = VERSION_ALL;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (op) {
|
||||
case '$set':
|
||||
case '$unset':
|
||||
@@ -1357,7 +1362,7 @@ Model.syncIndexes = function syncIndexes(options, callback) {
|
||||
cb = this.$wrapCallback(cb);
|
||||
|
||||
this.createCollection(err => {
|
||||
if (err) {
|
||||
if (err != null && err.codeName !== 'NamespaceExists') {
|
||||
return cb(err);
|
||||
}
|
||||
this.cleanIndexes((err, dropped) => {
|
||||
@@ -1568,7 +1573,7 @@ function _ensureIndexes(model, options, callback) {
|
||||
model.emit('error', err);
|
||||
}
|
||||
model.emit('index', err || indexError);
|
||||
callback && callback(err);
|
||||
callback && callback(err || indexError);
|
||||
};
|
||||
|
||||
for (const index of indexes) {
|
||||
@@ -1976,35 +1981,26 @@ Model.deleteMany = function deleteMany(conditions, options, callback) {
|
||||
/**
|
||||
* Finds documents.
|
||||
*
|
||||
* The `filter` are cast to their respective SchemaTypes before the command is sent.
|
||||
* Mongoose casts the `filter` to match the model's schema before the command is sent.
|
||||
* See our [query casting tutorial](/docs/tutorials/query_casting.html) for
|
||||
* more information on how Mongoose casts `filter`.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* // named john and at least 18
|
||||
* MyModel.find({ name: 'john', age: { $gte: 18 }});
|
||||
* // find all documents
|
||||
* await MyModel.find({});
|
||||
*
|
||||
* // find all documents named john and at least 18
|
||||
* await MyModel.find({ name: 'john', age: { $gte: 18 } }).exec();
|
||||
*
|
||||
* // executes, passing results to callback
|
||||
* MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
|
||||
*
|
||||
* // executes, name LIKE john and only selecting the "name" and "friends" fields
|
||||
* MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })
|
||||
* await MyModel.find({ name: /john/i }, 'name friends').exec();
|
||||
*
|
||||
* // passing options
|
||||
* MyModel.find({ name: /john/i }, null, { skip: 10 })
|
||||
*
|
||||
* // passing options and executes
|
||||
* MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});
|
||||
*
|
||||
* // executing a query explicitly
|
||||
* var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
|
||||
* query.exec(function (err, docs) {});
|
||||
*
|
||||
* // using the promise returned from executing a query
|
||||
* var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
|
||||
* var promise = query.exec();
|
||||
* promise.addBack(function (err, docs) {});
|
||||
* await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();
|
||||
*
|
||||
* @param {Object|ObjectId} filter
|
||||
* @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](http://mongoosejs.com/docs/api.html#query_Query-select)
|
||||
@@ -2067,26 +2063,14 @@ Model.find = function find(conditions, projection, options, callback) {
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // find adventure by id and execute
|
||||
* // Find the adventure with the given `id`, or `null` if not found
|
||||
* await Adventure.findById(id).exec();
|
||||
*
|
||||
* // using callback
|
||||
* Adventure.findById(id, function (err, adventure) {});
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findById(id).exec(callback);
|
||||
*
|
||||
* // select only the adventures name and length
|
||||
* Adventure.findById(id, 'name length', function (err, adventure) {});
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findById(id, 'name length').exec(callback);
|
||||
*
|
||||
* // include all properties except for `length`
|
||||
* Adventure.findById(id, '-length').exec(function (err, adventure) {});
|
||||
*
|
||||
* // passing options (in this case return the raw js objects, not mongoose documents by passing `lean`
|
||||
* Adventure.findById(id, 'name', { lean: true }, function (err, doc) {});
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findById(id, 'name').lean().exec(function (err, doc) {});
|
||||
* await Adventure.findById(id, 'name length').exec();
|
||||
*
|
||||
* @param {Any} id value of `_id` to query by
|
||||
* @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
||||
@@ -2122,26 +2106,14 @@ Model.findById = function findById(id, projection, options, callback) {
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // find one iphone adventures - iphone adventures??
|
||||
* Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});
|
||||
* // Find one adventure whose `country` is 'Croatia', otherwise `null`
|
||||
* await Adventure.findOne({ country: 'Croatia' }).exec();
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findOne({ type: 'iphone' }).exec(function (err, adventure) {});
|
||||
* // using callback
|
||||
* Adventure.findOne({ country: 'Croatia' }, function (err, adventure) {});
|
||||
*
|
||||
* // select only the adventures name
|
||||
* Adventure.findOne({ type: 'iphone' }, 'name', function (err, adventure) {});
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findOne({ type: 'iphone' }, 'name').exec(function (err, adventure) {});
|
||||
*
|
||||
* // specify options, in this case lean
|
||||
* Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }, callback);
|
||||
*
|
||||
* // same as above
|
||||
* Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }).exec(callback);
|
||||
*
|
||||
* // chaining findOne queries (same as above)
|
||||
* Adventure.findOne({ type: 'iphone' }).select('name').lean().exec(callback);
|
||||
* // select only the adventures name and length
|
||||
* await Adventure.findOne({ country: 'Croatia' }, 'name length').exec();
|
||||
*
|
||||
* @param {Object} [conditions]
|
||||
* @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
||||
@@ -2445,7 +2417,7 @@ Model.$where = function $where() {
|
||||
* @param {Object} [conditions]
|
||||
* @param {Object} [update]
|
||||
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
||||
* @param {Boolean} [options.new=false] By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied.
|
||||
* @param {Boolean} [options.new=false] By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied. To change the default to `true`, use `mongoose.set('returnOriginal', false);`.
|
||||
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and [the Mongoose lean tutorial](/docs/tutorials/lean.html).
|
||||
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
||||
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
|
||||
@@ -2588,7 +2560,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
||||
* @param {Object|Number|String} id value of `_id` to query by
|
||||
* @param {Object} [update]
|
||||
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
||||
* @param {Boolean} [options.new=false] By default, `findByIdAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied.
|
||||
* @param {Boolean} [options.new=false] By default, `findByIdAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied. To change the default to `true`, use `mongoose.set('returnOriginal', false);`.
|
||||
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and [the Mongoose lean tutorial](/docs/tutorials/lean.html).
|
||||
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
||||
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
|
||||
@@ -2787,7 +2759,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
||||
* @param {Object} filter Replace the first document that matches this filter
|
||||
* @param {Object} [replacement] Replace with this document
|
||||
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
||||
* @param {Boolean} [options.new=false] By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied.
|
||||
* @param {Boolean} [options.new=false] By default, `findOneAndReplace()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndReplace()` will instead give you the object after `update` was applied. To change the default to `true`, use `mongoose.set('returnOriginal', false);`.
|
||||
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and [the Mongoose lean tutorial](/docs/tutorials/lean.html).
|
||||
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
||||
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
|
||||
@@ -2981,26 +2953,16 @@ Model.findByIdAndRemove = function(id, options, callback) {
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // pass a spread of docs and a callback
|
||||
* Candy.create({ type: 'jelly bean' }, { type: 'snickers' }, function (err, jellybean, snickers) {
|
||||
* if (err) // ...
|
||||
* });
|
||||
* // Insert one new `Character` document
|
||||
* await Character.create({ name: 'Jean-Luc Picard' });
|
||||
*
|
||||
* // pass an array of docs
|
||||
* var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
|
||||
* Candy.create(array, function (err, candies) {
|
||||
* if (err) // ...
|
||||
* // Insert multiple new `Character` documents
|
||||
* await Character.create([{ name: 'Will Riker' }, { name: 'Geordi LaForge' }]);
|
||||
*
|
||||
* var jellybean = candies[0];
|
||||
* var snickers = candies[1];
|
||||
* // ...
|
||||
* });
|
||||
*
|
||||
* // callback is optional; use the returned promise if you like:
|
||||
* var promise = Candy.create({ type: 'jawbreaker' });
|
||||
* promise.then(function (jawbreaker) {
|
||||
* // ...
|
||||
* })
|
||||
* // Create a new character within a transaction. Note that you **must**
|
||||
* // pass an array as the first parameter to `create()` if you want to
|
||||
* // specify options.
|
||||
* await Character.create([{ name: 'Jean-Luc Picard' }], { session });
|
||||
*
|
||||
* @param {Array|Object} docs Documents to insert, as a spread or array
|
||||
* @param {Object} [options] Options passed down to `save()`. To specify `options`, `docs` **must** be an array, not a spread.
|
||||
@@ -3529,14 +3491,15 @@ Model.bulkWrite = function(ops, options, callback) {
|
||||
* var mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @param {Object|String} [projection] optional projection containing which fields should be selected for this document
|
||||
* @return {Document} document instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Model.hydrate = function(obj) {
|
||||
Model.hydrate = function(obj, projection) {
|
||||
_checkContext(this, 'hydrate');
|
||||
|
||||
const model = require('./queryhelpers').createModel(this, obj);
|
||||
const model = require('./queryhelpers').createModel(this, obj, projection);
|
||||
model.init(obj);
|
||||
return model;
|
||||
};
|
||||
@@ -4462,6 +4425,9 @@ function populate(model, docs, options, callback) {
|
||||
for (const arr of params) {
|
||||
const mod = arr[0];
|
||||
const assignmentOpts = arr[3];
|
||||
for (const val of vals) {
|
||||
mod.options._childDocs.push(val);
|
||||
}
|
||||
_assign(model, vals, mod, assignmentOpts);
|
||||
}
|
||||
|
||||
@@ -4521,7 +4487,16 @@ function _execPopulateQuery(mod, match, select, assignmentOpts, callback) {
|
||||
query.populate(subPopulate);
|
||||
}
|
||||
|
||||
query.exec(callback);
|
||||
query.exec((err, docs) => {
|
||||
if (err != null) {
|
||||
return callback(err);
|
||||
}
|
||||
for (const val of docs) {
|
||||
leanPopulateMap.set(val, mod.model);
|
||||
}
|
||||
|
||||
callback(null, docs);
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -4598,9 +4573,7 @@ function _assign(model, vals, mod, assignmentOpts) {
|
||||
}
|
||||
}
|
||||
// flag each as result of population
|
||||
if (lean) {
|
||||
leanPopulateMap.set(val, mod.model);
|
||||
} else {
|
||||
if (!lean) {
|
||||
val.$__.wasPopulated = true;
|
||||
}
|
||||
}
|
||||
@@ -4620,6 +4593,7 @@ function _assign(model, vals, mod, assignmentOpts) {
|
||||
justOne: mod.justOne,
|
||||
isVirtual: mod.isVirtual,
|
||||
allOptions: mod,
|
||||
populatedModel: mod.model,
|
||||
lean: lean,
|
||||
virtual: mod.virtual,
|
||||
count: mod.count,
|
||||
@@ -4736,23 +4710,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
||||
|
||||
const _userProvidedOptions = schema._userProvidedOptions || {};
|
||||
|
||||
// `bufferCommands` is true by default...
|
||||
let bufferCommands = true;
|
||||
// First, take the global option
|
||||
if (connection.base.get('bufferCommands') != null) {
|
||||
bufferCommands = connection.base.get('bufferCommands');
|
||||
}
|
||||
// Connection-specific overrides the global option
|
||||
if (connection.config.bufferCommands != null) {
|
||||
bufferCommands = connection.config.bufferCommands;
|
||||
}
|
||||
// And schema options override global and connection
|
||||
if (_userProvidedOptions.bufferCommands != null) {
|
||||
bufferCommands = _userProvidedOptions.bufferCommands;
|
||||
}
|
||||
|
||||
const collectionOptions = {
|
||||
bufferCommands: bufferCommands,
|
||||
schemaUserProvidedOptions: _userProvidedOptions,
|
||||
capped: schema.options.capped,
|
||||
autoCreate: schema.options.autoCreate,
|
||||
Promise: model.base.Promise
|
||||
@@ -4846,17 +4805,8 @@ Model.__subclass = function subclass(conn, schema, collection) {
|
||||
utils.toCollectionName(_this.modelName, this.base.pluralize());
|
||||
}
|
||||
|
||||
let bufferCommands = true;
|
||||
if (s) {
|
||||
if (conn.config.bufferCommands != null) {
|
||||
bufferCommands = conn.config.bufferCommands;
|
||||
}
|
||||
if (_userProvidedOptions.bufferCommands != null) {
|
||||
bufferCommands = _userProvidedOptions.bufferCommands;
|
||||
}
|
||||
}
|
||||
const collectionOptions = {
|
||||
bufferCommands: bufferCommands,
|
||||
schemaUserProvidedOptions: _userProvidedOptions,
|
||||
capped: s && options.capped
|
||||
};
|
||||
|
||||
@@ -4898,7 +4848,7 @@ Model.$wrapCallback = function(callback) {
|
||||
if (err != null && err.name === 'MongoServerSelectionError') {
|
||||
arguments[0] = serverSelectionError.assimilateError(err);
|
||||
}
|
||||
if (err != null && err.name === 'MongoNetworkError' && err.message.endsWith('timed out')) {
|
||||
if (err != null && err.name === 'MongoNetworkTimeoutError' && err.message.endsWith('timed out')) {
|
||||
_this.db.emit('timeout');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user