Skip to content

Commit e355b09

Browse files
authored
Merge pull request #16274 from Automattic/vkarpov15/gh-16269
BREAKING CHANGE: require passing both filter and update to Query.prototype.updateOne(), make Document.prototype.init() fully sync
2 parents 1ed1567 + a78762b commit e355b09

7 files changed

Lines changed: 105 additions & 240 deletions

File tree

lib/cursor/queryCursor.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,15 @@ function _nextDoc(ctx, doc, pop, callback) {
595595
}
596596

597597
const { model, _fields, _userProvidedFields, options } = ctx.query;
598-
helpers.createModelAndInit(model, doc, _fields, _userProvidedFields, options, pop, (err, doc) => {
599-
if (err != null) {
600-
return callback(err);
601-
}
598+
try {
599+
doc = helpers.createModelAndInit(model, doc, _fields, _userProvidedFields, options, pop);
602600
if (options.session != null) {
603601
doc.$session(options.session);
604602
}
605-
ctx.model.hooks.execPost('find', ctx.query, [[doc]]).then(() => callback(null, doc), err => callback(err));
606-
});
603+
} catch (err) {
604+
return callback(err);
605+
}
606+
ctx.model.hooks.execPost('find', ctx.query, [[doc]]).then(() => callback(null, doc), err => callback(err));
607607
}
608608

609609
/*!

lib/document.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -639,28 +639,22 @@ Document.prototype.toBSON = function() {
639639
* @param {object} doc raw document returned by mongo
640640
* @param {object} [opts]
641641
* @param {boolean} [opts.hydratedPopulatedDocs=false] If true, hydrate and mark as populated any paths that are populated in the raw document
642-
* @param {Function} [fn]
643642
* @api public
644643
* @memberOf Document
645644
* @instance
646645
*/
647646

648-
Document.prototype.init = function(doc, opts, fn) {
649-
if (typeof opts === 'function') {
650-
fn = opts;
651-
opts = null;
652-
}
653-
647+
Document.prototype.init = function(doc, opts) {
654648
if (doc == null) {
655649
throw new ObjectParameterError(doc, 'doc', 'init');
656650
}
651+
if (typeof opts === 'function' ||
652+
(arguments.length > 2 && typeof arguments[2] === 'function')) {
653+
throw new MongooseError('Document.prototype.init() no longer accepts a callback');
654+
}
657655

658656
this.$__init(doc, opts);
659657

660-
if (fn) {
661-
fn(null, this);
662-
}
663-
664658
return this;
665659
};
666660

lib/model.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,14 +3996,14 @@ Model.hydrate = function hydrate(obj, projection, options) {
39963996
* @api public
39973997
*/
39983998

3999-
Model.updateMany = function updateMany(conditions, update, options) {
3999+
Model.updateMany = function updateMany(filter, update, options) {
40004000
_checkContext(this, 'updateMany');
40014001

40024002
if (update == null) {
40034003
throw new MongooseError('updateMany `update` parameter cannot be nullish');
40044004
}
40054005

4006-
return _update(this, 'updateMany', conditions, update, options);
4006+
return _update(this, 'updateMany', filter, update, options);
40074007
};
40084008

40094009
/**
@@ -4044,10 +4044,10 @@ Model.updateMany = function updateMany(conditions, update, options) {
40444044
* @api public
40454045
*/
40464046

4047-
Model.updateOne = function updateOne(conditions, doc, options) {
4047+
Model.updateOne = function updateOne(filter, update, options) {
40484048
_checkContext(this, 'updateOne');
40494049

4050-
return _update(this, 'updateOne', conditions, doc, options);
4050+
return _update(this, 'updateOne', filter, update, options);
40514051
};
40524052

40534053
/**
@@ -4067,7 +4067,7 @@ Model.updateOne = function updateOne(conditions, doc, options) {
40674067
* - `replaceOne()`
40684068
*
40694069
* @param {object} filter
4070-
* @param {object} doc
4070+
* @param {object} replacement
40714071
* @param {object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
40724072
* @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
40734073
* @param {boolean} [options.upsert=false] if true, and no documents found, insert a new document
@@ -4081,15 +4081,15 @@ Model.updateOne = function updateOne(conditions, doc, options) {
40814081
* @api public
40824082
*/
40834083

4084-
Model.replaceOne = function replaceOne(conditions, doc, options) {
4084+
Model.replaceOne = function replaceOne(filter, replacement, options) {
40854085
_checkContext(this, 'replaceOne');
40864086

40874087
const versionKey = this?.schema?.options?.versionKey || null;
4088-
if (versionKey && !doc[versionKey]) {
4089-
doc[versionKey] = 0;
4088+
if (versionKey && !replacement[versionKey]) {
4089+
replacement[versionKey] = 0;
40904090
}
40914091

4092-
return _update(this, 'replaceOne', conditions, doc, options);
4092+
return _update(this, 'replaceOne', filter, replacement, options);
40934093
};
40944094

40954095
/**

0 commit comments

Comments
 (0)