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