From 62b25900b09991f757d65f62a2f87d100611f969 Mon Sep 17 00:00:00 2001 From: Marc MacLeod Date: Sat, 4 Oct 2025 19:17:19 -0500 Subject: [PATCH 1/2] fix: allow live queries to access optimistic inserts before sync completes Signed-off-by: Marc MacLeod --- .../query/live/collection-config-builder.ts | 5 +- .../tests/query/live-query-collection.test.ts | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/db/src/query/live/collection-config-builder.ts b/packages/db/src/query/live/collection-config-builder.ts index 4572b345a..586c2b0d9 100644 --- a/packages/db/src/query/live/collection-config-builder.ts +++ b/packages/db/src/query/live/collection-config-builder.ts @@ -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 ) } diff --git a/packages/db/tests/query/live-query-collection.test.ts b/packages/db/tests/query/live-query-collection.test.ts index 23b6e3d62..1181794ee 100644 --- a/packages/db/tests/query/live-query-collection.test.ts +++ b/packages/db/tests/query/live-query-collection.test.ts @@ -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({ + 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 From 294de56ecb32588dba502aa4d44e31d0e797247f Mon Sep 17 00:00:00 2001 From: Marc MacLeod Date: Sat, 4 Oct 2025 19:24:30 -0500 Subject: [PATCH 2/2] chore: add changeset Signed-off-by: Marc MacLeod --- .changeset/slow-lamps-follow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/slow-lamps-follow.md diff --git a/.changeset/slow-lamps-follow.md b/.changeset/slow-lamps-follow.md new file mode 100644 index 000000000..3e720db47 --- /dev/null +++ b/.changeset/slow-lamps-follow.md @@ -0,0 +1,5 @@ +--- +"@tanstack/db": patch +--- + +fix: allow live queries to access optimistic inserts before sync completes