Open History to see the last commit
1# @tanstack/node-db-sqlite-persistence
2
3Thin Node SQLite persistence for TanStack DB.
4
5Start with the [SQLite Persistence guide](../../docs/guides/sqlite-persistence.md) for Collection setup and lifecycle behavior.
6
7## Public API
8
9- `createNodeSQLitePersistence(...)`
10- `persistedCollectionOptions(...)` (re-exported from core)
11
12## Quick start
13
14```ts
15import { createCollection } from '@tanstack/db'
16import {
17 createNodeSQLitePersistence,
18 persistedCollectionOptions,
19} from '@tanstack/node-db-sqlite-persistence'
20import Database from 'better-sqlite3'
21
22type Todo = {
23 id: string
24 title: string
25 completed: boolean
26}
27
28// You own database lifecycle directly.
29const database = new Database(`./tanstack-db.sqlite`)
30
31// One shared persistence instance for the whole database.
32const persistence = createNodeSQLitePersistence({
33 database,
34})
35
36export const todosCollection = createCollection(
37 persistedCollectionOptions<Todo, string>({
38 id: `todos`,
39 getKey: (todo) => todo.id,
40 persistence,
41 schemaVersion: 1, // Per-collection schema version
42 }),
43)
44```
45
46## Notes
47
48- `createNodeSQLitePersistence` is shared across collections; it resolves
49 mode-specific behavior (`sync-present` vs `sync-absent`) automatically.
50- `schemaVersion` is specified per collection via `persistedCollectionOptions`.
51- Call `database.close()` when your app shuts down.
52
53## Applied transaction pruning
54
55The `applied_tx` log is a replayable cache, so it is pruned by default to keep
56the SQLite file from growing without bound. When you don't pass prune options,
57the node driver applies:
58
59- `appliedTxPruneMaxRows: 1_000` (per-collection row cap)
60- `appliedTxPruneMaxAgeSeconds: 86_400` (24h age backstop)
61
62Pruning runs inside each write transaction, so every collection self-trims on
63its next sync. If a process asks to recover from a point older than the retained
64replay window, recovery falls back to a full reload. Override either value to
65tune retention, or pass `0` to disable that limit:
66
67```ts
68const persistence = createNodeSQLitePersistence({
69 database,
70 appliedTxPruneMaxRows: 5_000, // higher row cap
71 appliedTxPruneMaxAgeSeconds: 0, // disable the age backstop
72})
73```
74
75Pruning removes rows from `applied_tx`, but SQLite may not immediately shrink
76the database file on disk. Use `VACUUM`, `auto_vacuum`, or your own maintenance
77process if you need to reclaim disk space.
78
79The defaults are exported as `DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS` and
80`DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS`.