Skip to content

fix(db): reuse collection descriptors by id - #1770

Merged
tannerlinsley merged 2 commits into
mainfrom
fix-db-client-descriptor-identity
Aug 25, 2026
Merged

fix(db): reuse collection descriptors by id#1770
tannerlinsley merged 2 commits into
mainfrom
fix-db-client-descriptor-identity

Conversation

@KyleAMathews

@KyleAMathews KyleAMathews commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

DbClient now treats a collection descriptor's id as its identity, so separately created descriptors with the same id reuse one collection. React code can create dynamic descriptors from current parameters without keeping a descriptor cache, as long as every collection-defining parameter is included in the id.

Root cause

DbClient memoized materialized collections by descriptor object reference in a WeakMap. A new descriptor object therefore missed the cache even when its id matched an existing collection, and the client rejected the duplicate id. This diverged from the id-based identity used to track hydration, dehydration, cleanup, and live-query collections.

Approach

  • Use collectionsById as the single source of truth for materialized collections.
  • Return the first collection materialized for an id without invoking later same-id descriptor factories.
  • Recheck the id map after a factory runs, so a factory that recursively materializes the same id still resolves to one canonical collection.
  • Keep render-time sync deferral and dehydration eligibility intact when a collection is reused.
  • Document how dynamic scope values must contribute to both the descriptor id and Query key.

Key invariants

  • One DbClient has at most one materialized collection for each id.
  • The first descriptor materialized for an id defines that collection's configuration.
  • A reused collection keeps the correct render-time sync and dehydration behavior.
  • Different business scopes use different ids.

Non-goals

  • Compare descriptor factories, concrete configs, query keys, or query IR by deep value.
  • Merge or replace configuration from later descriptors that reuse an id.
  • Share collections across separate DbClient instances.

Trade-offs

Id-based lookup removes the need for caller-owned descriptor caches and matches the client's other collection bookkeeping. It also makes id collisions meaningful: callers must include every parameter that changes collection behavior in the id, and the first materialized descriptor wins.

Verification

pnpm --filter @tanstack/db build
pnpm exec vitest run packages/db/tests/db-client.test.ts --pool-options.threads.maxThreads=2
pnpm --filter @tanstack/react-db exec vitest --run --pool-options.threads.maxThreads=2

The database package tests cover separately created descriptors and reentrant same-id materialization. The React package test recreates a descriptor across renders and verifies that its factory runs once and its live-query collection stays stable.

Files changed

  • .changeset/fix-db-client-descriptor-identity.md: record the @tanstack/db patch release note.
  • packages/db/src/client.ts: canonicalize collection materialization by descriptor id.
  • packages/db/tests/db-client.test.ts: cover same-id descriptor reuse and reentrant factories.
  • packages/react-db/tests/useLiveQuery.test.tsx: cover fresh dynamic descriptors across React renders.
  • docs/guides/ssr.md: state the per-client id identity rule.
  • docs/collections/query-collection.md: replace descriptor-cache guidance with dynamic id guidance.

Summary by CodeRabbit

  • Bug Fixes

    • Collections with the same ID now reuse a single materialized collection, preventing duplicates when descriptors are recreated.
    • Preserves existing collection data and avoids repeated materialization across renders and nested creation.
  • Documentation

    • Clarified descriptor identity behavior and recommended including all dynamic parameters in collection IDs.
    • Updated guidance for creating business-scoped collection descriptors.

@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f65c3a06-cb79-4495-91d4-3f6388bfee1a

📥 Commits

Reviewing files that changed from the base of the PR and between afbacf7 and 9a7b572.

📒 Files selected for processing (6)
  • .changeset/fix-db-client-descriptor-identity.md
  • docs/collections/query-collection.md
  • docs/guides/ssr.md
  • packages/db/src/client.ts
  • packages/db/tests/db-client.test.ts
  • packages/react-db/tests/useLiveQuery.test.tsx

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

DbClient now reuses materialized collections by descriptor ID. Tests cover separate, recursive, and React-created descriptors. Documentation and the changeset describe the identity contract and dynamic ID requirements.

Changes

Descriptor identity reuse

Layer / File(s) Summary
ID-based collection materialization
packages/db/src/client.ts
DbClient checks collection IDs before and after factory execution. Matching IDs reuse the existing collection through reuseMaterializedCollection.
Core and React identity coverage
packages/db/tests/db-client.test.ts, packages/react-db/tests/useLiveQuery.test.tsx
Tests verify one factory execution, retained rows, recursive reuse, and stable live-query collections for recreated descriptors.
Descriptor identity documentation
docs/collections/query-collection.md, docs/guides/ssr.md, .changeset/fix-db-client-descriptor-identity.md
Documentation and release notes describe ID-based reuse and require dynamic parameters in descriptor IDs.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 9a7b5

