Skip to content

BREAKING CHANGE: require passing both filter and update to Query.prototype.updateOne(), make Document.prototype.init() fully sync#16274

Open
vkarpov15 wants to merge 8 commits into
10.0from
vkarpov15/gh-16269
Open

BREAKING CHANGE: require passing both filter and update to Query.prototype.updateOne(), make Document.prototype.init() fully sync#16274
vkarpov15 wants to merge 8 commits into
10.0from
vkarpov15/gh-16269

Conversation

@vkarpov15
Copy link
Copy Markdown
Collaborator

Fix #16269

Summary

Remove some more callback-based internals: Document.prototype.init() has no reason to support callbacks it is fully sync. Likewise, logic that uses it, like completeOne(), doesn't necessarily need to be async unless there's populate() involved.

Also, for consistency with our TypeScript types and Model.updateX(); Query.prototype.updateOne(), updateMany(), and replaceOne() now require both a filter and update/replacement argument. To avoid any confusion with our legacy behavior of Query.prototype.updateOne(obj) treating obj as a update not a filter.

Examples

…otype.updateOne(), make Document.prototype.init() fully sync

Fix #16269
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes remaining callback-based internals by making document hydration (Document#init()) fully synchronous, and introduces a breaking API change that requires explicitly passing both filter and update/replacement to Query.prototype.updateOne(), updateMany(), and replaceOne() to match the TypeScript signatures and avoid legacy ambiguity.

Changes:

  • Make Document.prototype.init() synchronous-only (no callback) and update internal hydration helpers accordingly.
  • Refactor query hydration completion paths (_completeOne, completeOne, _completeMany, _completeOneLean) to remove callback plumbing.
  • Enforce Query.prototype.updateOne()/updateMany()/replaceOne() requiring at least 2 args and rejecting callbacks; update affected tests.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
lib/query.js Removes callback-based hydration completion, enforces new Query update* signatures, and adjusts lean completion logic.
lib/queryHelpers.js Makes createModelAndInit() synchronous and return the hydrated model instance.
lib/document.js Makes Document#init() synchronous-only and removes callback support.
lib/cursor/queryCursor.js Adapts cursor hydration to new synchronous createModelAndInit() with try/catch error handling.
test/query.test.js Updates pre-hook error test to use new Query#updateOne(filter, update) signature and exec(op).
test/model.updateOne.test.js Updates tests to pass explicit filter/update for query update methods and adjusts related coverage.
Comments suppressed due to low confidence (2)

lib/query.js:4300

  • The JSDoc examples for Query.prototype.updateMany() still show chaining with a single argument (treating it as the update). Since this PR makes filter + update required, these examples should be updated (for example, pass {} as the filter when using chaining syntax) to avoid documenting an invalid signature.
 * @param {object} filter the filter to match documents to update
 * @param {object|Array} update the update command. If array, this update will be treated as an update pipeline and not casted.
 * @param {object} [options]
 * @param {boolean} [options.cloneUpdate=true] if `false`, Mongoose will not clone the update before executing the query
 * @param {boolean} [options.multipleCastError] by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
 * @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)

lib/query.js:4359

  • The JSDoc examples for Query.prototype.updateOne() still show findOne(...).updateOne(update) and updateOne(update) as supported syntaxes, but this PR makes filter + update required. Please update or remove those examples so the docs match the new runtime behavior.
 * @param {object} filter
 * @param {object|Array} update the update command. If array, this update will be treated as an update pipeline and not casted.
 * @param {object} [options]
 * @param {boolean} [options.cloneUpdate=true] if `false`, Mongoose will not clone the update before executing the query
 * @param {boolean} [options.multipleCastError] by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
 * @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)

Comment thread lib/query.js
Comment on lines +2699 to +2703
return model.populate(doc, pop).then(doc => _completeOneLean(model.schema, doc, null, res, options));
}

completeOne(model, doc, res, options, projection, userProvidedFields, [], (err, doc) => {
if (err != null) {
return callback(err);
}
model.populate(doc, pop).then(res => { callback(null, res); }, err => { callback(err); });
});
doc = completeOne(model, doc, res, options, projection, userProvidedFields, []);
return model.populate(doc, pop);
Comment thread lib/query.js Outdated
Comment thread lib/query.js Outdated
Comment thread lib/query.js Outdated
Comment thread test/model.updateOne.test.js Outdated
Comment thread test/model.updateOne.test.js Outdated
Comment thread lib/query.js Outdated
Comment thread lib/document.js
Copy link
Copy Markdown
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good change, but the Copilot comments should likely be investigated, also slight style suggestion.

Comment thread lib/query.js Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants