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

README.md

109 lines3.9 KB

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