fix: do not re-apply the query for per-item patchMany/removeMany calls - #48
Merged
Merged
Conversation
The query of `patchMany`/`removeMany` selects *which* items are affected and is consumed by the `find` of the per-item fallback. Re-applying it to every `patch(id, ...)`/`remove(id, ...)` call was redundant at best: adapters use `params.query` as an additional filter, so a stale item throws `NotFound` and takes the whole `Promise.all` down with it - after partially written changes. - the per-item calls now only get `$select` from the query - the fallback `find` replaces `$select` with the id property, since it is only there to collect the ids Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
patchMany/removeManyfall back to one call per item when the service does not allow multi. In that fallback,params.querywas forwarded to everypatch(id, ...)/remove(id, ...)call (minus$limit/$skip/$sort).But the query selects which items are affected and is already consumed by the
find. Re-applying it per item is redundant at best - and harmful at worst: adapters applyparams.queryas an additional filter (_get/findOneAndUpdatewith{ ...query, [id]: id }), so an item that no longer matches betweenfindandpatchthrowsNotFoundand takes the wholePromise.alldown with it, after changes have partially been written. Custom query operators that onlyfindhooks resolve run into the same wall. That$limit/$skip/$sorthad to be filtered out by hand was the symptom.Authorization is not an argument for keeping it: hook-based restrictions (e.g.
limitToOwner) set their query again in thepatch/removehooks anyway, and a caller-supplied query was already enforced by thefind.Change
src/common/change-many.ts- blacklist → whitelist:$selectfrom the query, so both paths (multi vs. per-item) return the same shape.provider,user,authenticationetc. are untouched.findreplaces$selectwith the id property - it only exists to collect the ids, so it no longer over-fetches and the id is always present.$sort/$limit/$skipstill shape thefind.Tests
Three new tests each for
patchManyandremoveMany:$sort/$limit/$skipstill shape thefind, incl. ordering$selectreaches the per-item calls, while thefindonly selects the idThe previous
does not forward selection filters to the single callstest asserted the old behaviour and is replaced.pnpm testgreen: 1255 tests, no lint errors, no type errors,change-many.tsat 100% lines.🤖 Generated with Claude Code