From 31933e0c4d0a37b89bb291acf6127fbd83b21fd5 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:09:54 +0300 Subject: [PATCH 1/9] fix(solid-db): use property for reconciliation --- .changeset/solid-id-rendering.md | 6 ++ packages/solid-db/src/useLiveQuery.ts | 2 +- packages/solid-db/tests/useLiveQuery.test.tsx | 64 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 .changeset/solid-id-rendering.md diff --git a/.changeset/solid-id-rendering.md b/.changeset/solid-id-rendering.md new file mode 100644 index 000000000..89a40aa12 --- /dev/null +++ b/.changeset/solid-id-rendering.md @@ -0,0 +1,6 @@ +--- +'@tanstack/solid-db': patch +--- + +Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that that items are matched correctly during reconcilation. Fixes #1524. + diff --git a/packages/solid-db/src/useLiveQuery.ts b/packages/solid-db/src/useLiveQuery.ts index 5380bae00..6a4d43f26 100644 --- a/packages/solid-db/src/useLiveQuery.ts +++ b/packages/solid-db/src/useLiveQuery.ts @@ -346,7 +346,7 @@ export function useLiveQuery( currentCollection: Collection, ) => { setData((prev) => - reconcile(Array.from(currentCollection.values()))(prev).filter(Boolean), + reconcile(Array.from(currentCollection.values()), { key: "$key" })(prev).filter(Boolean), ) } diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 378c2a0fc..98ec97166 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2587,4 +2587,68 @@ describe(`Query Collections`, () => { expect(rendered.result()).toBeUndefined() }) }) +it(`should render correctly when using a custom key field and reordering`, async () => { + type Item = { + _id: string + name: string + } + + const collection = createCollection( + mockSyncCollectionOptions({ + id: `custom-key-reorder-test`, + getKey: (item) => item._id, + initialData: [ + { _id: `bob1`, name: `Bob` }, + { _id: `kevin1`, name: `Kevin` }, + { _id: `stuart1`, name: `Stuart` }, + ], + }), + ) + + function TestComponent() { + const query = useLiveQuery((q) => + q + .from({ items: collection }) + .orderBy(({ items }) => items.name, `asc`), + ) + + return ( +
    + + {(item) =>
  1. {item.name}
  2. } +
    +
