fix(model): ensure $isDeleted is set after calling doc.deleteOne() successfully#15898
fix(model): ensure $isDeleted is set after calling doc.deleteOne() successfully#15898
doc.deleteOne() successfully#15898Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where doc.$isDeleted() was not being set to true after successfully calling doc.deleteOne(). The fix uses query.transform() to check the deletedCount property in the MongoDB result and sets $isDeleted(true) only when the deletion succeeds.
Key changes:
- Added
query.transform()hook inModel.prototype.deleteOne()to set$isDeleted(true)whendeletedCount > 0 - Added comprehensive tests covering successful deletion, failed deletion, and no-op behavior when already deleted
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/model.js | Adds transform hook to set $isDeleted(true) after successful deleteOne operation |
| test/document.test.js | Adds tests verifying $isDeleted is set correctly after deleteOne, including success and failure cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hasezoey
left a comment
There was a problem hiding this comment.
LGTM, aside from what copilot brought up about the issue id in test name
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Hafez <a.hafez852@gmail.com>
|
Implemented in #16137
Usage: query.post(function(result) {
if (result.deletedCount > 0) {
// Handle successful delete
}
});Supported signatures:
Works with find(), findOne(), updateOne(), deleteOne(), and other query operations. Fully backward compatible. |
…-15898) - Pass result to exec post hooks so query.post(fn) receives result as first param - Supports: function(), function(result), function(result, next) - Fully backward compatible - Add tests for find, findOne, updateOne, deleteOne Made-with: Cursor
…-15898) - Pass result to exec post hooks so query.post(fn) receives result as first param - Supports: function(), function(result), function(result, next) - Fully backward compatible - Add tests for find, findOne, updateOne, deleteOne Made-with: Cursor
Fix #15858
Summary
Using
query.transform()instead ofquery.post()because Mongoose doesn't pass the result toquery.post(), and adding the result toquery.post()would be backwards breaking for cb-based post hooks.Examples