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

README.md

141 lines5.0 KB

1# @tanstack/electron-db-sqlite-persistence
2​
3Thin Electron bridge for TanStack DB SQLite persistence.
4​
5## Public API
6​
7- `exposeElectronSQLitePersistence(...)` (main process)
8- `createElectronSQLitePersistence(...)` (renderer process)
9- `ElectronCollectionCoordinator`
10- `persistedCollectionOptions(...)` (re-exported from core)
11​
12Use `@tanstack/electron-db-sqlite-persistence/main` and
13`@tanstack/electron-db-sqlite-persistence/renderer` if you prefer
14explicit process-specific entrypoints.
15​
16## Main process
17​
18```ts
19import { ipcMain } from 'electron'
20import { createNodeSQLitePersistence } from '@tanstack/node-db-sqlite-persistence'
21import { exposeElectronSQLitePersistence } from '@tanstack/electron-db-sqlite-persistence/main'
22import Database from 'better-sqlite3'
23​
24const database = new Database(`./tanstack-db.sqlite`)
25​
26const persistence = createNodeSQLitePersistence({
27 database,
28})
29​
30const dispose = exposeElectronSQLitePersistence({
31 ipcMain,
32 persistence,
33})
34​
35// Call dispose() and database.close() during shutdown.
36```
37​
38## Renderer process
39​
40```ts
41import { createCollection } from '@tanstack/db'
42import { ipcRenderer } from 'electron'
43import {
44 createElectronSQLitePersistence,
45 persistedCollectionOptions,
46} from '@tanstack/electron-db-sqlite-persistence'
47​
48type Todo = {
49 id: string
50 title: string
51 completed: boolean
53​
54const persistence = createElectronSQLitePersistence<Todo, string>({
55 ipcRenderer,
56})
57​
58export const todosCollection = createCollection(
59 persistedCollectionOptions<Todo, string>({
60 id: `todos`,
61 getKey: (todo) => todo.id,
62 persistence,
63 schemaVersion: 1, // Per-collection schema version
64 }),
66```
67​
68## Multi-renderer ownership
69​
70Pass an `ElectronCollectionCoordinator` when multiple renderers share the same
71database. Coordinators using the same `dbName` elect a writer independently for
72each collection.
73​
74```ts
75import {
76 ElectronCollectionCoordinator,
77 createElectronSQLitePersistence,
78} from '@tanstack/electron-db-sqlite-persistence'
79​
80const coordinator = new ElectronCollectionCoordinator({
81 dbName: `tanstack-db`,
82})
83​
84const persistence = createElectronSQLitePersistence({
85 ipcRenderer,
86 coordinator,
87})
88​
89// On teardown:
90// coordinator.dispose()
91```
92​
93The persisted sync wrapper routes every source transaction with durable effects
94through the coordinator's required
95`requestApplyCommittedTx(collectionId, tx)` method. The elected renderer applies
96the complete `PersistedTx` through its resolved per-collection renderer adapter,
97which forwards it over IPC to the main-process persistence owner. Resolution
98remains specific to the collection id, mode, and `schemaVersion`.
99​
100Truncation, row changes, row metadata, collection metadata, and stream position
101stay in the same transaction. Electron does not feature-detect a partial route
102or fall back to row-only mutation RPC. A custom coordinator that omits
103`requestApplyCommittedTx` is rejected while the collection is configured,
104before its sync source can publish rows. An effect-free source commit neither
105publishes nor consumes a coordinator sequence.
106​
107If a mutating RPC loses its response, Electron coordination replays it only
108while the requester still knows the same non-null leader id and term. An
109unknown initial route or any leader/term change rejects with
110`IndeterminateCommitError`; the application must reconcile the outcome. The
111coordinator does not retry that mutation against an unknown or replacement
112leader.
113​
114Remote-subset requests use the same crash-only boundary. The coordinator
115projects a live `LoadSubsetOptions` input to the exported
116`TransportedLoadSubsetOptions` type before either local completion or renderer
117transport. Live `signal` and `subscription` fields do not cross the boundary.
118Unsupported nested values throw `RemoteSubsetWireValueError` with the exact
119value path before publication; Electron does not add codecs, coercion, or a
120fallback representation.
121​
122Electron uses the same explicit lease lifecycle as Browser coordination. The
123same request object reuses one acquisition identity, distinct equal objects
124remain independent, and release unloads the exact transported options once.
125Leadership loss unloads the retiring renderer's leases; still-live requester
126leases replay against the next leader. A second owner registration for one
127collection throws `DuplicateRemoteSubsetOwnerError`, and there is no adapter
128fallback for a missing owner. Remote follower transport/admission failures
129reject and retain demand for its normal retry without entering the follower's
130local owner lifecycle. Terminal release identities expire after the existing
131RPC dedupe horizon while delayed duplicates inside that horizon remain
132idempotent.
133​
134## Notes
135​
136- The renderer API mirrors other runtimes: one shared `create...Persistence`.
137- Collection mode (`sync-present` vs `sync-absent`) and `schemaVersion` are
138 resolved per collection and forwarded across IPC automatically.
139- Without an `ElectronCollectionCoordinator`, single-renderer mode uses
140 `SingleProcessCoordinator`: it is always leader, but still routes complete
141 transactions to the collection's resolved IPC adapter.