From 2c3e9b1d53b03b4c380a856cdca6f2f272f09173 Mon Sep 17 00:00:00 2001 From: Igor Barakaiev Date: Mon, 29 Dec 2025 11:04:06 -0800 Subject: [PATCH 1/4] fix: ensure sync is started for on-demand collections in join queries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using join queries with collections configured with `syncMode: 'on-demand'`, the source collections were never having their sync initialized. This caused join queries to return empty results because: 1. On-demand collections use `offset: 'now'` and only receive changes from when sync starts, not historical data 2. The lazy join loader calls `requestSnapshot()` to fetch matching rows 3. Without `startSyncImmediate()`, the collection stays idle and `requestSnapshot()` has nothing to work with The fix adds a `collection.startSyncImmediate()` call in `subscribeToAllCollections()` before subscribing, ensuring all source collections in a query (including those used in joins) have their sync properly initialized. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- packages/db/src/query/live/collection-config-builder.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/db/src/query/live/collection-config-builder.ts b/packages/db/src/query/live/collection-config-builder.ts index b663fdad5..1706be42c 100644 --- a/packages/db/src/query/live/collection-config-builder.ts +++ b/packages/db/src/query/live/collection-config-builder.ts @@ -866,6 +866,11 @@ export class CollectionConfigBuilder< }) syncState.unsubscribeCallbacks.add(statusUnsubscribe) + // Ensure sync is started for all source collections in the query. + // This is critical for on-demand sync collections used in joins - without this, + // they remain idle and requestSnapshot() calls from lazy join loading won't work. + collection.startSyncImmediate() + const subscription = collectionSubscriber.subscribe() // Store subscription by alias (not collection ID) to support lazy loading // which needs to look up subscriptions by their query alias From 77467641902d2dbbbbf54a28d312a3ab0aef8349 Mon Sep 17 00:00:00 2001 From: Igor Barakaiev Date: Mon, 29 Dec 2025 11:31:53 -0800 Subject: [PATCH 2/4] chore: add changeset for on-demand sync fix --- .changeset/fix-join-on-demand-sync.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-join-on-demand-sync.md diff --git a/.changeset/fix-join-on-demand-sync.md b/.changeset/fix-join-on-demand-sync.md new file mode 100644 index 000000000..d3d3d853b --- /dev/null +++ b/.changeset/fix-join-on-demand-sync.md @@ -0,0 +1,5 @@ +--- +"@tanstack/db": patch +--- + +Fix on-demand sync collections not loading data when used in join queries. Previously, collections with `syncMode: 'on-demand'` would remain idle when used as sources in join queries, causing empty results. Now `startSyncImmediate()` is called on all source collections in `subscribeToAllCollections()` to ensure sync is properly initialized. From 2ed70ce4264687a125aed1f1369529210ce4dc51 Mon Sep 17 00:00:00 2001 From: Igor Barakaiev Date: Mon, 29 Dec 2025 14:17:44 -0800 Subject: [PATCH 3/4] fix: trigger requestSnapshot for all on-demand sync collection aliases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using on-demand sync collections (e.g., Electric with offset: 'now'), join queries were not receiving initial data for "lazy" aliases. The issue was that subscribeToMatchingChanges() only called requestSnapshot() when includeInitialState was true. But for lazy aliases in joins, includeInitialState is false (to optimize data loading). However, on-demand collections still need requestSnapshot() to trigger the sync layer. This fix always calls requestSnapshot({}) in subscribeToMatchingChanges(), ensuring all on-demand collection aliases properly load their initial state. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- packages/db/src/query/live/collection-subscriber.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/db/src/query/live/collection-subscriber.ts b/packages/db/src/query/live/collection-subscriber.ts index 303c833fc..c56e610c2 100644 --- a/packages/db/src/query/live/collection-subscriber.ts +++ b/packages/db/src/query/live/collection-subscriber.ts @@ -197,6 +197,12 @@ export class CollectionSubscriber< whereExpression, }) + // Trigger initial data load for on-demand sync collections. + // This is called regardless of includeInitialState because on-demand collections + // need to explicitly request data loading. For lazy aliases in joins, + // includeInitialState is false but we still need to trigger the sync layer. + subscription.requestSnapshot({}) + return subscription } From c28794942e42bce16b9ad2c13acb1ccdee6f8d42 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 29 Dec 2025 22:35:58 +0000 Subject: [PATCH 4/4] ci: apply automated fixes --- .changeset/fix-join-on-demand-sync.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fix-join-on-demand-sync.md b/.changeset/fix-join-on-demand-sync.md index d3d3d853b..163062d62 100644 --- a/.changeset/fix-join-on-demand-sync.md +++ b/.changeset/fix-join-on-demand-sync.md @@ -1,5 +1,5 @@ --- -"@tanstack/db": patch +'@tanstack/db': patch --- Fix on-demand sync collections not loading data when used in join queries. Previously, collections with `syncMode: 'on-demand'` would remain idle when used as sources in join queries, causing empty results. Now `startSyncImmediate()` is called on all source collections in `subscribeToAllCollections()` to ensure sync is properly initialized.