Repository navigation
fix(handles): read a field the entity data does not hold as null - #178
Merged
Merged
Conversation
FieldHandle.value is typed `T | null`, but a field that hasMany.add() did not set read `undefined`, before and after persist: a created entity's snapshot holds only the keys set on it. The snapshot stays sparse, because an absent key is what keeps the column out of the create mutation (so the database default applies); the handle maps the absent key to null. value, serverValue and isDirty share the one reader. Fixes #175 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
Review round 1
Verified: 2 of the 3 new tests fail on Findings
|
Normalizing both sides to null made a field cleared after a create read clean, while the next persist still writes it: the server data of a created entity has no key for an unset field, and the persister diffs raw values. Only value and serverValue map an absent key to null now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
Review response, round 1
Local on b4f4c5c: typecheck clean, eslint on the changed files clean, |
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.
Summary
FieldHandle.valueis typedT | null, but a field thathasMany.add()did not set readundefined, both before and after persist. A created entity's snapshot holds only the keys set on it, andFieldHandlepassed the missing key through.Fixes #175
Fix
FieldHandle.valueandserverValuenow map a key the entity data does not hold tonull.isDirtykeeps comparing the raw values, the same values the persister diffs. After a create, the server data has no key for an unset field, so clearing that field withsetValue(null)is written on the next persist, and the field must report itself dirty. The fallback from the schema path to the read alias still applies only toundefined, so an explicitnullwritten bysetValue(null)is not hidden by a stale aliased value.Why the fix is in the handle and not the store, and why the type stays as it is: the store keeps a created entity's record sparse on purpose.
MutationCollector.buildCreateDatasends only the keys that are present and not null, so an unset column is left to its database default. Filling the snapshot withnullwould putnullinto entity data that the persister, the undo journal and the update diff all read. The read-side mapping changes only what a consumer reads. Changing the type toT | null | undefinedwould make every consumer handle a third state that only means "no value", which is the same thingnullalready means here.Tests
tests/react/relations/hasMany/addUnsetFieldValue.test.tsx(the repro from the issue, extended):valueandserverValuereadnull(onmainboth readundefined;isDirtywas alreadyfalse)isDirtyistrue, and the next persist sends{ color: null }(guards review finding 1)toStrictEqual([{ tags: [{ alias, create: { name: 'New tag' } }] }]), socoloris sent neither asnullnor as anundefinedkey (passes onmainand here)Local:
bun run typecheckclean,eslinton the changed files clean,bun run test2612 pass / 0 fail on b4f4c5c.Out of scope
$dataof the same added entity (assembled byEntityDataAssembler) also lacks the unset key:Object.keys(tag.$data)is['name', 'id']althoughcoloris selected. This PR does not change that; it needs its own decision about alias keys of created entities.After a create persist,
serverValueof an unset field readsnull, even where the database applied a non-null default. The client never reads the created row back. This is no worse thanundefinedonmain; the full fix is to read the created row's selected fields back after the create.Decisions
tests/react/relations/hasMany/addUnsetFieldValue.test.tsx; 2 of 4 tests fail onmainnull, as the type promises. If the right answer is to change the type instead, say why."packages/bindx/src/handles/FieldHandle.ts(readValue)FieldHandle, store record left sparse; reason above.$datanot covered (see Out of scope)nullin the mutation … Test it."is left out of the create mutationFixes #175"Status
main02bea00🤖 Generated with Claude Code