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

fix: allow live queries to access optimistic inserts before sync completes
5 changes: 4 additions & 1 deletion packages/db/src/query/live/collection-config-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ export class CollectionConfigBuilder<
private allCollectionsReadyOrInitialCommit() {
return Object.values(this.collections).every(
(collection) =>
collection.status === `ready` || collection.status === `initialCommit`
collection.status === `ready` ||
collection.status === `initialCommit` ||
// Allow graph to run if collection has data (optimistic inserts)
collection.size > 0
)
}

Expand Down
56 changes: 56 additions & 0 deletions packages/db/tests/query/live-query-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,62 @@ describe(`createLiveQueryCollection`, () => {
expect(liveQuery.isReady()).toBe(false)
})

it(`should include optimistic inserts in live query before source collection sync completes`, async () => {
// Create a collection that doesn't sync initially (idle state)
const sourceCollection = createCollection(
mockSyncCollectionOptionsNoInitialState<User>({
id: `collection-with-optimistic-data`,
getKey: (user) => user.id,
})
)

expect(sourceCollection.status).toBe(`idle`)

// Insert data optimistically BEFORE creating the live query
sourceCollection.insert({ id: 1, name: `Alice`, active: true })
sourceCollection.insert({ id: 3, name: `Charlie`, active: false })
expect(sourceCollection.size).toBe(2)

// Create a live query from the collection with optimistic data
// This will trigger sync on the source collection (idle → loading)
const activeUsers = createLiveQueryCollection({
query: (q) =>
q
.from({ user: sourceCollection })
.where(({ user }) => eq(user.active, true)),
startSync: true,
})

// The live query should see the optimistic data even though the source
// collection's sync hasn't completed yet (still in 'loading' state)
expect(activeUsers.size).toBe(1)
expect(Array.from(activeUsers.values())).toMatchObject([
{ id: 1, name: `Alice`, active: true },
])
expect(sourceCollection.status).toBe(`loading`)

// Now simulate the source collection's sync completing
const utils = (sourceCollection as any).config.utils
utils.begin()
utils.write({
type: `insert`,
value: { id: 1, name: `Alice`, active: true },
})
utils.write({
type: `insert`,
value: { id: 3, name: `Charlie`, active: false },
})
utils.commit()
utils.markReady()

// After source sync completes, data should still be correct
expect(sourceCollection.status).toBe(`ready`)
expect(activeUsers.size).toBe(1)
expect(Array.from(activeUsers.values())).toMatchObject([
{ id: 1, name: `Alice`, active: true },
])
})

it(`should update after source collection is loaded even when not preloaded before rendering`, async () => {
// Create a source collection that doesn't start sync immediately
let beginCallback: (() => void) | undefined
Expand Down