Open History to see the last commit
1# @tanstack/tauri-db-sqlite-persistence
2
3Thin SQLite persistence for Tauri apps using `@tauri-apps/plugin-sql`.
4
5Start with the [SQLite Persistence guide](../../docs/guides/sqlite-persistence.md) for Collection setup and lifecycle behavior.
6
7## Public API
8
9- `createTauriSQLitePersistence(...)`
10- `persistedCollectionOptions(...)` (re-exported from core)
11
12## Install
13
14```bash
15pnpm add @tanstack/tauri-db-sqlite-persistence @tauri-apps/plugin-sql
16```
17
18## Consumer-side Tauri setup
19
20Install the official SQL plugin in your Tauri app:
21
22```bash
23cd src-tauri
24cargo add tauri-plugin-sql --features sqlite
25```
26
27Register the plugin in `src-tauri/src/main.rs`:
28
29```rust
30fn main() {
31 tauri::Builder::default()
32 .plugin(tauri_plugin_sql::Builder::default().build())
33 .run(tauri::generate_context!())
34 .expect("error while running tauri application");
35}
36```
37
38Enable the SQL permissions in `src-tauri/capabilities/default.json`:
39
40```json
41{
42 "permissions": ["core:default", "sql:default", "sql:allow-execute"]
43}
44```
45
46## Quick start
47
48```ts
49import Database from '@tauri-apps/plugin-sql'
50import { createCollection } from '@tanstack/db'
51import {
52 createTauriSQLitePersistence,
53 persistedCollectionOptions,
54} from '@tanstack/tauri-db-sqlite-persistence'
55
56type Todo = {
57 id: string
58 title: string
59 completed: boolean
60}
61
62const database = await Database.load(`sqlite:tanstack-db.sqlite`)
63
64const persistence = createTauriSQLitePersistence({
65 database,
66})
67
68export const todosCollection = createCollection(
69 persistedCollectionOptions<Todo, string>({
70 id: `todos`,
71 getKey: (todo) => todo.id,
72 persistence,
73 schemaVersion: 1,
74 }),
75)
76```
77
78## Notes
79
80- `createTauriSQLitePersistence` is shared across collections.
81- Reuse a single `Database.load('sqlite:...')` handle per SQLite file when using
82 this package. Opening multiple plugin handles to the same file can reintroduce
83 SQLite locking behavior outside this package's serialized transaction queue.
84- Mode defaults (`sync-present` vs `sync-absent`) are inferred from whether a
85 `sync` config is present in `persistedCollectionOptions`.
86- This package expects a database handle created by
87 `@tauri-apps/plugin-sql`, typically from `Database.load('sqlite:...')`.
88- The database path is resolved by Tauri's SQL plugin, not by this package.
89- This package does not publish or require package-specific Rust code. Only the
90 app-level Tauri SQL plugin registration shown above is required.
91
92## Testing
93
94- `pnpm --filter @tanstack/tauri-db-sqlite-persistence test`
95 runs the driver and shared adapter contract tests.
96- `pnpm --filter @tanstack/tauri-db-sqlite-persistence test:e2e`
97 builds the repo-local Tauri harness and runs the persisted collection
98 conformance suite inside a real Tauri runtime.