Skip to content
Source
Sign inSign up
TanStack/db

fix(db): materialize includes into parent rows before commit #1684

Closedjuliuslipp(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1684/headopened

🎯 Changes

Fixes #1635.

Live query includes are materialized into parent rows only after the parent collection commits: flushIncludesState patches the stored row in place (setIncludedValue) without emitting an event, and the follow-up re-emit (inlineReEmitKeys) only fires for correlation keys with pending child changes.

Any consumer that copies rows at emit time never observes that patch. The most common shape is another live query layered on the collection — which is what every framework adapter produces for component-level queries (useLiveQuery((q) => q.from({ row: myLiveQuery }))). For those consumers, a parent-only update (e.g. a status column change synced from the server, no child rows touched) re-emits the parent row with every included field unset, and since no child change follows, they keep the empty includes until an unrelated child change recomputes the row. This is the "nested includes are undefined on next render after collection.update, a forced re-render brings them back" behavior reported in #1635.

The fix: materialize the current includes state into the new parent change values before begin()/commit(), so the emitted events already carry the included values. The post-commit flush then re-applies the same values idempotently (it still owns child-collection bookkeeping and registry maintenance), and the child-change re-emit path is unchanged. Only inline materializations (array/singleton/concat) are patched pre-commit; collection materialization still attaches in the post-commit flush.

New regression test: includes subqueries > parent-only updates > keeps materialized includes on rows observed through a layered live query — fails on main, passes with this change.

✅ Checklist

  • I have tested this code locally with pnpm test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Materialize behavior so included child fields are populated on parent rows before change events are emitted.
    • Ensured included values remain correct after parent-only updates, including when used in layered live queries.
    • Prevented included fields from temporarily appearing empty until a subsequent child change occurs.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 4c03308a-8d39-481b-87c0-bedd0db3d783

      📥 Commits

      Reviewing files that changed from the base of the PR and between a9761370040a3fb0e75e724539922db918259726 and c8d03e445863bc355ef08d2e5a4bf657424f6c1e.

      📒 Files selected for processing (2)
      • packages/db/src/query/live/collection-config-builder.ts
      • packages/db/tests/query/includes.test.ts
      🚧 Files skipped from review as they are similar to previous changes (2)
      • packages/db/tests/query/includes.test.ts
      • packages/db/src/query/live/collection-config-builder.ts

      📝 Walkthrough

      Walkthrough

      The live query flush now materializes inline includes into parent changes before commit and emission. A regression test verifies layered queries retain included child rows after parent-only updates, and the changeset documents the fix for issue 1635.

      Changes

      Live query include materialization

      Layer / File(s)Summary
      Pre-commit parent include patching
      packages/db/src/query/live/collection-config-builder.ts, .changeset/heavy-pandas-tell.md
      Pending parent inserts are patched with inline materialized include values before transaction commit; the changeset records the patch release and behavior fix.
      Parent-only update regression test
      packages/db/tests/query/includes.test.ts
      A layered live query test verifies materialized child includes remain populated after updating only the parent row.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Possibly related issues

      • TanStack/db#1571 — Both changes update include propagation so parent rows receive current included values before emitted changes are observed.
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title is concise and clearly describes the main change: materializing includes into parent rows before commit.
      Description check✅ PassedThe description follows the template and includes the change summary, checklist, and release-impact notes.
      Linked Issues check✅ PassedThe PR addresses #1635 by fixing nested includes after parent-only updates and adding a regression test.
      Out of Scope Changes check✅ PassedThe changes stay focused on the bug fix, regression test, and required changeset.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. Actionable comments posted: 2

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/db/src/query/live/collection-config-builder.ts`:
      - Around line 1257-1260: Update patchParentChangesFromIncludes to use a generic
      result type and type parentChanges as Map<unknown, Changes<TResult>> instead of
      any. Preserve the caller’s Changes<TResult> type through the function contract,
      using narrowly scoped internal casts only where symbol-based indexing or
      setIncludedValue requires them.
      
      In `@packages/db/tests/query/includes.test.ts`:
      - Around line 755-757: In the test flow around layered.get(1), replace the fixed
      10ms timeout with a deterministic synchronization mechanism, such as awaiting
      the collection’s change event or using the project’s retry-polling helper, so
      the assertion runs only after the async update completes. Replace the any cast
      on renamed with the appropriate concrete type inferred from the collection or
      existing test types.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 6ec1e02b-c316-4ea5-9673-dc1fae95c75e

      📥 Commits

      Reviewing files that changed from the base of the PR and between 7f0fa36ff6d48b0494ed5f6a1223fd18c317e41f and a9761370040a3fb0e75e724539922db918259726.

      📒 Files selected for processing (3)
      • .changeset/heavy-pandas-tell.md
      • packages/db/src/query/live/collection-config-builder.ts
      • packages/db/tests/query/includes.test.ts
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
    3. juliuslipp(GitHub profile, opens in a new tab)replied on packages/db/src/query/live/collection-config-builder.ts lines 1257 to 1260

      Fixed in c8d03e4 — the function is now generic over the caller's Changes<TResult> (<TResult extends object>, matching the class constraint), with a single narrow cast to Record<string | symbol, any> for the symbol-indexed INCLUDES_ROUTING access and setIncludedValue compatibility.

    4. juliuslipp(GitHub profile, opens in a new tab)replied on packages/db/tests/query/includes.test.ts lines 755 to 757

      Fixed in c8d03e4 — the fixed 10ms sleep is replaced with vi.waitFor polling for the propagated rename, and the as any casts are gone: the layered live query's inferred row type already carries name/issues, so plain non-null narrowing suffices.

    5. coderabbitai[bot](GitHub profile, opens in a new tab)replied on packages/db/src/query/live/collection-config-builder.ts lines 1257 to 1260

      @juliuslipp, thanks for fixing this. The generic constraint preserves the caller’s result type, while keeping the symbol-indexed access confined to a narrow internal cast.


      If you found this review helpful, would you consider giving us a shout-out on X?

      Thank you for using CodeRabbit!

    6. coderabbitai[bot](GitHub profile, opens in a new tab)replied on packages/db/tests/query/includes.test.ts lines 755 to 757

      @juliuslipp, thanks for addressing this. Using vi.waitFor makes the propagation assertion deterministic, and relying on the inferred row type removes the unnecessary any casts.


      If you found this review helpful, would you consider giving us a shout-out on X?

      Thank you for using CodeRabbit!

Sign in to join this conversation.