Skip to content
Source
Sign inSign up
TanStack/db

fix(solid-db): set key option to $key when calling reconcile #1598

ClosedLeonabcd123(GitHub profile, opens in a new tab)wants to merge 9 commits intomain fromrefs/pull/1598/headopened

Fixes #1524.

🎯 Changes

Add key option when calling reconcile which is set to $key, which makes it so items are matched correctly even when the id property doesn't exist.

✅ 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 live query reconciliation to match list items using the correct key field, preventing incorrect item reuse during updates and reordering.
  • Tests

    • Added coverage for custom item keys, confirming that sorted results update to the expected order after an item changes.
    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 Plus

      Run ID: 5ee98229-f10f-4426-a8b3-96745f1054fc

      📥 Commits

      Reviewing files that changed from the base of the PR and between bc32c8d3e6bafc8bb7e661d4c41191cd5bb01294 and a04b71aeeba295cf69d74560345a68599ea9a85a.

      📒 Files selected for processing (3)
      • .changeset/solid-id-rendering.md
      • packages/solid-db/src/useLiveQuery.ts
      • packages/solid-db/tests/useLiveQuery.test.tsx
      💤 Files with no reviewable changes (1)
      • .changeset/solid-id-rendering.md
      🚧 Files skipped from review as they are similar to previous changes (2)
      • packages/solid-db/src/useLiveQuery.ts
      • packages/solid-db/tests/useLiveQuery.test.tsx

      📝 Walkthrough

      Walkthrough

      syncDataFromCollection now passes { key: "$key" } to reconcile. A regression test verifies item preservation and reordering for a collection keyed by _id. A patch changeset records the fix.

      Changes

      useLiveQuery reconciliation key fix

      Layer / File(s)Summary
      Reconciliation key and regression coverage
      packages/solid-db/src/useLiveQuery.ts, packages/solid-db/tests/useLiveQuery.test.tsx, .changeset/solid-id-rendering.md
      syncDataFromCollection now passes { key: "$key" } to reconcile. The test verifies that an _id-keyed collection preserves existing items and reorders results after an item name changes. The changeset records a patch release.

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

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 50.00% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly identifies the Solid DB fix and the specific reconcile key option change.
      Description check✅ PassedThe description explains the fix, records local testing, and confirms the required changeset for published code.
      Linked Issues check✅ PassedThe implementation and regression test address issue #1524 by reconciling items with custom keys such as _id.
      Out of Scope Changes check✅ PassedThe source fix, regression test, and changeset are directly related to issue #1524 and the stated pull request objectives.
      ✨ 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: 1

      🤖 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 @.changeset/solid-id-rendering.md:
      - Line 5: The release note in the changeset file contains two typos that need
      correction on line 5: the phrase "that that" appears as a duplicate word and
      should be simplified to "that", and the word "reconcilation" is misspelled and
      should be corrected to "reconciliation". Update the text to fix both instances
      so the release note reads clearly and professionally.
      🪄 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: e433e653-ee95-4104-8db2-eb84d69e6a44

      📥 Commits

      Reviewing files that changed from the base of the PR and between 00389a47b258ad58fc3a03c5cc6f66957b9bd2d1 and 31933e0c4d0a37b89bb291acf6127fbd83b21fd5.

      📒 Files selected for processing (3)
      • .changeset/solid-id-rendering.md
      • packages/solid-db/src/useLiveQuery.ts
      • packages/solid-db/tests/useLiveQuery.test.tsx
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
    3. Leonabcd123(GitHub profile, opens in a new tab)replied on .changeset/solid-id-rendering.md line 5

      Fixed in 5721c019bbc64309380e6ef50d1c27ac8a78debd and b547dda348ffe9f707b06f889a34be7b588dc9ad.

    4. coderabbitai[bot](GitHub profile, opens in a new tab)replied on .changeset/solid-id-rendering.md line 5

      @Leonabcd123, thanks for fixing the typos! The changeset description now reads correctly. 🐇


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

      Thank you for using CodeRabbit!

    5. Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (1)
      packages/solid-db/tests/useLiveQuery.test.tsx (1)

      2609-2639: ⚠️ Potential issue | 🟠 Major

      Wrap useLiveQuery in a managed reactive root to avoid leaked subscriptions.

      This test directly instantiates useLiveQuery without a reactive owner context. In Solid.js, effects and signals created by useLiveQuery must be disposed properly to prevent subscription leakage between tests. Use renderHook (the established pattern throughout this file) or createRoot with cleanup to ensure proper lifecycle management.

      Suggested fix
      -  const query = useLiveQuery((q) =>
      -    q
      -      .from({ items: collection })
      -      .orderBy(({ items }) => items.name, `asc`),
      -  )
      +  const rendered = renderHook(() =>
      +    useLiveQuery((q) =>
      +      q
      +        .from({ items: collection })
      +        .orderBy(({ items }) => items.name, `asc`),
      +    ),
      +  )
      
         await waitFor(() => {
      -    expect(query.isReady).toBe(true)
      +    expect(rendered.result.isReady).toBe(true)
         })
      
         expect(
      -    Array.from(query()).map((item) => item.name),
      +    Array.from(rendered.result()).map((item) => item.name),
         ).toEqual([`Bob`, `Kevin`, `Stuart`])
      
         collection.utils.begin()
         collection.utils.write({
           type: `update`,
           value: {
             _id: `stuart1`,
             name: `Alvin`,
           },
         })
         collection.utils.commit()
      
         await waitFor(() => {
           expect(
      -      Array.from(query()).map((item) => item.name),
      +      Array.from(rendered.result()).map((item) => item.name),
           ).toEqual([`Alvin`, `Bob`, `Kevin`])
         })
      🤖 Prompt for 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.
      
      In `@packages/solid-db/tests/useLiveQuery.test.tsx` around lines 2609 - 2639, The
      test is directly calling useLiveQuery without a managed reactive root context,
      which can leak subscriptions between tests in Solid.js. Wrap the useLiveQuery
      call and all subsequent query operations in either renderHook (the established
      pattern used elsewhere in this test file) or createRoot with proper cleanup to
      ensure effects and signals are disposed correctly. This ensures the reactive
      context is properly initialized and cleaned up after the test completes,
      preventing subscription leakage.

      Source: Coding guidelines

      🤖 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.
      
      Outside diff comments:
      In `@packages/solid-db/tests/useLiveQuery.test.tsx`:
      - Around line 2609-2639: The test is directly calling useLiveQuery without a
      managed reactive root context, which can leak subscriptions between tests in
      Solid.js. Wrap the useLiveQuery call and all subsequent query operations in
      either renderHook (the established pattern used elsewhere in this test file) or
      createRoot with proper cleanup to ensure effects and signals are disposed
      correctly. This ensures the reactive context is properly initialized and cleaned
      up after the test completes, preventing subscription leakage.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: c00179c5-f4e2-4a12-8731-4007dfcd934c

      📥 Commits

      Reviewing files that changed from the base of the PR and between bc32c8d3e6bafc8bb7e661d4c41191cd5bb01294 and 21f021b814805097b8cf26c437fa15f457dec270.

      📒 Files selected for processing (1)
      • packages/solid-db/tests/useLiveQuery.test.tsx
    6. ✅ Action performed

      Reviews resumed.

Sign in to join this conversation.