Skip to content
Closed
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/fix-join-on-demand-sync.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions packages/db/src/query/live/collection-config-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions packages/db/src/query/live/collection-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading