@tanstack/browser-db-sqlite-persistence 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -5
- package/dist/assets/opfs-worker-uyjx5Rus.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# @tanstack/browser-db-sqlite-persistence
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser SQLite persistence for TanStack DB using `wa-sqlite` + OPFS.
|
|
4
|
+
|
|
5
|
+
Supports both single-tab (default) and multi-tab usage. Multi-tab coordination
|
|
6
|
+
is opt-in by passing a `BrowserCollectionCoordinator`.
|
|
4
7
|
|
|
5
8
|
## Public API
|
|
6
9
|
|
|
@@ -8,7 +11,12 @@ Thin browser SQLite persistence for TanStack DB using `wa-sqlite` + OPFS.
|
|
|
8
11
|
- `openBrowserWASQLiteOPFSDatabase(...)`
|
|
9
12
|
- `persistedCollectionOptions(...)` (re-exported from core)
|
|
10
13
|
|
|
11
|
-
## Quick start
|
|
14
|
+
## Quick start (single-tab)
|
|
15
|
+
|
|
16
|
+
By default, `createBrowserWASQLitePersistence` uses `SingleProcessCoordinator`
|
|
17
|
+
semantics — no leader election, no `BroadcastChannel`, no Web Locks. This is
|
|
18
|
+
the right choice when your app is only ever open in one tab at a time, or when
|
|
19
|
+
each tab uses its own database.
|
|
12
20
|
|
|
13
21
|
```ts
|
|
14
22
|
import { createCollection } from '@tanstack/db'
|
|
@@ -42,13 +50,60 @@ export const todosCollection = createCollection(
|
|
|
42
50
|
)
|
|
43
51
|
```
|
|
44
52
|
|
|
53
|
+
## Multi-tab usage
|
|
54
|
+
|
|
55
|
+
To safely share a single OPFS database across multiple tabs of the same
|
|
56
|
+
origin, pass a `BrowserCollectionCoordinator` via the `coordinator` option.
|
|
57
|
+
The coordinator uses the Web Locks API to elect a leader tab, and
|
|
58
|
+
`BroadcastChannel` to fan out committed transactions to follower tabs.
|
|
59
|
+
Follower tabs forward writes to the leader via RPC over the channel.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { createCollection } from '@tanstack/db'
|
|
63
|
+
import {
|
|
64
|
+
BrowserCollectionCoordinator,
|
|
65
|
+
createBrowserWASQLitePersistence,
|
|
66
|
+
openBrowserWASQLiteOPFSDatabase,
|
|
67
|
+
persistedCollectionOptions,
|
|
68
|
+
} from '@tanstack/browser-db-sqlite-persistence'
|
|
69
|
+
|
|
70
|
+
const database = await openBrowserWASQLiteOPFSDatabase({
|
|
71
|
+
databaseName: `tanstack-db.sqlite`,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const coordinator = new BrowserCollectionCoordinator({
|
|
75
|
+
dbName: `tanstack-db`,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const persistence = createBrowserWASQLitePersistence({
|
|
79
|
+
database,
|
|
80
|
+
coordinator,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
export 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
|
+
|
|
97
|
+
See [`examples/react/offline-transactions`](../../examples/react/offline-transactions/src/db/persisted-todos.ts)
|
|
98
|
+
for a full multi-tab example.
|
|
99
|
+
|
|
45
100
|
## Notes
|
|
46
101
|
|
|
47
|
-
- This package is Phase 7 single-tab browser wiring: it uses
|
|
48
|
-
`SingleProcessCoordinator` semantics by default.
|
|
49
102
|
- `openBrowserWASQLiteOPFSDatabase(...)` starts a dedicated Web Worker and
|
|
50
103
|
routes SQL operations through it. OPFS sync access handle APIs are used in
|
|
51
104
|
that worker context.
|
|
52
|
-
- Single-tab mode does not require BroadcastChannel or Web Locks for
|
|
105
|
+
- Single-tab mode does not require `BroadcastChannel` or Web Locks for
|
|
53
106
|
correctness.
|
|
107
|
+
- Multi-tab mode requires `BroadcastChannel` and the Web Locks API; both are
|
|
108
|
+
available in all modern browsers.
|
|
54
109
|
- OPFS capability failures are surfaced as `PersistenceUnavailableError`.
|