feat(db): expose tracked source record subscriptions #1500
main fromrefs/pull/1500/head·opened Description#
Two new APIs let application code observe which source records a live query is consuming, so callers can subscribe/unsubscribe at an underlying sync layer in lockstep with query usage.
What's added#
type TrackedSourceRecord = {
collectionId: string
key: string | number
}
type TrackedSourceRecordsChange = {
added: Array<TrackedSourceRecord>
removed: Array<TrackedSourceRecord>
}
type SubscribeTrackedSourceRecordsOptions = {
includeInitialState?: boolean
}
Methods on every Collection:
collection.getTrackedSourceRecords(): Array<TrackedSourceRecord>
collection.subscribeTrackedSourceRecords(
callback: (change: TrackedSourceRecordsChange) => void,
options?: SubscribeTrackedSourceRecordsOptions,
): () => void
Same method, polymorphic dispatch:
- On a live-query collection → "records this query reads from its sources." Refcounted across the query's aliases (self-joins are deduped). Visible only while the query has active subscribers.
- On any other collection (base, electric, query-db, localStorage, sqlite-persisted, etc.) → "records of mine consumed by some live query." Refcounted across all live queries that depend on this collection. If two queries use key
K, the base view seesKonce; only when the last query stops usingKdoes it emitremoved.
Why#
Useful for custom sync layers that subscribe/unsubscribe at the record level. They need to subscribe when a record becomes actively used by queries and unsubscribe when the last query stops using it. Base-collection residency is too broad — it covers everything sync'd, whether queries want it or not. These APIs provide the narrower "actively used by live queries" signal.
How an app developer uses this#
On a single live query (per-query scope):
function ActiveUsersList() {
const activeUsers = useMemo(
() =>
createLiveQueryCollection((q) =>
q.from({ user: usersCollection }).where(({ user }) => eq(user.active, true)),
),
[],
)
// The hook drives sync (it calls subscribeChanges internally), so the
// tracked-source listener will start receiving deltas as the live query
// mounts and reads from its sources.
useEffect(() => {
return activeUsers.subscribeTrackedSourceRecords(({ added, removed }) => {
for (const r of added) mySyncLayer.subscribe(r.collectionId, r.key)
for (const r of removed) mySyncLayer.unsubscribe(r.collectionId, r.key)
})
}, [activeUsers])
const { data } = useLiveQuery(activeUsers)
return <ul>{data?.map((u) => <li key={u.id}>{u.name}</li>)}</ul>
}
On a base collection (union scope across all live queries):
const unsubscribe = usersCollection.subscribeTrackedSourceRecords(
({ added, removed }) => {
// Fires only when the first query starts tracking a key (0→1),
// or the last one stops (1→0).
for (const r of added) mySyncLayer.subscribe(r.collectionId, r.key)
for (const r of removed) mySyncLayer.unsubscribe(r.collectionId, r.key)
},
{ includeInitialState: true },
)
The same pattern works for any collection that uses CollectionImpl under the hood — electric, query-db, localStorage, sqlite-persisted, etc. The feature is sync-backend-agnostic because tracking happens in the live-query pipeline (the IVM), not in the sync config.
Behaviour notes:
includeInitialState: truereplays the current snapshot asaddedon subscribe (if any records are currently tracked and exposed).0 → 1+subscribers (a live query gains its first consumer): the per-query view emitsaddedfor every key the query is reading; the base view emitsaddedfor any key reaching refcount 1.1+ → 0subscribers (last consumer leaves): the per-query view emitsremovedfor every key it was reading; the base view emitsremovedfor any key whose refcount drops to 0.- Stable-membership updates (an ordered query updates a row that stays in range) emit nothing.
must-refetch/ truncate-refetch cycles that end with the same keys still tracked emit nothing.
How this works internally#
Three concentric scopes, each with a real owner.
Scope 1 — alias-level: CollectionSubscriber.sentToD2Keys
For every alias in a live query (e.g. { user: usersCollection }), one CollectionSubscriber is created. It already maintained sentToD2Keys: Set<string | number> to deduplicate inserts into the D2 dataflow graph — the authoritative per-alias record of "keys this alias is feeding the query."
This PR turns that set's mutations into events. On each sendChangesToPipeline, after filterDuplicateInserts has mutated the set, the subscriber derives a net delta from the filtered change stream itself — every surviving insert is a 0→1 transition for its key, every surviving delete is a 1→0 transition. Insert/delete pairs for the same key within one batch (emitted by splitUpdates for stable-membership ordered updates) net to zero. Cost is O(|batch|) — no dependency on the size of sentToD2Keys. The result is emitted via a single public callback property assigned 1:1 by the builder.
Scope 2 — per-live-query: LiveQueryTrackedSourceRecordsAggregator
One aggregator per sync session. Owns a nested refcount map:
private readonly entries = new Map<string, Map<string | number, { refCount: number }>>()
private exposed = false
Outer key is collectionId, inner key is the source record's primitive key — no composite serialization. The refcount is over aliases: a self-join like { employee: emp, manager: emp } has two subscribers emitting independently, and the aggregator dedupes. apply(collectionId, added, removed):
- Refcount the adds/removes against the per-collection inner map. 0→1 transitions go into
netAdded, 1→0 intonetRemoved. Empty inner buckets are pruned. - If
exposed: propagate net transitions tosourceCollections[collectionId]._trackedSourceRecords.apply(...)(scope 3), and fan out directly to the live-query-local listener set (held by reference from the builder).
setExposed(boolean) is the lifecycle gate. false → true replays the snapshot as added. true → false replays as removed. The builder wires this to subscribers:change:
config.collection.on('subscribers:change', (event) => {
trackedSourceRecordsAggregator.setExposed(event.subscriberCount > 0)
})
Scope 3 — per-base-collection: TrackedSourceRecordsManager
Each CollectionImpl owns one _trackedSourceRecords: TrackedSourceRecordsManager<TKey> field. Refcount map is Map<TKey, { key, refCount }> over live queries. On 0↔1 transitions, listeners registered via subscribe see added / removed. Record-object allocation is skipped when the listener set is empty.
Polymorphic dispatch on Collection.subscribeTrackedSourceRecords
CollectionImpl has an optional _liveQueryTrackedSourceView field. It's undefined on every collection except live queries; bridgeToCreateCollection sets it at construction time to a stable adapter on CollectionConfigBuilder (which routes through whatever aggregator the current sync session has). The public methods on CollectionImpl:
public getTrackedSourceRecords(): Array<TrackedSourceRecord> {
return this._liveQueryTrackedSourceView?.snapshot()
?? this._trackedSourceRecords.get()
}
public subscribeTrackedSourceRecords(callback, options?): () => void {
if (this._liveQueryTrackedSourceView) {
return this._liveQueryTrackedSourceView.subscribe(callback, options)
}
return this._trackedSourceRecords.subscribe(callback, options)
}
So callers don't need to branch on collection type. Same method name, scope determined by what kind of collection you're holding.
The live-query listener set lives on CollectionConfigBuilder (long-lived). The aggregator holds it by reference. External subscribers attached before the first sync session, or across session start/stop cycles, do not need to re-attach.
End-to-end flow:
CollectionSubscriber
sentToD2Keys mutates (filterDuplicateInserts)
count inserts/deletes in filtered batch → net delta
→ onTrackedKeysChange({ added, removed }) [direct callback, 1:1]
↓
LiveQueryTrackedSourceRecordsAggregator
apply(collectionId, added, removed)
refcount per (collectionId → key) in nested map
if exposed:
→ sourceCollections[id]._trackedSourceRecords.apply(netAdded, netRemoved)
if liveQuery listeners.size > 0:
for listener of listeners: listener({ added: records, removed: records })
↑
liveQuery.subscribeTrackedSourceRecords
(held by reference; survives sync sessions)
↓
TrackedSourceRecordsManager (on each source collection)
apply(netAdded, netRemoved)
refcount per key
if 0↔1 transitions AND listeners.size > 0:
for listener of listeners: listener({ added: records, removed: records })
↑
baseCollection.subscribeTrackedSourceRecords
Performance characteristics#
All hot paths are bounded by batch size, with no dependency on the total tracked-set size:
- Scope 1 delta:
O(|batch|)via insert/delete count netting on the filtered stream. No copy ofsentToD2Keys. - Scope 2 refcount:
O(|batch|)using a nestedMap<collectionId, Map<key, Entry>>— primitive keys, no serialization. - Scope 3 refcount:
O(|batch|)usingMap<TKey, Entry>— primitive keys, no serialization. - Record-object allocation at scopes 2 and 3 is gated on a non-empty listener set. Pure-internal batches (refcount updates with no app-code subscribers) skip the object materialization entirely.
Why not just expose CollectionSubscription.subscribedKeys?#
Two reasons:
sentToD2Keysis per-alias. A live query with multiple aliases on the same collection (self-joins) would require consumers to dedupe. Scope 2 exists precisely to handle that.subscribeChangesbuffers truncate/refetch to hide transient empty states.subscribeTrackedSourceRecordsneeds to emit net membership. Deriving one from the other would mix concerns.
Three scopes with clear ownership match the three semantic views application code may want.
What's not changed#
The feature is purely additive on CollectionImpl. Collection.utils and createCollection are byte-identical to upstream — no in-place mutation of user-supplied utils, no merge-precedence changes. The live-query path's utils merge stays at upstream's { ...options.utils, ...config.utils } (config wins). Tracked-source helpers are not on utils at all; they're direct methods on CollectionImpl.
How was this change tested?#
- Automated test (unit, integration, etc.)
- Manual test (provide reproducible testing steps below)
Regression coverage in packages/db/tests/query/live-query-collection.test.ts:
- Base-view delta flow with
includeInitialState, plus snapshot-as-removed on last-subscriber unsubscribe. - Base-view ref-counting across overlapping live queries (key not removed until the last query stops using it).
- Stable-membership updates emit nothing (ordered query, in-range update).
- Truncate-refetch cycles that keep the same keys tracked emit nothing.
- Live-query-local view: delta flow + snapshot-as-removed on unsubscribe.
- Live-query-local view:
includeInitialStatereplay. - Polymorphic-scope test: two live queries with disjoint where-clauses on the same base, asserting the base view sees the union and each per-query view sees only its own scope.
Plus a regression test in packages/db/tests/utility-exposure.test.ts pinning that user-supplied utils keys override built-ins on collision (the upstream merge-precedence contract).
The feature lives entirely on CollectionImpl and the live-query pipeline (CollectionSubscriber → aggregator → manager) — none of the sync backends are touched, so the in-memory tests exercise the same code paths that electric, query-db, localStorage, etc. would hit.
Screenshots#
- 1
- packages/db/src/collection/index.ts line 315OutdatedResolvedby kevin-dp(GitHub profile, opens in a new tab)@@ -299,3 +309,7 @@299309 // The core state of the collection is "public" so that is accessible in tests300310 // and for debugging301311 public _state: CollectionStateManager<TOutput, TKey, TSchema, TInput>312+ // Aggregated view of source-records currently being used by active live313+ // queries that depend on this collection. Public so live-query aggregators314+ // can push deltas in.315+ public _trackedSourceRecords: TrackedSourceRecordsManager<TKey>
It's a bit weird to have underscored variables that are exposed publicly. We could replace the direct cross-class access with a method on
CollectionImpl(e.g.applyTrackedSourceDelta(added, removed)that delegates internally). Then the field can be private.Addressed.
_trackedSourceRecordsis now private, and the aggregator no longer reaches into the field directly. I used a module-private WeakMap bridge so the aggregator can apply deltas without exposing a new public method or property onCollectionImpl.packages/db/src/collection/index.ts line 321OutdatedResolvedby kevin-dp(GitHub profile, opens in a new tab)@@ -299,3 +309,13 @@299309 // The core state of the collection is "public" so that is accessible in tests300310 // and for debugging301311 public _state: CollectionStateManager<TOutput, TKey, TSchema, TInput>312+ // Aggregated view of source-records currently being used by active live313+ // queries that depend on this collection. Public so live-query aggregators314+ // can push deltas in.315+ public _trackedSourceRecords: TrackedSourceRecordsManager<TKey>316+ // For live-query collections only: a live-query-local view of "source317+ // records this query is currently using." Set by the live-query path318+ // during construction; undefined on base collections. When present, the319+ // public `getTrackedSourceRecords` / `subscribeTrackedSourceRecords`320+ // methods route to this view instead of `_trackedSourceRecords`.321+ public _liveQueryTrackedSourceView?: {Similarly, it's a bit weird to have this public underscored variable. We could avoid this by passing the view through
CollectionConfig(or aCollectionImplconstructor argument) instead of assigning it post-construction. Then the field can be readonly and the assignment site moves into the constructor. The builder already exists at the momentbridgeToCreateCollectioncallscreateCollection, so this is mechanically straightforward — just thread it throughCollectionConfig.Addressed. _liveQueryTrackedSourceView is now private/readonly and initialized during collection construction instead of assigned externally after construction.
packages/db/src/query/live/collection-config-builder.ts line 76OutdatedResolvedby kevin-dp(GitHub profile, opens in a new tab)@@ -69,3 +73,4 @@6973 [LIVE_QUERY_INTERNAL]: LiveQueryInternalUtils7074 }717576+export type LiveQueryCollectionUtils<TUtils extends UtilsRecord = {}> = TUtils &Old:
LiveQueryCollectionUtils = UtilsRecord & { built-ins }(string-indexable). New:LiveQueryCollectionUtils<TUtils = {}> = TUtils & LiveQueryBuiltInUtils.LiveQueryCollectionUtils(no type arg) is now{} & LiveQueryBuiltInUtils, which lacks theRecord<string, any>index signature the old type had. Anyone doingconst u: LiveQueryCollectionUtils = ...; u.someCustomKeywithout specifyingTUtilswill see a new type error.Addressed by restoring the previous string-indexable UtilsRecord shape for live-query utils.
packages/db/src/collection/index.ts line 273OutdatedResolvedby kevin-dp(GitHub profile, opens in a new tab)@@ -266,4 +270,4 @@266270 collection.utils = {}267271 }268272269- return collection273+ return collection as unknown as Collection<Can't we pass these type args to
CollectionImpl(in particularUtilsRecord) such that we don't have to do an unsafe typecast here?Addressed by restoring the previous createCollection implementation typing, so this PR no longer introduces that cast.
@@ -0,0 +22,21 @@22+ private readonly listeners = new Set<23+ (change: TrackedSourceRecordsChange) => void24+ >()25+26+ constructor(private readonly collectionId: string) {}27+28+ apply(added: Iterable<TKey>, removed: Iterable<TKey>): void {29+ const netAdded: Array<TKey> = []30+ const netRemoved: Array<TKey> = []31+32+ for (const key of added) {33+ const existing = this.entries.get(key)34+ if (existing) {35+ existing.refCount++36+ } else {37+ this.entries.set(key, { key, refCount: 1 })38+ netAdded.push(key)39+ }40+ }41+42+ for (const key of removed) {Can it happen that a key wasn't known until now and occurs in both
addedandremoved? In that case it would be part of bothnetAddedandnetRemoved, is that problematic?I asked Claude about this, i agree with its answer and would go for adding built-in defense mechanism.
Not from the current callers, but the manager doesn't enforce it. Tracing upward:
CollectionSubscriber.emitTrackedKeyDeltanets per-key first (Map<key, number>), then partitions by sign — soaddedandremovedit emits are disjoint by construction.LiveQueryTrackedSourceRecordsAggregator.applyis called with that disjoint pair (single subscriber → single call), and its ownnetAdded/netRemovedlikewise can't share a key within one call.TrackedSourceRecordsManager.applyis only called by the aggregator, with the disjointnetAdded/netRemoved.
So end-to-end the precondition holds today. But if it were violated —
addedandremovedboth containing a keyKthat's not inentries— the manager would:- Process
added:entries.set(K, { refCount: 1 }), push K tonetAdded. - Process
removed:existing.refCount === 1→entries.delete(K), push K tonetRemoved.
Net result: listeners get
{ added: [K], removed: [K] }for a key that was never actually tracked. A sync layer wired to this wouldsubscribe(K)then immediatelyunsubscribe(K)for nothing. That is problematic.Two other overlap shapes are fine: if
Kalready exists withrefCount ≥ 1, the add/remove pair leavesrefCountunchanged and emits nothing (theif (existing.refCount === 1)guard correctly skips netRemoved when refCount is now > 1). Only the brand-new-key case is buggy.The aggregator's
applyhas the identical shape and the same latent fragility.Two options:
- Document the precondition in a JSDoc on both
applymethods: "added and removed must be disjoint; duplicates within either must be deduplicated by the caller." Cheap, makes the assumption explicit, leaves it to future contributors to honor. - Net defensively — same trick as
emitTrackedKeyDelta:
Costs one Map allocation per call but eliminates the entire class of caller mistakes (overlap and duplicates within either array).const net = new Map<TKey, number>() for (const k of added) net.set(k, (net.get(k) ?? 0) + 1) for (const k of removed) net.set(k, (net.get(k) ?? 0) - 1) // then iterate net entries by sign
I'd lean toward netting in the manager (it's the public-API boundary — callers reach through
_trackedSourceRecordsfrom outside the class, so the contract is harder to enforce by code review), and just documenting the precondition on the aggregator (private-ish, single internal caller).Addressed. Added defensive per-key netting before applying tracked-source deltas, so overlapping or duplicate keys in a single call cannot emit spurious added/removed churn.
kevin-dp(GitHub profile, opens in a new tab)replied on
packages/db/src/collection/tracked-source-records.tsline 42I asked Claude about this, i agree with its answer and would go for adding built-in defense mechanism.
Not from the current callers, but the manager doesn't enforce it. Tracing upward:
CollectionSubscriber.emitTrackedKeyDeltanets per-key first (Map<key, number>), then partitions by sign — soaddedandremovedit emits are disjoint by construction.LiveQueryTrackedSourceRecordsAggregator.applyis called with that disjoint pair (single subscriber → single call), and its ownnetAdded/netRemovedlikewise can't share a key within one call.TrackedSourceRecordsManager.applyis only called by the aggregator, with the disjointnetAdded/netRemoved.
So end-to-end the precondition holds today. But if it were violated —
addedandremovedboth containing a keyKthat's not inentries— the manager would:- Process
added:entries.set(K, { refCount: 1 }), push K tonetAdded. - Process
removed:existing.refCount === 1→entries.delete(K), push K tonetRemoved.
Net result: listeners get
{ added: [K], removed: [K] }for a key that was never actually tracked. A sync layer wired to this wouldsubscribe(K)then immediatelyunsubscribe(K)for nothing. That is problematic.Two other overlap shapes are fine: if
Kalready exists withrefCount ≥ 1, the add/remove pair leavesrefCountunchanged and emits nothing (theif (existing.refCount === 1)guard correctly skips netRemoved when refCount is now > 1). Only the brand-new-key case is buggy.The aggregator's
applyhas the identical shape and the same latent fragility.Two options:
- Document the precondition in a JSDoc on both
applymethods: "added and removed must be disjoint; duplicates within either must be deduplicated by the caller." Cheap, makes the assumption explicit, leaves it to future contributors to honor. - Net defensively — same trick as
emitTrackedKeyDelta:
Costs one Map allocation per call but eliminates the entire class of caller mistakes (overlap and duplicates within either array).const net = new Map<TKey, number>() for (const k of added) net.set(k, (net.get(k) ?? 0) + 1) for (const k of removed) net.set(k, (net.get(k) ?? 0) - 1) // then iterate net entries by sign
I'd lean toward netting in the manager (it's the public-API boundary — callers reach through
_trackedSourceRecordsfrom outside the class, so the contract is harder to enforce by code review), and just documenting the precondition on the aggregator (private-ish, single internal caller). More templates
@tanstack/angular-db
npm i https://pkg.pr.new/@tanstack/angular-db@1500@tanstack/browser-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/browser-db-sqlite-persistence@1500@tanstack/capacitor-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/capacitor-db-sqlite-persistence@1500@tanstack/cloudflare-durable-objects-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/cloudflare-durable-objects-db-sqlite-persistence@1500@tanstack/db
npm i https://pkg.pr.new/@tanstack/db@1500@tanstack/db-ivm
npm i https://pkg.pr.new/@tanstack/db-ivm@1500@tanstack/db-sqlite-persistence-core
npm i https://pkg.pr.new/@tanstack/db-sqlite-persistence-core@1500@tanstack/electric-db-collection
npm i https://pkg.pr.new/@tanstack/electric-db-collection@1500@tanstack/electron-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/electron-db-sqlite-persistence@1500@tanstack/expo-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/expo-db-sqlite-persistence@1500@tanstack/node-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/node-db-sqlite-persistence@1500@tanstack/offline-transactions
npm i https://pkg.pr.new/@tanstack/offline-transactions@1500@tanstack/powersync-db-collection
npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1500@tanstack/query-db-collection
npm i https://pkg.pr.new/@tanstack/query-db-collection@1500@tanstack/react-db
npm i https://pkg.pr.new/@tanstack/react-db@1500@tanstack/react-native-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/react-native-db-sqlite-persistence@1500@tanstack/rxdb-db-collection
npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1500@tanstack/solid-db
npm i https://pkg.pr.new/@tanstack/solid-db@1500@tanstack/svelte-db
npm i https://pkg.pr.new/@tanstack/svelte-db@1500@tanstack/tauri-db-sqlite-persistence
npm i https://pkg.pr.new/@tanstack/tauri-db-sqlite-persistence@1500@tanstack/trailbase-db-collection
npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1500@tanstack/vue-db
npm i https://pkg.pr.new/@tanstack/vue-db@1500commit:
0e1f0cfAddressed the feedback as separate commits, will reply inline to the comments, but for the additional coverage requested - added this:
- Added base-collection includeInitialState replay coverage.
- Added nested live-query coverage and kept the current behavior explicit: nested live queries expose direct source records for now.
- Added truncate/refetch coverage for different keys, plus cleanup-after-unsubscribe coverage for same-key truncate/refetch.
kevin-dp(GitHub profile, opens in a new tab) approved these changes
Thanks for addressing my review comments. I'll merge it when CI turns green. Thank you for your contribution!
@lalitkapoor Could you generate a changeset for this PR please? Then it will be ready to merge
Sign in to join this conversation.
kevin-dp(GitHub profile, opens in a new tab) requested changes
Hi @lalitkapoor
Thanks for your contribution! I reviewed your PR and left some comments. I also asked Claude to review and it reported some test gaps, may be worth adding those tests:
Test gap:
TrackedSourceRecordsManager.subscribeincludeInitialStatereplay#In every base-collection test,
subscribeTrackedSourceRecordsis called beforesubscribeChanges, so the manager is empty when the listener attaches and the initial-state replay path:is never executed. The mirror path in
liveQueryTrackedSourceView.subscribeis covered (test "should replay the live-query-local snapshot as initial state…"). Worth adding a base-collection counterpart: start a live query, preload, then subscribe to the base collection withincludeInitialState: trueand assert one immediateaddedevent.Test gap: nested live queries#
The PR description says a live query whose source is another live query "works because
_trackedSourceRecordslives onCollectionImpl." That's true mechanically — nothing crashes — but there's no end-to-end test, and the design has an interesting implication:liveQueryBwill populateliveQueryA._trackedSourceRecords(the base manager), butliveQueryA.subscribeTrackedSourceRecordswill always route through_liveQueryTrackedSourceViewand never expose that data. So the base-view of any live-query collection is silently unreachable. Either document that, or skip allocating_trackedSourceRecordswhen_liveQueryTrackedSourceViewis set.Test gap: truncate-refetch with different keys#
The truncate test (
should not emit tracked source churn across truncate refetch…) only covers the case where the refetched keys equal the previous keys. The interesting opposite case — refetch returns a different set, which should net to non-zero added/removed — isn't covered. Worth adding to pin the contract that real membership changes do still emit across a truncate cycle.