-
Notifications
You must be signed in to change notification settings - Fork 104
Handle pushed down predicates in query collection #681
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
base: kevin/dedup-callback
Are you sure you want to change the base?
Handle pushed down predicates in query collection #681
Conversation
🦋 Changeset detectedLatest commit: 21f9f10 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: 89.4 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 2.89 kB ℹ️ View Unchanged
|
1db4f71
to
285fff5
Compare
I pushed a unit test for limited ordered queries. The test currently fails because the query collection uses |
a844ab2
to
61beafa
Compare
9630dbe
to
e865acd
Compare
I added support for |
Co-authored-by: Kevin De Porre <[email protected]> Co-authored-by: Sam Willis <[email protected]>
e865acd
to
21f9f10
Compare
61beafa
to
313299a
Compare
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
Note: This PR replaces #646 with a rebased branch to remove duplicate commits from #669 that were already merged into main.