Open History to see the last commit
1# @tanstack/capacitor-db-sqlite-persistence
2
3Thin SQLite persistence for Capacitor apps using
4`@capacitor-community/sqlite`.
5
6Start with the [SQLite Persistence guide](../../docs/guides/sqlite-persistence.md) for Collection setup and lifecycle behavior.
7
8## Public API
9
10- `createCapacitorSQLitePersistence(...)`
11- `persistedCollectionOptions(...)` (re-exported from core)
12
13## Install
14
15```bash
16pnpm add @tanstack/capacitor-db-sqlite-persistence @capacitor-community/sqlite
17```
18
19## Quick start
20
21```ts
22import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite'
23import { createCollection } from '@tanstack/db'
24import { QueryClient } from '@tanstack/query-core'
25import { queryCollectionOptions } from '@tanstack/query-db-collection'
26import {
27 createCapacitorSQLitePersistence,
28 persistedCollectionOptions,
29} from '@tanstack/capacitor-db-sqlite-persistence'
30
31type Todo = {
32 id: string
33 title: string
34 completed: boolean
35}
36
37const sqlite = new SQLiteConnection(CapacitorSQLite)
38const database = await sqlite.createConnection(
39 `tanstack-db`,
40 false,
41 `no-encryption`,
42 1,
43 false,
44)
45
46await database.open()
47
48// One shared persistence instance for the whole database.
49const persistence = createCapacitorSQLitePersistence({
50 database,
51})
52const queryClient = new QueryClient()
53
54export const todosCollection = createCollection(
55 persistedCollectionOptions<Todo, string>({
56 ...queryCollectionOptions({
57 queryKey: [`todos`],
58 queryFn: async () => {
59 const response = await fetch(`/api/todos`)
60 return response.json() as Promise<Array<Todo>>
61 },
62 queryClient,
63 getKey: (todo) => todo.id,
64 }),
65 id: `todos`,
66 persistence,
67 schemaVersion: 1, // Per-collection schema version
68 }),
69)
70```
71
72## Platform notes
73
74- `createCapacitorSQLitePersistence` is shared across collections.
75- Mode defaults (`sync-present` vs `sync-absent`) are inferred from whether a
76 `sync` config is present in `persistedCollectionOptions`.
77- This package assumes you provide an already-created and opened
78 `SQLiteDBConnection`.
79- Calling `close()` on this driver's database adapter closes the underlying
80 `SQLiteDBConnection`, so treat driver ownership as connection ownership.
81- This package targets native Capacitor runtimes. Use the dedicated browser and
82 Electron persistence packages instead of the plugin's web or Electron modes.
83- The plugin's database version and upgrade APIs are separate from TanStack DB
84 `schemaVersion`.
85- `@capacitor-community/sqlite` uses SQLCipher-backed native builds, so app
86 setup may require additional platform configuration outside this package.
87- Capacitor 8 creates iOS projects with Swift Package Manager by default, but
88 `@capacitor-community/sqlite` currently links through CocoaPods on iOS. Add
89 iOS with `npx cap add ios --packagemanager CocoaPods`, or recreate an SPM
90 project in CocoaPods mode before expecting the native plugin to load.
91
92## Testing
93
94- `pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2e`
95 runs the package e2e suite against the default `better-sqlite3` harness.
96- `pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2e:ios`
97 builds the package-local Capacitor harness in `e2e/app`, launches the iOS
98 simulator, and runs the full persisted collection e2e suite inside the real
99 native Capacitor runtime.
100- `pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2e:android`
101 builds the same package-local Capacitor harness in `e2e/app`, launches an
102 Android emulator or uses a connected debug target, and runs the full
103 persisted collection e2e suite inside the real native Capacitor runtime.
104- `test:e2e:ios` is a repo-local validation path for this package. It depends on
105 the checked-in `e2e/app` harness plus local Capacitor/Xcode tooling.
106- `test:e2e:android` depends on a local Android SDK. The runner auto-detects the
107 default macOS SDK location, boots the first available AVD when needed, and
108 reads the app-owned SQLite result database out of the debug sandbox with
109 `adb run-as`.
110- The native harness lives under `e2e/app` so the same app can be extended to
111 other native targets later, including Android.