-
Notifications
You must be signed in to change notification settings - Fork 102
Handle pushed down predicates in query collection #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 65c076f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Size Change: 0 B Total Size: 83.6 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 2.36 kB ℹ️ View Unchanged
|
aa2e623
to
2ae236f
Compare
074868c
to
80b7b6d
Compare
91ea051
to
3493f6d
Compare
868b9d5
to
8c72581
Compare
…ubclass the same event emiiter implimetation
…an unsunbscribed event to it
… event to the subscription, fix types
07f5a6f
to
652af67
Compare
652af67
to
58d7af8
Compare
Co-authored-by: Kevin De Porre <[email protected]> Co-authored-by: Sam Willis <[email protected]>
58d7af8
to
3bef58d
Compare
Closing this PR and reopening with a rebased branch to remove duplicate commits from #669 that are already in main. New PR incoming... |
Stacked on: #669
Overview
This PR extends Query Collections to support predicate pushdown from live queries by enabling multiple concurrent queries with different predicates/filters. When live queries push down predicates via
loadSubset
, Query Collections now create separate TanStack Query instances for each unique set of options, pass those options to both the query key builder and query function, and manage the lifecycle of multiple queries with proper reference counting and garbage collection.Problem
When live queries push down predicates (via
loadSubset
), Query Collections need to:LoadSubsetOptions
(predicates, limits, ordering) to the query key builder and query functionWithout this, Query Collections couldn't properly support the on-demand sync mode introduced in #669.
Solution
This PR implements a comprehensive multi-query management system that flows predicates from live queries through to your TanStack Query implementation:
Predicate Flow
When a live query calls
loadSubset
with predicates, those options flow through the system:collection._sync.loadSubset(options)
createQueryFromOpts(options)
options
to create unique query keyoptions
viacontext.meta.loadSubsetOptions
Example Usage
In this example:
queryKey
function builds different cache keys based on page/filtersqueryFn
receives the same options viactx.meta.loadSubsetOptions
to fetch the right data1. Dynamic Query Keys
Type: Added
TQueryKeyBuilder<TQueryKey>
typeThe
queryKey
config option now accepts either:LoadSubsetOptions
(for on-demand mode with predicate pushdown)LoadSubsetOptions Structure:
2. Meta Property Extension
When creating a query, the collection merges
LoadSubsetOptions
into the query's meta:Your
queryFn
can then access these options viacontext.meta.loadSubsetOptions
to fetch the appropriate data.3. Multi-Query Tracking System
Implemented comprehensive state tracking using Maps:
Reference Counting: Rows are only deleted from the collection when their reference count drops to zero (no queries reference them anymore).
4.
createQueryFromOpts
FunctionNew internal function that creates or reuses queries based on
LoadSubsetOptions
:Return Type:
true | Promise<void>
true
synchronously if query data is already availablePromise<void>
that resolves when query data loadsPromise<void>
that rejects if query encounters an errorBehavior:
QueryObserver
instances when the same predicates are requestedmeta.loadSubsetOptions
)5. Query Garbage Collection
Listens to TanStack Query's cache events to handle query removal:
Cleanup Process:
6. Sync Mode Integration
Eager Mode (default):
{}
)loadSubset
(returnsundefined
)On-Demand Mode:
markReady()
immediately since there's nothing to wait forloadSubset(options)
is calledcreateQueryFromOpts
directly as theloadSubset
implementationSync Started Tracking:
Added
syncStarted
flag to determine when to subscribe to new queries:true
when sync begins (viapreload()
,startSync
, or first subscriber)config.startSync
to handle all sync scenariosChanges
Files Modified:
packages/query-db-collection/src/query.ts
- Core implementation (+354 lines)packages/query-db-collection/tests/query.test.ts
- Comprehensive test suite (+567 lines).changeset/silent-trains-tell.md
- Changeset entryTest Coverage:
preload()
in on-demand modeKey Features
✅ Predicate Pushdown - Pass
LoadSubsetOptions
from live queries to TanStack Query✅ Multiple Concurrent Queries - Manage multiple TanStack Query instances with different predicates
✅ Reference Counting - Track which queries reference which rows
✅ Automatic Garbage Collection - Clean up queries and rows when no longer needed
✅ Promise-Based Loading - Return promises that resolve when data is available
✅ Sync Mode Support - Works with both eager and on-demand sync modes
✅ Immediate Ready State - On-demand collections transition to ready immediately
Breaking Changes
None - this is a backward-compatible extension. Existing Query Collections with static query keys continue to work as before.
Migration Guide
If you want to enable predicate pushdown:
syncMode: 'on-demand'
queryKey
from a static value to a builder functionqueryFn
viacontext.meta.loadSubsetOptions
Before:
After:
Related