Open History to see the last commit
1# @tanstack/browser-db-sqlite-persistence
2
3Browser SQLite persistence for TanStack DB using `wa-sqlite` + OPFS.
4
5Supports both single-tab (default) and multi-tab usage. Multi-tab coordination
6is opt-in by passing a `BrowserCollectionCoordinator`.
7
8## Public API
9
10- `createBrowserWASQLitePersistence(...)`
11- `openBrowserWASQLiteOPFSDatabase(...)`
12- `persistedCollectionOptions(...)` (re-exported from core)
13
14## Quick start (single-tab)
15
16By default, `createBrowserWASQLitePersistence` uses `SingleProcessCoordinator`
17semantics — no leader election, no `BroadcastChannel`, no Web Locks. This is
18the right choice when your app is only ever open in one tab at a time, or when
19each tab uses its own database.
20
21```ts
22import { createCollection } from '@tanstack/db'
23import {
24 createBrowserWASQLitePersistence,
25 openBrowserWASQLiteOPFSDatabase,
26 persistedCollectionOptions,
27} from '@tanstack/browser-db-sqlite-persistence'
28
29type Todo = {
30 id: string
31 title: string
32 completed: boolean
33}
34
35const database = await openBrowserWASQLiteOPFSDatabase({
36 databaseName: `tanstack-db.sqlite`,
37})
38
39const persistence = createBrowserWASQLitePersistence({
40 database,
41})
42
43export const todosCollection = createCollection(
44 persistedCollectionOptions<Todo, string>({
45 id: `todos`,
46 getKey: (todo) => todo.id,
47 persistence,
48 schemaVersion: 1, // Per-collection schema version
49 }),
50)
51```
52
53## Multi-tab usage
54
55To safely share a single OPFS database across multiple tabs of the same
56origin, pass a `BrowserCollectionCoordinator` via the `coordinator` option.
57The coordinator uses the Web Locks API to elect a leader tab, and
58`BroadcastChannel` to fan out committed transactions to follower tabs.
59Follower tabs forward writes to the leader via RPC over the channel.
60
61```ts
62import { createCollection } from '@tanstack/db'
63import {
64 BrowserCollectionCoordinator,
65 createBrowserWASQLitePersistence,
66 openBrowserWASQLiteOPFSDatabase,
67 persistedCollectionOptions,
68} from '@tanstack/browser-db-sqlite-persistence'
69
70const database = await openBrowserWASQLiteOPFSDatabase({
71 databaseName: `tanstack-db.sqlite`,
72})
73
74const coordinator = new BrowserCollectionCoordinator({
75 dbName: `tanstack-db`,
76})
77
78const persistence = createBrowserWASQLitePersistence({
79 database,
80 coordinator,
81})
82
83export const todosCollection = createCollection(
84 persistedCollectionOptions<Todo, string>({
85 id: `todos`,
86 getKey: (todo) => todo.id,
87 persistence,
88 schemaVersion: 1,
89 }),
90)
91
92// On teardown:
93// coordinator.dispose()
94// await database.close?.()
95```
96
97See [`examples/react/offline-transactions`](../../examples/react/offline-transactions/src/db/persisted-todos.ts)
98for a full multi-tab example.
99
100## Notes
101
102- `openBrowserWASQLiteOPFSDatabase(...)` starts a dedicated Web Worker and
103 routes SQL operations through it. OPFS sync access handle APIs are used in
104 that worker context.
105- On `pagehide`, the connection terminates its worker synchronously and rejects
106 pending initialization, queries, and close operations with `AbortError`. This
107 also applies when the document enters the back/forward cache. Applications
108 restored through
109 `pageshow` with `event.persisted` must create fresh database connections and
110 persistence/collection instances (or reload the application).
111- Flush application writes and await `database.close()` before navigating when
112 possible. The `pagehide` cleanup releases resources; it cannot await pending
113 writes or serve as a durability guarantee.
114- Single-tab mode does not require `BroadcastChannel` or Web Locks for
115 correctness.
116- Multi-tab mode requires `BroadcastChannel` and the Web Locks API; both are
117 available in all modern browsers.
118- OPFS capability failures are surfaced as `PersistenceUnavailableError`.
119- SQLite open failures include the underlying VFS error name and message when
120 wa-sqlite exposes them. Some failures, including native access-handle
121 contention, are not currently exposed by wa-sqlite and retain SQLite's
122 generic open error.