This change reuses collections by descriptor id while preserving collection behavior; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant CollectionDescriptor
  participant DbClient
  participant optionsFactory
  participant MaterializedCollection
  CollectionDescriptor->>DbClient: Request materialization by ID
  DbClient->>MaterializedCollection: Check existing collection by ID
  DbClient->>optionsFactory: Invoke factory when no collection exists
  optionsFactory->>DbClient: Return collection options
  DbClient->>MaterializedCollection: Reuse matching collection
Loading

Suggested reviewers: tannerlinsley

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (3 skipped: 3 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: reusing collection descriptors by ID in DbClient.
Description check ✅ Passed The description is complete and directly related to the change. It explains the root cause, approach, invariants, trade-offs, non-goals, verification, affected files, and release impact through the in…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is complete and directly related to the change. It explains the root cause, approach, invariants, trade-offs, non-goals, verification, affected files, and release impact through the included changeset.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (3 skipped: 3 unsupported.)

✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-db-client-descriptor-identity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Aug 25, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@1770

@tanstack/browser-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/browser-db-sqlite-persistence@1770

@tanstack/capacitor-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/capacitor-db-sqlite-persistence@1770

@tanstack/cloudflare-durable-objects-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/cloudflare-durable-objects-db-sqlite-persistence@1770

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@1770

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@1770

@tanstack/db-sqlite-persistence-core

npm i https://pkg.pr.new/@tanstack/db-sqlite-persistence-core@1770

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@1770

@tanstack/electron-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/electron-db-sqlite-persistence@1770

@tanstack/expo-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/expo-db-sqlite-persistence@1770

@tanstack/node-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/node-db-sqlite-persistence@1770

@tanstack/offline-transactions

npm i https://pkg.pr.new/@tanstack/offline-transactions@1770

@tanstack/powersync-db-collection

npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1770

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@1770

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@1770

@tanstack/react-native-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/react-native-db-sqlite-persistence@1770

@tanstack/react-router-with-db

npm i https://pkg.pr.new/@tanstack/react-router-with-db@1770

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1770

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@1770

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@1770

@tanstack/tauri-db-sqlite-persistence

npm i https://pkg.pr.new/@tanstack/tauri-db-sqlite-persistence@1770

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1770

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@1770

commit: 9a7b572

@github-actions

Copy link
Copy Markdown
Contributor

Size Change: -78 B (-0.05%)

Total Size: 156 kB

📦 View Changed
Filename Size Change
packages/db/dist/esm/client.js 3.63 kB -78 B (-2.1%)
ℹ️ View Unchanged
Filename Size
packages/db/dist/esm/collection-options.js 236 B
packages/db/dist/esm/collection/change-events.js 1.44 kB
packages/db/dist/esm/collection/changes.js 1.95 kB
packages/db/dist/esm/collection/cleanup-queue.js 810 B
packages/db/dist/esm/collection/events.js 434 B
packages/db/dist/esm/collection/index.js 3.99 kB
packages/db/dist/esm/collection/indexes.js 1.99 kB
packages/db/dist/esm/collection/lifecycle.js 1.86 kB
packages/db/dist/esm/collection/mutations.js 2.54 kB
packages/db/dist/esm/collection/state.js 5.56 kB
packages/db/dist/esm/collection/subscription.js 5.94 kB
packages/db/dist/esm/collection/sync.js 4.13 kB
packages/db/dist/esm/collection/transaction-metadata.js 144 B
packages/db/dist/esm/deferred.js 207 B
packages/db/dist/esm/errors.js 5.26 kB
packages/db/dist/esm/event-emitter.js 748 B
packages/db/dist/esm/index.js 3.72 kB
packages/db/dist/esm/indexes/auto-index.js 829 B
packages/db/dist/esm/indexes/base-index.js 784 B
packages/db/dist/esm/indexes/basic-index.js 2.17 kB
packages/db/dist/esm/indexes/btree-index.js 2.29 kB
packages/db/dist/esm/indexes/index-registry.js 820 B
packages/db/dist/esm/indexes/reverse-index.js 557 B
packages/db/dist/esm/live-query-adapter.js 318 B
packages/db/dist/esm/live-query-observer.js 3.65 kB
packages/db/dist/esm/live-query-options.js 702 B
packages/db/dist/esm/live-query-window-controller.js 4.28 kB
packages/db/dist/esm/local-only.js 975 B
packages/db/dist/esm/local-storage.js 2.18 kB
packages/db/dist/esm/optimistic-action.js 359 B
packages/db/dist/esm/paced-mutations.js 496 B
packages/db/dist/esm/proxy.js 3.75 kB
packages/db/dist/esm/query/builder/functions.js 1.47 kB
packages/db/dist/esm/query/builder/index.js 6.59 kB
packages/db/dist/esm/query/builder/ref-proxy.js 1.24 kB
packages/db/dist/esm/query/compiler/evaluators.js 1.9 kB
packages/db/dist/esm/query/compiler/expressions.js 430 B
packages/db/dist/esm/query/compiler/group-by.js 3.69 kB
packages/db/dist/esm/query/compiler/index.js 8.71 kB
packages/db/dist/esm/query/compiler/joins.js 2.95 kB
packages/db/dist/esm/query/compiler/lazy-targets.js 1.11 kB
packages/db/dist/esm/query/compiler/order-by.js 1.8 kB
packages/db/dist/esm/query/compiler/parent-routes.js 319 B
packages/db/dist/esm/query/compiler/route-metadata.js 419 B
packages/db/dist/esm/query/compiler/select.js 1.58 kB
packages/db/dist/esm/query/effect.js 5.19 kB
packages/db/dist/esm/query/expression-helpers.js 1.43 kB
packages/db/dist/esm/query/ir-stable-identity.js 2.2 kB
packages/db/dist/esm/query/ir.js 1.59 kB
packages/db/dist/esm/query/live-query-collection.js 391 B
packages/db/dist/esm/query/live/bucket-facade-adapter.js 2.76 kB
packages/db/dist/esm/query/live/collection-config-builder.js 6.63 kB
packages/db/dist/esm/query/live/collection-registry.js 264 B
packages/db/dist/esm/query/live/collection-subscriber.js 2.39 kB
packages/db/dist/esm/query/live/internal.js 145 B
packages/db/dist/esm/query/live/materialized-pipeline.js 2.47 kB
packages/db/dist/esm/query/live/subset-demand-controller.js 1.24 kB
packages/db/dist/esm/query/live/utils.js 1.35 kB
packages/db/dist/esm/query/optimizer.js 2.92 kB
packages/db/dist/esm/query/predicate-utils.js 2.97 kB
packages/db/dist/esm/query/query-once.js 359 B
packages/db/dist/esm/query/subset-dedupe.js 1.34 kB
packages/db/dist/esm/scheduler.js 1.43 kB
packages/db/dist/esm/SortedMap.js 1.3 kB
packages/db/dist/esm/strategies/debounceStrategy.js 247 B
packages/db/dist/esm/strategies/queueStrategy.js 428 B
packages/db/dist/esm/strategies/throttleStrategy.js 246 B
packages/db/dist/esm/transactions.js 3.5 kB
packages/db/dist/esm/utils.js 927 B
packages/db/dist/esm/utils/array-utils.js 273 B
packages/db/dist/esm/utils/browser-polyfills.js 304 B
packages/db/dist/esm/utils/btree.js 5.61 kB
packages/db/dist/esm/utils/comparison.js 1.34 kB
packages/db/dist/esm/utils/cursor.js 457 B
packages/db/dist/esm/utils/index-optimization.js 2.39 kB
packages/db/dist/esm/utils/type-guards.js 157 B
packages/db/dist/esm/utils/uuid.js 449 B
packages/db/dist/esm/virtual-props.js 360 B

compressed-size-action::db-package-size

@github-actions

Copy link
Copy Markdown
Contributor

Size Change: 0 B

Total Size: 7.25 kB

ℹ️ View Unchanged
Filename Size
packages/react-db/dist/esm/DbProvider.js 317 B
packages/react-db/dist/esm/HydrationBoundary.js 263 B
packages/react-db/dist/esm/index.js 330 B
packages/react-db/dist/esm/live-query-internals.js 282 B
packages/react-db/dist/esm/useLiveInfiniteQuery.js 1.81 kB
packages/react-db/dist/esm/useLiveQuery.js 2.68 kB
packages/react-db/dist/esm/useLiveQueryEffect.js 355 B
packages/react-db/dist/esm/useLiveSuspenseQuery.js 812 B
packages/react-db/dist/esm/usePacedMutations.js 401 B

compressed-size-action::react-db-package-size

@tannerlinsley
tannerlinsley merged commit 9ad882f into main Aug 25, 2026
11 checks passed
@tannerlinsley
tannerlinsley deleted the fix-db-client-descriptor-identity branch August 25, 2026 18:09
@github-actions github-actions Bot mentioned this pull request Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants