-
Notifications
You must be signed in to change notification settings - Fork 99
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 88b3092 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.33 kB (+1.77%) Total Size: 76.5 kB
ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
c3e7c93
to
0f7798b
Compare
* Maps each source alias to its collection ID. Enables per-alias subscriptions for self-joins. | ||
* Example: `{ employee: 'employees-col-id', manager: 'employees-col-id' }` | ||
*/ | ||
aliasToCollectionId: 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.
This is a mapping of the alias used in a from
or join
to the collection id that is to be used for that source.
* Maps outer alias to inner alias for subqueries (e.g., `{ activeUser: 'user' }`). | ||
* Used to resolve subscriptions during lazy loading when aliases differ. | ||
*/ | ||
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.
In this query below the alias mapping is used to track that activeUser
is an alias of the user
alias.
q.from(issue: issueCollection)
.join(
{ activeUser: q.from({ user: userCollection }) },
({ issue }) => ....
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:
employee
andmanager
aliases mapped to the same collectionemployeesCollection
employee
was incorrectly applied tomanager
tooThe Solution
Subscribe once per source alias, not once per collection.
Now
employee
andmanager
each 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
→sourceWhereClauses
tables
→sources
in 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.