Skip to content

Commit 4f4589e

Browse files
KyleAMathewsclaude
andauthored
docs: enhance installation and add comprehensive quick-start guide (#326)
Co-authored-by: Claude <[email protected]>
1 parent 0f22de1 commit 4f4589e

File tree

3 files changed

+230
-76
lines changed

3 files changed

+230
-76
lines changed

docs/config.json

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,15 @@
2121
"label": "Installation",
2222
"to": "installation"
2323
},
24-
{
25-
"label": "Live Queries",
26-
"to": "live-queries"
27-
}
2824
],
29-
"frameworks": [
30-
{
31-
"label": "react",
32-
"children": [
33-
{
34-
"label": "React Adapter",
35-
"to": "framework/react/adapter"
36-
}
37-
]
38-
},
39-
{
40-
"label": "vue",
41-
"children": [
42-
{
43-
"label": "Vue Adapter",
44-
"to": "framework/vue/adapter"
45-
}
46-
]
47-
}
48-
]
4925
},
5026
{
5127
"label": "Guides",
5228
"children": [
29+
{
30+
"label": "Live Queries",
31+
"to": "live-queries"
32+
},
5333
{
5434
"label": "Error Handling",
5535
"to": "error-handling"
@@ -60,49 +40,5 @@
6040
}
6141
]
6242
},
63-
{
64-
"label": "API Reference",
65-
"children": [
66-
{
67-
"label": "Core API Reference",
68-
"to": "reference/index"
69-
}
70-
],
71-
"frameworks": [
72-
{
73-
"label": "react",
74-
"children": [
75-
{
76-
"label": "React Hooks",
77-
"to": "framework/react/reference/index"
78-
}
79-
]
80-
},
81-
{
82-
"label": "vue",
83-
"children": [
84-
{
85-
"label": "Vue Hooks",
86-
"to": "framework/vue/reference/index"
87-
}
88-
]
89-
}
90-
]
91-
},
92-
{
93-
"label": "Examples",
94-
"children": [],
95-
"frameworks": [
96-
{
97-
"label": "react",
98-
"children": [
99-
{
100-
"label": "Todo List",
101-
"to": "framework/react/examples/todo"
102-
}
103-
]
104-
}
105-
]
106-
}
10743
]
10844
}

docs/installation.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ title: Installation
33
id: installation
44
---
55

