Overview
TanStack DB is a client-side embedded database layer built on differential dataflow. It maintains normalized collections, uses incremental computation for live queries, provides automatic optimistic mutations, and integrates with TanStack Query for data fetching. Sub-millisecond updates even with 100k+ rows.
Package: @tanstack/react-db
Query Integration: @tanstack/query-db-collection
Status: Beta (v0.5)
Installation
Core Concepts
- Collections: Normalized data stores wrapping data sources (TanStack Query, Electric, etc.)
- Live Queries: Reactive subscriptions with SQL-like query builder
- Optimistic Mutations: Automatic instant UI updates with rollback on failure
- Differential Dataflow: Only recomputes affected query results on changes
Collections
Creating a Collection
Sync Modes
Live Queries
Basic Query
Query Builder API
Joins
Filter Operators
With Ordering and Limits
Optimistic Mutations
Insert
No Manual Boilerplate
Query-Driven Sync (On-Demand)
Live queries automatically generate optimized network requests:
Predicate Mapping
Performance
Supported Collection Types
- Query Collection - TanStack Query integration
- Electric Collection - Electric SQL real-time sync
- TrailBase Collection - TrailBase backend
- RxDB Collection - RxDB integration
- PowerSync Collection - PowerSync sync
- LocalStorage Collection - Browser persistence
- LocalOnly Collection - In-memory only
API Summary
Best Practices
- Define collections at module level - they're singletons
- Choose the right sync mode:
eager(<10k),on-demand(>50k),progressive(collaborative) - Use joins instead of view-specific APIs - load normalized collections once
- Let TanStack Query handle fetching - DB augments Query, doesn't replace it
- Use
parseLoadSubsetOptionsto map live query predicates to API params - Rely on automatic optimistic updates - don't manually manage optimistic state
- Use schemas for runtime validation and TypeScript inference
- Leverage incremental computation - let the engine handle filtering vs manual
.filter()
Common Pitfalls
- Creating collections inside components (should be module-level)
- Trying to replace TanStack Query entirely (DB builds on top of it)
- Using manual
.filter()in render instead of live querywhereclauses - Not providing
getKeyfor proper normalization - Forgetting mutation handlers (
onInsert,onUpdate,onDelete) for server sync