+ ) + } + + const { findByTestId } = render(() => ) + + await waitFor(async () => { + const list = await findByTestId(`list`) + expect(list.children).toHaveLength(3) + }) + + let list = await findByTestId(`list`) + expect( + Array.from(list.children).map((el) => el.textContent), + ).toEqual([`Bob`, `Kevin`, `Stuart`]) + + collection.utils.begin() + collection.utils.write({ + type: `update`, + value: { + _id: `stuart1`, + name: `Alvin`, + }, + }) + collection.utils.commit() + + await waitFor(async () => { + list = await findByTestId(`list`) + + expect( + Array.from(list.children).map((el) => el.textContent), + ).toEqual([`Alvin`, `Bob`, `Kevin`]) + }) +}) }) From 5721c019bbc64309380e6ef50d1c27ac8a78debd Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:20:15 +0300 Subject: [PATCH 2/9] Changeset --- .changeset/solid-id-rendering.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/solid-id-rendering.md b/.changeset/solid-id-rendering.md index 89a40aa12..ad39eb5e2 100644 --- a/.changeset/solid-id-rendering.md +++ b/.changeset/solid-id-rendering.md @@ -2,5 +2,5 @@ '@tanstack/solid-db': patch --- -Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that that items are matched correctly during reconcilation. Fixes #1524. +Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that items are matched correctly during reconcilation. Fixes #1524. From b547dda348ffe9f707b06f889a34be7b588dc9ad Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:23:05 +0300 Subject: [PATCH 3/9] Changeset --- .changeset/solid-id-rendering.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/solid-id-rendering.md b/.changeset/solid-id-rendering.md index ad39eb5e2..4c68dfbcc 100644 --- a/.changeset/solid-id-rendering.md +++ b/.changeset/solid-id-rendering.md @@ -2,5 +2,5 @@ '@tanstack/solid-db': patch --- -Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that items are matched correctly during reconcilation. Fixes #1524. +Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that items are matched correctly during reconciliation. Fixes #1524. From d3f8af63c3b32f74c4f380594c1027e7ffa229e9 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:45:15 +0300 Subject: [PATCH 4/9] Add describe block --- packages/solid-db/tests/useLiveQuery.test.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 98ec97166..11567fa25 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2587,6 +2587,7 @@ describe(`Query Collections`, () => { expect(rendered.result()).toBeUndefined() }) }) + describe(`custom id field`, () => { it(`should render correctly when using a custom key field and reordering`, async () => { type Item = { _id: string @@ -2651,4 +2652,5 @@ it(`should render correctly when using a custom key field and reordering`, async ).toEqual([`Alvin`, `Bob`, `Kevin`]) }) }) + }) }) From bc32c8d3e6bafc8bb7e661d4c41191cd5bb01294 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:46:29 +0300 Subject: [PATCH 5/9] Rename --- packages/solid-db/tests/useLiveQuery.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 11567fa25..0d51de06d 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2588,7 +2588,7 @@ describe(`Query Collections`, () => { }) }) describe(`custom id field`, () => { -it(`should render correctly when using a custom key field and reordering`, async () => { +it(`should render correctly when using a custom id field and reordering`, async () => { type Item = { _id: string name: string From 21f021b814805097b8cf26c437fa15f457dec270 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:51:25 +0300 Subject: [PATCH 6/9] Test --- packages/solid-db/tests/useLiveQuery.test.tsx | 29 +++++-------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 0d51de06d..576617908 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2588,7 +2588,7 @@ describe(`Query Collections`, () => { }) }) describe(`custom id field`, () => { -it(`should render correctly when using a custom id field and reordering`, async () => { +it(`should keep all existing items when using a custom id field and reordering`, async () => { type Item = { _id: string name: string @@ -2606,32 +2606,19 @@ it(`should render correctly when using a custom id field and reordering`, async }), ) - function TestComponent() { const query = useLiveQuery((q) => q .from({ items: collection }) .orderBy(({ items }) => items.name, `asc`), ) - return ( -
    - - {(item) =>
  1. {item.name}
  2. } -
    -
