packages/react-native-db-sqlite-persistence
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
@tanstack/react-native-db-sqlite-persistence#
Thin SQLite persistence for React Native apps (including Expo runtime).
Start with the SQLite Persistence guide for Collection setup and lifecycle behavior. For pending server mutations, see the React Native recipe with offline transactions.
Public API#
createReactNativeSQLitePersistence(...)persistedCollectionOptions(...)(re-exported from core)
Quick start#
import { open } from '@op-engineering/op-sqlite'
import { createCollection } from '@tanstack/db'
import {
createReactNativeSQLitePersistence,
persistedCollectionOptions,
} from '@tanstack/react-native-db-sqlite-persistence'
type Todo = {
id: string
title: string
completed: boolean
}
const database = open({
name: `tanstack-db.sqlite`,
location: `default`,
})
// One shared persistence instance for the whole database.
const persistence = createReactNativeSQLitePersistence({
database,
})
export const todosCollection = createCollection(
persistedCollectionOptions<Todo, string>({
id: `todos`,
getKey: (todo) => todo.id,
persistence,
schemaVersion: 1, // Per-collection schema version
}),
)
Notes#
- The same API is used for React Native and Expo runtimes.
createReactNativeSQLitePersistenceis shared across collections.- Mode defaults (
sync-presentvssync-absent) are inferred from whether asyncconfig is present inpersistedCollectionOptions. - Published OP-SQLite methods return self-describing object envelopes and need
no result-shape option. If a custom database wrapper instead returns a bare
array, set
arrayResultMode: 'rows'for an array of data rows orarrayResultMode: 'statement-results'for an array containing one statement result. Undeclared arrays that also look like statement envelopes are rejected rather than being reshaped heuristically.