@tanstack/capacitor-db-sqlite-persistence
v0.1.5
Published
Capacitor SQLite persisted collection adapter for TanStack DB
Readme
@tanstack/capacitor-db-sqlite-persistence
Thin SQLite persistence for Capacitor apps using
@capacitor-community/sqlite.
Public API
createCapacitorSQLitePersistence(...)persistedCollectionOptions(...)(re-exported from core)
Install
pnpm add @tanstack/capacitor-db-sqlite-persistence @capacitor-community/sqliteQuick start
import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite'
import { createCollection } from '@tanstack/db'
import { QueryClient } from '@tanstack/query-core'
import { queryCollectionOptions } from '@tanstack/query-db-collection'
import {
createCapacitorSQLitePersistence,
persistedCollectionOptions,
} from '@tanstack/capacitor-db-sqlite-persistence'
type Todo = {
id: string
title: string
completed: boolean
}
const sqlite = new SQLiteConnection(CapacitorSQLite)
const database = await sqlite.createConnection(
`tanstack-db`,
false,
`no-encryption`,
1,
false,
)
await database.open()
// One shared persistence instance for the whole database.
const persistence = createCapacitorSQLitePersistence({
database,
})
const queryClient = new QueryClient()
export const todosCollection = createCollection(
persistedCollectionOptions<Todo, string>({
...queryCollectionOptions({
queryKey: [`todos`],
queryFn: async () => {
const response = await fetch(`/api/todos`)
return response.json() as Promise<Array<Todo>>
},
queryClient,
getKey: (todo) => todo.id,
}),
id: `todos`,
persistence,
schemaVersion: 1, // Per-collection schema version
}),
)Platform notes
createCapacitorSQLitePersistenceis shared across collections.- Mode defaults (
sync-presentvssync-absent) are inferred from whether asyncconfig is present inpersistedCollectionOptions. - This package assumes you provide an already-created and opened
SQLiteDBConnection. - Calling
close()on this driver's database adapter closes the underlyingSQLiteDBConnection, so treat driver ownership as connection ownership. - This package targets native Capacitor runtimes. Use the dedicated browser and Electron persistence packages instead of the plugin's web or Electron modes.
- The plugin's database version and upgrade APIs are separate from TanStack DB
schemaVersion. @capacitor-community/sqliteuses SQLCipher-backed native builds, so app setup may require additional platform configuration outside this package.- Capacitor 8 creates iOS projects with Swift Package Manager by default, but
@capacitor-community/sqlitecurrently links through CocoaPods on iOS. Add iOS withnpx cap add ios --packagemanager CocoaPods, or recreate an SPM project in CocoaPods mode before expecting the native plugin to load.
Testing
pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2eruns the package e2e suite against the defaultbetter-sqlite3harness.pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2e:iosbuilds the package-local Capacitor harness ine2e/app, launches the iOS simulator, and runs the full persisted collection e2e suite inside the real native Capacitor runtime.pnpm --filter @tanstack/capacitor-db-sqlite-persistence test:e2e:androidbuilds the same package-local Capacitor harness ine2e/app, launches an Android emulator or uses a connected debug target, and runs the full persisted collection e2e suite inside the real native Capacitor runtime.test:e2e:iosis a repo-local validation path for this package. It depends on the checked-ine2e/appharness plus local Capacitor/Xcode tooling.test:e2e:androiddepends on a local Android SDK. The runner auto-detects the default macOS SDK location, boots the first available AVD when needed, and reads the app-owned SQLite result database out of the debug sandbox withadb run-as.- The native harness lives under
e2e/appso the same app can be extended to other native targets later, including Android.
