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

Fixed local collection manual transactions
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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KyleAMathews ok updated with the uuid approach. As far as I can tell, the id automatically generated by the base collection impl is not available in time for use in the collection filtering below, so I had to generate it here.


// 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