Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/solid-id-rendering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/solid-db': patch
---

Fix reconcile usage in useLiveQuery by setting the key field to "$key" so that items are matched correctly during reconciliation. Fixes #1524.
4 changes: 3 additions & 1 deletion packages/solid-db/src/useLiveQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ export function useLiveQuery(
currentCollection: Collection<any, any, any>,
) => {
setData((prev) =>
reconcile(Array.from(currentCollection.values()))(prev).filter(Boolean),
reconcile(Array.from(currentCollection.values()), { key: '$key' })(
prev,
).filter(Boolean),
)
}

Expand Down
54 changes: 54 additions & 0 deletions packages/solid-db/tests/useLiveQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2587,4 +2587,58 @@ describe(`Query Collections`, () => {
expect(rendered.result()).toBeUndefined()
})
})
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<Item>({
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`,
])
})
})
})