6-
You can install TanStack DB with any [NPM](https://npmjs.com) package manager.
7-
8-
Only install one of the following packages depending on your use case:
6+
Each supported framework comes with its own package. Each framework package re-exports everything from the core `@tanstack/db` package.
97

108
## React
119

@@ -21,16 +19,12 @@ TanStack DB is compatible with React v16.8+
2119
npm install @tanstack/solid-db
2220
```
2321

24-
COMING SOON
25-
2622
## Svelte
2723

2824
```sh
2925
npm install @tanstack/svelte-db
3026
```
3127

32-
COMING SOON
33-
3428
## Vue
3529

3630
```sh
@@ -45,4 +39,49 @@ TanStack DB is compatible with Vue v3.3.0+
4539
npm install @tanstack/db
4640
```
4741

48-
Install the the core `@tanstack/db` package to use with any framework or without a framework. Each framework package up above will also re-export everything from this core package.
42+
Install the the core `@tanstack/db` package to use DB without a framework.
43+
44+
## Collection Packages
45+
46+
TanStack DB also provides specialized collection packages for different data sources and storage needs:
47+
48+
### Query Collection
49+
50+
For loading data using TanStack Query:
51+
52+
```sh
53+
npm install @tanstack/query-db-collection
54+
```
55+
56+
Use `queryCollectionOptions` to fetch data into collections using TanStack Query. This is perfect for REST APIs and existing TanStack Query setups.
57+
58+
### Local Collections
59+
60+
Local storage and in-memory collections are included with the framework packages:
61+
62+
- **LocalStorageCollection** - For persistent local data that syncs across browser tabs
63+
- **LocalOnlyCollection** - For temporary in-memory data and UI state
64+
65+
Both use `localStorageCollectionOptions` and `localOnlyCollectionOptions` respectively, available from your framework package (e.g., `@tanstack/react-db`).
66+
67+
### Sync Engines
68+
69+
#### Electric Collection
70+
71+
For real-time sync with [ElectricSQL](https://electric-sql.com):
72+
73+
```sh
74+
npm install @tanstack/electric-db-collection
75+
```
76+
77+
Use `electricCollectionOptions` to sync data from Postgres databases through ElectricSQL shapes. Ideal for real-time, local-first applications.
78+
79+
#### TrailBase Collection
80+
81+
For syncing with [TrailBase](https://trailbase.io) backends:
82+
83+
```sh
84+
npm install @tanstack/trailbase-db-collection
85+
```
86+
87+
Use `trailBaseCollectionOptions` to sync records from TrailBase's Record APIs with built-in subscription support.

docs/quick-start.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,182 @@
22
title: Quick Start
33
id: quick-start
44
---
5+
6+
TanStack DB is a reactive client store for building super fast apps. This example will show you how to:
7+
8+
- **Load data** into collections using TanStack Query
9+
- **Query data** with blazing fast live queries
10+
- **Mutate data** with instant optimistic updates
11+
12+
```tsx
13+
import { createCollection, useLiveQuery } from '@tanstack/react-db'
14+
import { queryCollectionOptions } from '@tanstack/query-db-collection'
15+
16+
// Define a collection that loads data using TanStack Query
17+
const todoCollection = createCollection(
18+
queryCollectionOptions({
19+
queryKey: ['todos'],
20+
queryFn: async () => {
21+
const response = await fetch('/api/todos')
22+
return response.json()
23+
},
24+
getKey: (item) => item.id,
25+
onUpdate: async ({ transaction }) => {
26+
const { original, modified } = transaction.mutations[0]
27+
await fetch(`/api/todos/${original.id}`, {
28+
method: 'PUT',
29+
body: JSON.stringify(modified),
30+
})
31+
},
32+
})
33+
)
34+
35+
function Todos() {
36+
// Live query that updates automatically when data changes
37+
const { data: todos } = useLiveQuery((q) =>
38+
q.from({ todo: todoCollection })
39+
.where(({ todo }) => !todo.completed)
40+
.orderBy(({ todo }) => todo.createdAt, 'desc')
41+
)
42+
43+
const toggleTodo = (todo) => {
44+
// Instantly applies optimistic state, then syncs to server
45+
todoCollection.update(todo.id, (draft) => {
46+
draft.completed = !draft.completed
47+
})
48+
}
49+
50+
return (
51+
<ul>
52+
{todos.map((todo) => (
53+
<li key={todo.id} onClick={() => toggleTodo(todo)}>
54+
{todo.text}
55+
</li>
56+
))}
57+
</ul>
58+
)
59+
}
60+
```
61+
62+
You now have collections, live queries, and optimistic mutations! Let's break this down further.
63+
64+
## Installation
65+
66+
```bash
67+
npm install @tanstack/react-db @tanstack/query-db-collection
68+
```
69+
70+
## 1. Create a Collection
71+
72+
Collections store your data and handle persistence. The `queryCollectionOptions` loads data using TanStack Query and defines mutation handlers for server sync:
73+
74+
```tsx
75+
const todoCollection = createCollection(
76+
queryCollectionOptions({
77+
queryKey: ['todos'],
78+
queryFn: async () => {
79+
const response = await fetch('/api/todos')
80+
return response.json()
81+
},
82+
getKey: (item) => item.id,
83+
// Handle all CRUD operations
84+
onInsert: async ({ transaction }) => {
85+
const { modified: newTodo } = transaction.mutations[0]
86+
await fetch('/api/todos', {
87+
method: 'POST',
88+
body: JSON.stringify(newTodo),
89+
})
90+
},
91+
onUpdate: async ({ transaction }) => {
92+
const { original, modified } = transaction.mutations[0]
93+
await fetch(`/api/todos/${original.id}`, {
94+
method: 'PUT',
95+
body: JSON.stringify(modified),
96+
})
97+
},
98+
onDelete: async ({ transaction }) => {
99+
const { original } = transaction.mutations[0]
100+
await fetch(`/api/todos/${original.id}`, { method: 'DELETE' })
101+
},
102+
})
103+
)
104+
```
105+
106+
## 2. Query with Live Queries
107+
108+
Live queries reactively update when data changes. They support filtering, sorting, joins, and transformations:
109+
110+
```tsx
111+
function TodoList() {
112+
// Basic filtering and sorting
113+
const { data: incompleteTodos } = useLiveQuery((q) =>
114+
q.from({ todo: todoCollection })
115+
.where(({ todo }) => !todo.completed)
116+
.orderBy(({ todo }) => todo.createdAt, 'desc')
117+
)
118+
119+
// Transform the data
120+
const { data: todoSummary } = useLiveQuery((q) =>
121+
q.from({ todo: todoCollection })
122+
.select(({ todo }) => ({
123+
id: todo.id,
124+
summary: `${todo.text} (${todo.completed ? 'done' : 'pending'})`,
125+
priority: todo.priority || 'normal'
126+
}))
127+
)
128+
129+
return <div>{/* Render todos */}</div>
130+
}
131+
```
132+
133+
## 3. Optimistic Mutations
134+
135+
Mutations apply instantly and sync to your server. If the server request fails, changes automatically roll back:
136+
137+
```tsx
138+
function TodoActions({ todo }) {
139+
const addTodo = () => {
140+
todoCollection.insert({
141+
id: crypto.randomUUID(),
142+
text: 'New todo',
143+
completed: false,
144+
createdAt: new Date(),
145+
})
146+
}
147+
148+
const toggleComplete = () => {
149+
todoCollection.update(todo.id, (draft) => {
150+
draft.completed = !draft.completed
151+
})
152+
}
153+
154+
const updateText = (newText) => {
155+
todoCollection.update(todo.id, (draft) => {
156+
draft.text = newText
157+
})
158+
}
159+
160+
const deleteTodo = () => {
161+
todoCollection.delete(todo.id)
162+
}
163+
164+
return (
165+
<div>
166+
<button onClick={addTodo}>Add Todo</button>
167+
<button onClick={toggleComplete}>Toggle</button>
168+
<button onClick={() => updateText('Updated!')}>Edit</button>
169+
<button onClick={deleteTodo}>Delete</button>
170+
</div>
171+
)
172+
}
173+
```
174+
175+
## Next Steps
176+
177+
You now understand the basics of TanStack DB! The collection loads and persists data, live queries provide reactive views, and mutations give instant feedback with automatic server sync.
178+
179+
Explore the docs to learn more about:
180+
181+
- **[Installation](./installation.md)** - All framework and collection packages
182+
- **[Overview](./overview.md)** - Complete feature overview and examples
183+
- **[Live Queries](./live-queries.md)** - Advanced querying, joins, and aggregations

0 commit comments

Comments
 (0)