Skip to content

fix: use collectCreateData for nested hasMany entities in mutations#18

Merged
vparys merged 4 commits intomainfrom
fix/nested-hasmany-create-mutation
Apr 13, 2026
Merged

fix: use collectCreateData for nested hasMany entities in mutations#18
vparys merged 4 commits intomainfrom
fix/nested-hasmany-create-mutation

Conversation

@vparys
Copy link
Copy Markdown
Member

@vparys vparys commented Apr 13, 2026

Summary

  • Fixed a bug where nested hasMany entities (3+ levels deep) lost their store-managed relations during mutation collection
  • The old code in collectHasManyOperations manually read snapshot data and called processNestedData, which only handled embedded data — relations added via addToHasMany/getOrCreateRelation were silently dropped
  • Replaced with a call to collectCreateData, which already handles both embedded data and store-managed relations recursively

Test plan

  • Added 3 test cases covering the bug scenario (Program → Approval → Round → Reviews)
  • Test 1: verifies reviews added via store's hasMany are included in nested create mutation
  • Test 2: verifies embedded reviews data still works (regression)
  • Test 3: verifies standalone collectCreateData includes hasMany store state
  • All tests pass (bun test)

vparys added 4 commits April 13, 2026 11:58
Reviews added via store.addToHasMany to a Round nested inside
an Approval create are not included in the mutation because
collectHasManyOperations uses processNestedData instead of
collectCreateData for nested entities.
collectHasManyOperations was using processNestedData for created
entities, which only processed raw snapshot data and ignored
store-tracked hasMany relations. This caused deeply nested
hasMany items (e.g. Round → Reviews) to be missing from mutations
when the parent was itself nested in another create.
…ter persist

- MutationCollector now materializes embedded hasMany/hasOne data into
  proper store entities during collectCreateData, enabling tracking and
  post-persist ID mapping
- Added connectExistingToHasMany to store for connecting existing entities
  without marking them as created
- Extended TransactionMutationResult with nestedResults for adapters to
  return server-assigned IDs for inline-created entities
- BatchPersister now commits nested entities after successful persist and
  processes nestedResults for ID mapping
@vparys vparys merged commit 431b0ce into main Apr 13, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant