feat(query-db): support eager initial data#1683
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughQuery Collections now support eager ChangesEager initial data support
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant QueryCollection
participant QueryObserver
participant QueryClient
QueryCollection->>QueryObserver: configure initialData and select
QueryObserver->>QueryClient: read or seed query cache
QueryClient-->>QueryObserver: response and freshness state
QueryObserver-->>QueryCollection: materialized projected rows
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed: private package registry requires authentication. Disable ESLint in CodeRabbit settings or use public packages. 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. Comment |
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 125 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 4.22 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 125 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 4.22 kB ℹ️ View Unchanged
|
Adds typed, collection-local
initialDataandinitialDataUpdatedAtsupport to eager Query Collections. Initial Query responses now materialize immediately as collection rows while retaining TanStack Query's cache identity, freshness, and wrapped-response semantics.Root Cause
Query Collections already materialized data preloaded into a
QueryClient, but they could not declare initial data per collection. Applications sharing one client across many collections therefore had to choose between overly broad client defaults and imperative cache seeding. Forwarding defaults unchanged was also unsafe for on-demand subsets and placeholder data: neither has collection-wide row-membership semantics.Approach
initialDataandinitialDataUpdatedAtoptions using the original Query response type.select, materialization, and row-ownership pipeline.placeholderDatadefaults because placeholder results are observer-local UI state, not Query cache data suitable for normalized rows.Key Invariants
QueryClient.selectmaterializes rows without replacing the original wrapped response in the Query cache.Non-goals
placeholderDataas DB rows.Trade-offs
On-demand mode rejects explicit initial-data options rather than guessing subset membership. Callers that know an exact derived key can still seed or hydrate that Query cache entry. Eager mode suppresses placeholder defaults to keep observer presentation state out of the collection's shared normalized state.
Verification
The focused Query Collection suite covers fresh and stale initial data, function initializers, wrapped response projection, shared clients and exact keys, existing-cache precedence, on-demand rejection/default suppression, and placeholder-default suppression. Lint passes with existing warnings.
Files changed
packages/query-db-collection/src/query.ts— adds the public options and observer initialization policy.packages/query-db-collection/src/errors.ts— adds the on-demand configuration error.packages/query-db-collection/tests/query.test.ts— adds initialization and isolation coverage.docs/collections/query-collection.md— documents usage and user-visible semantics.docs/collections/query-initial-placeholder-data-design.md— records authority, ownership, transitions, writes, and persistence decisions..changeset/young-cats-initialize.md— records the minor package feature.Closes #346. Advances #1643.