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/release-deleted-sync-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/db': patch
---

Release synced key tracking when rows are deleted.
1 change: 1 addition & 0 deletions packages/db/src/collection/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ export class CollectionStateManager<
break
}
case `delete`:
this.syncedKeys.delete(key)
this.syncedData.delete(key)
this.syncedMetadata.delete(key)
// Clean up origin and pending tracking for deleted rows
Expand Down
34 changes: 34 additions & 0 deletions packages/db/tests/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,40 @@ describe(`Collection`, () => {
})
})

it(`should remove deleted keys from synced key tracking`, async () => {
const collection = createCollection(
mockSyncCollectionOptionsNoInitialState<{
id: number
value: string
}>({
id: `sync-key-delete-test`,
getKey: (item) => item.id,
startSync: true,
}),
)

collection.utils.begin()
collection.utils.write({
type: `insert`,
value: { id: 1, value: `one` },
})
collection.utils.write({
type: `insert`,
value: { id: 2, value: `two` },
})
collection.utils.commit()
collection.utils.markReady()

await collection.stateWhenReady()
expect(collection._state.syncedKeys).toEqual(new Set([1, 2]))

collection.utils.begin()
collection.utils.write({ type: `delete`, key: 1 })
collection.utils.commit()

expect(collection._state.syncedKeys).toEqual(new Set([2]))
})

it(`should delete row metadata when sync deletes the row`, async () => {
let testSyncFunctions: any = null

Expand Down