- ) - } - const { findByTestId } = render(() => ) + await waitFor(() => { + expect(query.isReady).toBe(true) + }) - await waitFor(async () => { - const list = await findByTestId(`list`) - expect(list.children).toHaveLength(3) - }) - - let list = await findByTestId(`list`) expect( - Array.from(list.children).map((el) => el.textContent), + Array.from(query()).map((item) => item.name), ).toEqual([`Bob`, `Kevin`, `Stuart`]) collection.utils.begin() @@ -2644,11 +2631,9 @@ it(`should render correctly when using a custom id field and reordering`, async }) collection.utils.commit() - await waitFor(async () => { - list = await findByTestId(`list`) - + await waitFor(() => { expect( - Array.from(list.children).map((el) => el.textContent), + Array.from(query()).map((item) => item.name), ).toEqual([`Alvin`, `Bob`, `Kevin`]) }) }) From c1e7c72e50c07996bdf4badbc84f1e3a9db61f8c Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:54:37 +0300 Subject: [PATCH 7/9] Remove waitFor --- packages/solid-db/tests/useLiveQuery.test.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 576617908..a585dd29d 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2612,11 +2612,6 @@ it(`should keep all existing items when using a custom id field and reordering`, .orderBy(({ items }) => items.name, `asc`), ) - - await waitFor(() => { - expect(query.isReady).toBe(true) - }) - expect( Array.from(query()).map((item) => item.name), ).toEqual([`Bob`, `Kevin`, `Stuart`]) @@ -2631,11 +2626,9 @@ it(`should keep all existing items when using a custom id field and reordering`, }) collection.utils.commit() - await waitFor(() => { expect( Array.from(query()).map((item) => item.name), ).toEqual([`Alvin`, `Bob`, `Kevin`]) - }) }) }) }) From 53433d16bc476da7f1d8937d2ece0366b9f46891 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 18:58:55 +0300 Subject: [PATCH 8/9] Review --- packages/solid-db/tests/useLiveQuery.test.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index a585dd29d..810c3c967 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2606,14 +2606,19 @@ it(`should keep all existing items when using a custom id field and reordering`, }), ) - const query = useLiveQuery((q) => + const rendered = renderHook(() => useLiveQuery((q) => q .from({ items: collection }) .orderBy(({ items }) => items.name, `asc`), - ) + ), + ) + + await waitFor(() => { + 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() @@ -2627,7 +2632,7 @@ it(`should keep all existing items when using a custom id field and reordering`, collection.utils.commit() expect( - Array.from(query()).map((item) => item.name), + Array.from(rendered.result()).map((item) => item.name), ).toEqual([`Alvin`, `Bob`, `Kevin`]) }) }) From a04b71aeeba295cf69d74560345a68599ea9a85a Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 18 Jun 2026 19:15:26 +0300 Subject: [PATCH 9/9] Format --- .changeset/solid-id-rendering.md | 1 - packages/solid-db/src/useLiveQuery.ts | 4 +- packages/solid-db/tests/useLiveQuery.test.tsx | 97 ++++++++++--------- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/.changeset/solid-id-rendering.md b/.changeset/solid-id-rendering.md index 4c68dfbcc..d81875bcc 100644 --- a/.changeset/solid-id-rendering.md +++ b/.changeset/solid-id-rendering.md @@ -3,4 +3,3 @@ --- Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that items are matched correctly during reconciliation. Fixes #1524. - diff --git a/packages/solid-db/src/useLiveQuery.ts b/packages/solid-db/src/useLiveQuery.ts index 6a4d43f26..278484109 100644 --- a/packages/solid-db/src/useLiveQuery.ts +++ b/packages/solid-db/src/useLiveQuery.ts @@ -346,7 +346,9 @@ export function useLiveQuery( currentCollection: Collection, ) => { setData((prev) => - reconcile(Array.from(currentCollection.values()), { key: "$key" })(prev).filter(Boolean), + reconcile(Array.from(currentCollection.values()), { key: '$key' })( + prev, + ).filter(Boolean), ) } diff --git a/packages/solid-db/tests/useLiveQuery.test.tsx b/packages/solid-db/tests/useLiveQuery.test.tsx index 810c3c967..5c2198398 100644 --- a/packages/solid-db/tests/useLiveQuery.test.tsx +++ b/packages/solid-db/tests/useLiveQuery.test.tsx @@ -2588,52 +2588,57 @@ describe(`Query Collections`, () => { }) }) describe(`custom id field`, () => { -it(`should keep all existing items when using a custom id field and reordering`, async () => { - type Item = { - _id: string - name: string - } - - const collection = createCollection( - mockSyncCollectionOptions({ - id: `custom-key-reorder-test`, - getKey: (item) => item._id, - initialData: [ - { _id: `bob1`, name: `Bob` }, - { _id: `kevin1`, name: `Kevin` }, - { _id: `stuart1`, name: `Stuart` }, - ], - }), - ) - - const rendered = renderHook(() => useLiveQuery((q) => - q - .from({ items: collection }) - .orderBy(({ items }) => items.name, `asc`), - ), - ) - - await waitFor(() => { - expect(rendered.result.isReady).toBe(true) - }); - - expect( - 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() + it(`should keep all existing items when using a custom id field and reordering`, async () => { + type Item = { + _id: string + name: string + } - expect( - Array.from(rendered.result()).map((item) => item.name), - ).toEqual([`Alvin`, `Bob`, `Kevin`]) -}) + const collection = createCollection( + mockSyncCollectionOptions({ + id: `custom-key-reorder-test`, + getKey: (item) => item._id, + initialData: [ + { _id: `bob1`, name: `Bob` }, + { _id: `kevin1`, name: `Kevin` }, + { _id: `stuart1`, name: `Stuart` }, + ], + }), + ) + + const rendered = renderHook(() => + useLiveQuery((q) => + q + .from({ items: collection }) + .orderBy(({ items }) => items.name, `asc`), + ), + ) + + await waitFor(() => { + expect(rendered.result.isReady).toBe(true) + }) + + expect(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() + + expect(Array.from(rendered.result()).map((item) => item.name)).toEqual([ + `Alvin`, + `Bob`, + `Kevin`, + ]) + }) }) })