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

README.md

96 lines2.6 KB

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