Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

README.md

197 lines7.6 KB

1# TanStack DB
2​
3<!-- ![TanStack DB Header](https://github.com/tanstack/db/raw/main/media/repo-header.png) -->
4​
5**A reactive client store for building super fast apps on sync**
6​
7TanStack DB extends TanStack Query with collections, live queries and optimistic mutations that keep your UI reactive, consistent and blazing fast 🔥
8​
9<p>
10 <a href="https://x.com/intent/post?text=TanStack%20DB&url=https://tanstack.com/db">
11 <img alt="#TanStack" src="https://img.shields.io/twitter/url?color=%2308a0e9&label=%23TanStack&style=social&url=https%3A%2F%2Ftwitter.com%2Fintent%2Ftweet%3Fbutton_hashtag%3DTanStack" /></a>
12 <a href="#status">
13 <img src="https://img.shields.io/badge/status-beta-yellow" alt="Status - BETA"></a>
14 <a href="https://npmjs.com/package/@tanstack/db">
15 <img alt="" src="https://img.shields.io/npm/dm/@tanstack/db.svg" /></a>
16 <a href="https://discord.gg/yjUNbvbraC">
17 <img alt="" src="https://img.shields.io/badge/Discord-TanStack-%235865F2" /></a>
18 <a href="https://github.com/tanstack/db/discussions">
19 <img alt="Join the discussion on Github" src="https://img.shields.io/badge/Discussions-Chat%20now!-green" /></a>
20 <a href="https://x.com/tan_stack">
21 <img alt="" src="https://img.shields.io/twitter/follow/tan_stack.svg?style=social&label=Follow @TanStack" /></a>
22</p>
23​
24Enjoy this library? Try the entire [TanStack](https://tanstack.com), including [TanStack Query](https://tanstack.com/query), [TanStack Store](https://tanstack.com/store), etc.
25​
26## 🚀 Why TanStack DB?
27​
28TanStack DB gives you robust support for real-time sync, live queries and local writes. With no stale data, super fast re-rendering and sub-millisecond cross-collection queries — even for large complex apps.
29​
30Built on a TypeScript implementation of differential dataflow ([#](https://github.com/electric-sql/d2ts)), TanStack DB gives you:
31​
32- 🔥 **a blazing fast query engine**<br />
33 for sub-millisecond live queries &mdash; even for complex queries with joins and aggregates
34- 🎯 **fine-grained reactivity**<br />
35 to minimize component re-rendering
36- 💪 **robust transaction primitives**<br />
37 for easy optimistic mutations with sync and lifecycle support
38- 🌟 **normalized data**<br />
39 to keep your backend simple
40​
41TanStack DB is **backend agnostic** and **incrementally adoptable**:
42​
43- plug in any backend: sync engines, REST APIs, GraphQL, polling, custom sources
44- builds on [TanStack Store](https://tanstack.com/store), works with and alongside [TanStack Query](https://tanstack.com/query)
45​
46## 💥 Usage example
47​
48Sync data into collections:
49​
50```ts
51import { createCollection, QueryClient } from "@tanstack/react-db"
52import { queryCollectionOptions } from "@tanstack/query-db-collection"
53​
54const todoCollection = createCollection(
55 queryCollectionOptions({
56 queryKey: ["todos"],
57 queryFn: async () => fetch("/api/todos"),
58 queryClient: new QueryClient(),
59 getKey: (item) => item.id,
60 schema: todoSchema, // any standard schema
61 })
63```
64​
65Use live queries in your components:
66​
67```tsx
68import { useLiveQuery } from "@tanstack/react-db"
69import { eq } from "@tanstack/db"
70​
71const Todos = () => {
72 const { data: todos } = useLiveQuery((query) =>
73 query
74 .from({ todos: todoCollection })
75 .where(({ todos }) => eq(todos.completed, false))
76 )
77​
78 return <List items={todos} />
80```
81​
82Apply mutations with local optimistic state:
83​
84```tsx
85// Define collection with persistence handlers
86const todoCollection = createCollection({
87 id: "todos",
88 // ... other config
89 onInsert: async ({ transaction }) => {
90 const modified = transaction.mutations[0].modified
91 await api.todos.create(modified)
92 },
93})
94​
95// Then use collection operators in your components
96const AddTodo = () => {
97 return (
98 <Button
99 onClick={() =>
100 todoCollection.insert({
101 id: uuid(),
102 text: "🔥 Make app faster",
103 completed: false,
104 })
105 }
106 />
107 )
109```
110​
111## 📚 Docs
112​
113See the [Usage guide](./docs/overview.md) for more details, including how to do:
114​
115- real-time sync
116- cross-collection queries
117- fine-grained reactivity
118- different strategies for data loading and handling mutations
119​
120There's also an example [React todo app](./examples/react/todo) and usage examples in the [package tests](./packages/db/tests).
121​
122## 🧱 Core concepts
123​
124### Collections
125​
126- typed sets of objects that can mirror a backend table or be populated with a filtered view or result set, such as `pendingTodos` or `decemberNewTodos`
127- collections are just JavaScript data &mdash; load them on demand and define as many as you need
128​
129### Live queries
130​
131- run reactively against and across collections with support for joins, filters and aggregates
132- powered by differential dataflow: query results update incrementally, not by re-running the whole query
133​
134### Transactional mutators
135​
136- batch and stage local changes across collections with immediate application of local optimistic updates
137- sync transactions to the backend with automatic rollbacks and management of optimistic state
138​
139## 📦 Collection Types
140​
141TanStack DB provides several collection types to support different backend integrations:
142​
143- **`@tanstack/db`** - Core collection functionality with local-only and local-storage collections for offline-first applications
144- **`@tanstack/query-db-collection`** - Collections backed by [TanStack Query](https://tanstack.com/query) for REST APIs and GraphQL endpoints
145- **`@tanstack/electric-db-collection`** - Real-time sync collections powered by [ElectricSQL](https://electric-sql.com) for live database synchronization
146- **`@tanstack/trailbase-db-collection`** - Collections for [TrailBase](https://trailbase.io) backend integration
147​
148## Framework integrations
149​
150TanStack DB integrates with React & Vue with more on the way!
151​
152- **`@tanstack/react-db`** - React hooks and components for using TanStack DB collections in React applications
153- **`@tanstack/vue-db`** - Vue composables for using TanStack DB collections in Vue applications
154​
155## 🔧 Install
156​
157```bash
158npm install @tanstack/react-db
159# Optional: for specific collection types
160npm install @tanstack/electric-db-collection @tanstack/query-db-collection
161```
162​
163Other framework integrations are in progress.
164​
165## ❓ FAQ
166​
167**How is this different from TanStack Query?**<br />
168TanStack DB builds _on top of_ TanStack Query. Use Query to fetch data; use DB to manage reactive local collections and mutations. They complement each other.
169​
170**Do I need a sync engine like ElectricSQL?**<br />
171No. TanStack DB _is_ designed to work with sync engines like [Electric](https://electric-sql.com) but _also_ works with any backend: polling APIs, GraphQL, REST, or custom sync logic.
172​
173**What is a Collection? Is it like a DB table?**<br />
174Kind of. Collections are typed sets of objects, but they can also be filtered views or custom groupings. They're just JavaScript structures that you define and manage.
175​
176**Is this an ORM? Do queries hit my backend?**<br />
177No. TanStack DB is not an ORM. Queries run entirely in the client against local collections. The framework provides strong primitives to manage how data is loaded and synced.
178​
179## Partners
180​
181<a href="https://electric-sql.com">
182 <img alt="ElectricSQL logo"
183 src="https://raw.githubusercontent.com/electric-sql/meta/main/identity/ElectricSQL-logo.with-background.sm.png"
184 />
185</a>
186​
187## Status
188​
189Tanstack DB is currently in BETA. See [the release post](https://tanstack.com/blog/tanstack-db-0.1-the-embedded-client-database-for-tanstack-query) for more details.
190​
191## Contributing
192​
193View the contributing guidelines [here](https://github.com/TanStack/query/blob/main/CONTRIBUTING.md).
194​
195### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
196​
197<!-- Use the force, Luke -->