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

README.md

60 lines2.0 KB

1# @tanstack/react-native-db-sqlite-persistence
2​
3Thin SQLite persistence for React Native apps (including Expo runtime).
4​
5Start with the [SQLite Persistence guide](../../docs/guides/sqlite-persistence.md) for Collection setup and lifecycle behavior.
6For pending server mutations, see the [React Native recipe with offline transactions](../../docs/guides/offline-transactions.md#use-sqlite-persistence-with-the-outbox).
7​
8## Public API
9​
10- `createReactNativeSQLitePersistence(...)`
11- `persistedCollectionOptions(...)` (re-exported from core)
12​
13## Quick start
14​
15```ts
16import { open } from '@op-engineering/op-sqlite'
17import { createCollection } from '@tanstack/db'
18import {
19 createReactNativeSQLitePersistence,
20 persistedCollectionOptions,
21} from '@tanstack/react-native-db-sqlite-persistence'
22​
23type Todo = {
24 id: string
25 title: string
26 completed: boolean
28​
29const database = open({
30 name: `tanstack-db.sqlite`,
31 location: `default`,
32})
33​
34// One shared persistence instance for the whole database.
35const persistence = createReactNativeSQLitePersistence({
36 database,
37})
38​
39export const todosCollection = createCollection(
40 persistedCollectionOptions<Todo, string>({
41 id: `todos`,
42 getKey: (todo) => todo.id,
43 persistence,
44 schemaVersion: 1, // Per-collection schema version
45 }),
47```
48​
49## Notes
50​
51- The same API is used for React Native and Expo runtimes.
52- `createReactNativeSQLitePersistence` is shared across collections.
53- Mode defaults (`sync-present` vs `sync-absent`) are inferred from whether a
54 `sync` config is present in `persistedCollectionOptions`.
55- Published OP-SQLite methods return self-describing object envelopes and need
56 no result-shape option. If a custom database wrapper instead returns a bare
57 array, set `arrayResultMode: 'rows'` for an array of data rows or
58 `arrayResultMode: 'statement-results'` for an array containing one statement
59 result. Undeclared arrays that also look like statement envelopes are rejected
60 rather than being reshaped heuristically.