-
Notifications
You must be signed in to change notification settings - Fork 103
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
Open
KyleAMathews
wants to merge
1
commit into
main
Choose a base branch
from
kevin/pred-pushdown-query-coll-rebased
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+926
−140
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: Kevin De Porre <kevin@electric-sql.com> Co-authored-by: Sam Willis <sam.willis@gmail.com>
🦋 Changeset detectedLatest commit: 1db4f71 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
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.