fix(db): require exact Collection.update keys - #1851
KyleAMathews wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review. 📝 WalkthroughWalkthrough
ChangesCollection update key type enforcement
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~15 minutes Change: Bug fix 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/react-router-with-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 165 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 7.34 kB ℹ️ View Unchanged
|
Summary
Collection.updatenow requires every key to match the collection's declaredTKeyacross single, bulk, config, and no-config overloads. Wrong key domains and erased branded keys are rejected at compile time instead of compiling and then missing the exact runtime lookup; runtime behavior is unchanged.Root Cause
The public overloads and matching internal signatures used
TKey | unknown. A union withunknowncollapses tounknown, so a numeric-key collection accepted calls such ascollection.update('1', ...)even thoughget,delete, mutation-handler keys, and the runtime state lookup all preserve the exact key type.Approach
TKey | unknowntoTKey, including their array forms.TKey | Array<TKey>through the implementation and mutation manager without changing execution.get,delete, callback draft types, and handler mutation keys as independent controls.undefinedis no longer admitted accidentally.@tanstack/db.Key Invariants
TKey.WritableDeep<TInput>and overload selection remains unchanged.createOptimisticActionhave the same key contract as direct calls.Non-goals
onMutatetyping.Trade-offs
This is a public type narrowing, so code that previously passed a sibling key domain or plain string in place of a branded key will now fail compilation. Those calls could not address the intended row under the existing exact runtime lookup, so the narrow correction aligns
updatewith the rest of the collection API without adding runtime complexity.Consumers passing possibly undefined keys (including unchecked indexed access) or plain strings to branded-key collections must narrow or assert those values to the declared key type before calling
update.Verification
git diff --checkpassed.TKey | unknownmade all 18 wrong-update controls fail as unused@ts-expect-errordirectives.string | numberwas also killed by the numeric, string, and branded-key controls.Files changed
packages/db/src/collection/index.ts: narrows the four public update overloads and implementation key parameter.packages/db/src/collection/mutations.ts: narrows the matching internal manager key parameter.packages/db/tests/collection-update-key-types.test-d.ts: adds the exact-key contract oracle and hostile-widening controls.packages/db/tests/collection.test.ts: marks known bulk fixture positions as present for the exact key signature..changeset/fix-collection-update-key-types.md: records the@tanstack/dbminor changeset.Summary by CodeRabbit
Bug Fixes
Tests