-
Notifications
You must be signed in to change notification settings - Fork 119
change to a subscription per collection alias rather than collection inside a live query #625
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: 48769e0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 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: +1.5 kB (+1.94%) Total Size: 78.4 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 1.46 kB ℹ️ View Unchanged
|
c3e7c93 to
0f7798b
Compare
kevin-dp
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through the PR and it looks good! Wasn't an easy change is the subscription and collection ID were spread everywhere, good job refactoring that! Left a few minor comments?
| // Track alias remapping for subqueries (outer alias → inner alias) | ||
| // e.g., when .join({ activeUser: subquery }) where subquery uses .from({ user: collection }) | ||
| // we store: aliasRemapping['activeUser'] = 'user' | ||
| const aliasRemapping: Record<string, string> = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how this works. What if the subquery is also a query with a join?
Does that matter for this alias?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It always follows to the main path following the from.
| id, | ||
| autoIndex: `off`, | ||
| config: { | ||
| autoIndex: `off`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come we need autoIndex in the top level object and also in the config property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a mock of a collection object, and autoIndex is exposed as a prop on the top level as well as in the config prop.
|
🎉 This PR has been released! Thank you for your contribution! |
This fixes this bug report from Discord: https://discord.com/channels/719702312431386674/1369767025723052134/1422935844175614072
That report identified that a self query with a where clause on the main alias would result in the join not seeing the whole collection. It also tries to tidy up the terminology we use, dropping "table" and using source & collection.
This has grown in to a larger refactor of the compiler that changes it to do a subscription per source alias rather than a single subscription per collection.
Per-Alias Subscriptions: Fix Self-Joins and Enable Independent Filtering
The Problem
Live queries previously subscribed once per collection ID. This caused critical bugs when the same collection appeared multiple times in a query with different aliases:
What went wrong:
employeeandmanageraliases mapped to the same collectionemployeesCollectionemployeewas incorrectly applied tomanagertooThe Solution
Subscribe once per source alias, not once per collection.
Now
employeeandmanagereach get their own independent subscription with:Key Changes
1. Compiler Output Tracking
Added two new fields to
CompilationResult:2. Alias-Keyed Everything
Before:
After:
3. Subquery Alias Resolution
When a subquery uses different aliases than the parent query:
The compiler now tracks:
aliasRemapping['author'] = 'user'so lazy loading can find the correct subscription.4. Better Error Messages
5. Terminology Consistency
collectionWhereClauses→sourceWhereClausestables→sourcesin pipeline code6. Code Simplification
Removed unnecessary two-phase compilation that was guarding against optimizer-generated aliases (which never actually happened). The compiler now runs once with cleaner logic.
What Now Works
✅ Self-Joins
✅ Multiple Aliases with Independent Filters
✅ Subquery Alias Resolution
✅ Lazy Loading in Self-Joins
Breaking Changes
None for users. This is entirely internal to the query compilation and live layer.
The only "breaking" aspect is that previously broken queries (self-joins) now work correctly.
Performance Impact
Neutral to positive:
Testing
All existing tests pass. The fact that tests passed throughout development confirms:
Self-join scenarios that previously failed now work correctly.
Files Changed
packages/db/src/query/compiler/index.ts- Core compilation changespackages/db/src/query/compiler/joins.ts- Join processing with alias trackingpackages/db/src/query/live/collection-config-builder.ts- Per-alias subscription creationpackages/db/src/query/live/collection-subscriber.ts- Alias-aware subscriptionspackages/db/src/errors.ts- New error classes with better contextMigration Guide
No migration needed. Existing queries work unchanged. Self-joins that were broken now work.