You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ORM: relation where re-resolves a non-_id relation target once per drain #446
PR #420 fixed the main case — this is a narrower follow-up, not a duplicate.
#420 added a per-execution _documentByNormalizedId memo inside _getById, so relation targets resolved by db.get are now read once per query execution instead of once per (parent, drain). Verified: the original shape issued 200 db.get calls at the audit commit and issues 2 on current main.
What survives: a relation where referencing a one relation joined on anything other than _id still re-resolves its target once per drain.
Root cause
packages/kitcn/src/orm/query.ts:8362 — _loadOneRelation sets useGetById only when the join is on the primary id (:8409):
with no cross-drain memo. The chunk drain at :9221 re-enters it for every 32-row batch, so the same target is re-read once per batch.
So r.one.x({ to: x.id }) — the common shape — is covered by #420's memo; r.one.x({ to: x.someOtherColumn }) is not.
Not affected by the other merges
#425, #426 and #427 do not touch this path. #427 explicitly excludes one relations from its existence-probe lowering — its own note: "A one relation is a single row either way, so there is nothing to stop early at."
Suggested fix
Give _loadOneRelation's non-id branch the same execution-scoped memo, keyed on (targetTable, targetFields, values) rather than on a normalized id.
The staleness argument #420 already wrote for _documentByNormalizedId applies verbatim — same execution scope, same invalidation reasoning.
Small.
Existing tests
packages/kitcn/src/orm/query.relation-where-reads.vitest.ts:110 and :132 (added by #420) assert counts.get <= 2 over a 50-row scan with 2 distinct targets — those pin the _getById memo. The non-_id branch has no equivalent; extend that file rather than starting a new one.
Environment
kitcn 0.32.1
convex 1.44.0
verified and measured against origin/main at 14bab503
Activity
fix(orm): memoize non-_id relation target reads per execution (udecod…