Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upsert return values properly destructured #319

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ CollectionHooks.defineWrapper('upsert', async function (userId, _super, instance

if (async) {
const wrappedCallback = async function (err, ret) {
if (err || (ret && ret.insertedId)) {
const { insertedId, numberAffected } = (ret ?? {})
if (err || insertedId) {
// Send any errors to afterInsert
await afterInsert(ret.insertedId, err)
await afterInsert(insertedId, err)
} else {
await afterUpdate(ret && ret.numberAffected, err) // Note that err can never reach here
await afterUpdate(numberAffected, err) // Note that err can never reach here
}

return CollectionHooks.hookedOp(function () {
Expand All @@ -97,11 +98,12 @@ CollectionHooks.defineWrapper('upsert', async function (userId, _super, instance
return CollectionHooks.directOp(() => _super.call(this, selector, mutator, options, wrappedCallback))
} else {
const ret = await CollectionHooks.directOp(() => _super.call(this, selector, mutator, options, callback))
const { insertedId, numberAffected } = (ret ?? {})

if (ret && ret.insertedId) {
await afterInsert(ret.insertedId)
if (insertedId) {
await afterInsert(insertedId)
} else {
await afterUpdate(ret && ret.numberAffected)
await afterUpdate(numberAffected)
}

return ret
Expand Down