Skip to content
Merged
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/metal-results-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/db': patch
---

Fixed `acceptMutations` not persisting data in local-only collections with manual transactions. The mutation filter was comparing against a stale `null` collection reference instead of using the collection ID, causing all mutations to be silently dropped after the transaction's `mutationFn` resolved.
10 changes: 6 additions & 4 deletions packages/db/src/local-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ export function localOnlyCollectionOptions<
): LocalOnlyCollectionOptionsResult<T, TKey, TSchema> & {
schema?: StandardSchemaV1
} {
const { initialData, onInsert, onUpdate, onDelete, ...restConfig } = config
const { initialData, onInsert, onUpdate, onDelete, id, ...restConfig } =
config

const collectionId = id ?? crypto.randomUUID()

// Create the sync configuration with transaction confirmation capability
const syncResult = createLocalOnlySync<T, TKey>(initialData)
Expand Down Expand Up @@ -247,9 +250,7 @@ export function localOnlyCollectionOptions<
}) => {
// Filter mutations that belong to this collection
const collectionMutations = transaction.mutations.filter(
(m) =>
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
m.collection === syncResult.collection,
(m) => m.collection.id === collectionId,
)

if (collectionMutations.length === 0) {
Expand All @@ -264,6 +265,7 @@ export function localOnlyCollectionOptions<

return {
...restConfig,
id: collectionId,
sync: syncResult.sync,
onInsert: wrappedOnInsert,
onUpdate: wrappedOnUpdate,
Expand Down
35 changes: 34 additions & 1 deletion packages/db/tests/local-only.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ describe(`LocalOnly Collection`, () => {
})

describe(`Manual transactions with acceptMutations`, () => {
it(`should accept and persist mutations from manual transactions`, () => {
it(`should accept and persist mutations from manual transactions`, async () => {
const tx = createTransaction({
mutationFn: async ({ transaction }: any) => {
// Simulate API call success
Expand Down Expand Up @@ -510,6 +510,39 @@ describe(`LocalOnly Collection`, () => {
id: 101,
name: `Manual Tx Insert 2`,
})

// Verify that the item is still present after async operations complete
await new Promise((resolve) => setTimeout(resolve, 1))
expect(collection.get(100)).toEqual({ id: 100, name: `Manual Tx Insert` })
})

it(`should work without explicit collection ID`, async () => {
// Create a collection without an explicit ID
const noIdCollection = createCollection<
TestItem,
number,
LocalOnlyCollectionUtils
>(
localOnlyCollectionOptions({
getKey: (item) => item.id,
}),
)

const tx = createTransaction({
mutationFn: async ({ transaction }: any) => {
noIdCollection.utils.acceptMutations(transaction)
},
autoCommit: false,
})

tx.mutate(() => {
noIdCollection.insert({ id: 999, name: `No ID Test` })
})

await tx.commit()

// Data should persist even without explicit ID
expect(noIdCollection.get(999)).toEqual({ id: 999, name: `No ID Test` })
})

it(`should only accept mutations for the specific collection`, () => {
Expand Down
Loading