Skip to content

Commit 63b5f8a

Browse files
committed
[rn] Types
1 parent 17d37a4 commit 63b5f8a

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

src/@types/omni/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export * from '../persisters/persister-partykit-server/index.d.ts';
2424
export * from '../persisters/persister-pglite/index.d.ts';
2525
export * from '../persisters/persister-postgres/index.d.ts';
2626
export * from '../persisters/persister-powersync/index.d.ts';
27+
export * from '../persisters/persister-react-native-sqlite/index.d.ts';
2728
export * from '../persisters/persister-remote/index.d.ts';
2829
export * from '../persisters/persister-sqlite-bun/index.d.ts';
2930
export * from '../persisters/persister-sqlite-wasm/index.d.ts';

src/@types/omni/with-schemas/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from '../../persisters/persister-partykit-server/with-schemas/index.d.t
2323
export * from '../../persisters/persister-pglite/with-schemas/index.d.ts';
2424
export * from '../../persisters/persister-postgres/with-schemas/index.d.ts';
2525
export * from '../../persisters/persister-powersync/with-schemas/index.d.ts';
26+
export * from '../../persisters/persister-react-native-sqlite/with-schemas/index.d.ts';
2627
export * from '../../persisters/persister-remote/with-schemas/index.d.ts';
2728
export * from '../../persisters/persister-sqlite-bun/with-schemas/index.d.ts';
2829
export * from '../../persisters/persister-sqlite-wasm/with-schemas/index.d.ts';

src/@types/persisters/docs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* |SqliteBunPersister| SQLite in Bun, via [bun:sqlite](https://bun.sh/docs/api/sqlite)|Yes|Yes*
2424
* |SqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|Yes|Yes*
2525
* |ExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|Yes|Yes*
26+
* |ReactNativeSqlitePersister|SQLite in React Native, via [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage)|Yes|Yes*
2627
* |PostgresPersister|PostgreSQL, via [postgres](https://github.com/porsager/postgres)|Yes|Yes*
2728
* |PglitePersister|PostgreSQL, via [PGlite](https://github.com/electric-sql/pglite)|Yes|Yes*
2829
* |CrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|Yes|No
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/**
2+
* The persister-react-native-sqlite module of the TinyBase project lets you
3+
* save and load Store data to and from a SQLite database using the
4+
* [`react-native-sqlite-storage`](https://github.com/andpor/react-native-sqlite-storage) module (in an appropriate React Native
5+
* environment).
6+
* @see Database Persistence guide
7+
* @packageDocumentation
8+
* @module persister-react-native-sqlite
9+
* @since v6.4.0
10+
*/
11+
/// persister-react-native-sqlite
12+
/**
13+
* The ReactNativeSqlitePersister interface represents a Persister that lets you
14+
* save and load Store data to and from a `react-native-sqlite-storage`
15+
* database.
16+
*
17+
* You should use the createReactNativeSqlitePersister function to create an
18+
* ReactNativeSqlitePersister object.
19+
*
20+
* It is a minor extension to the Persister interface and simply provides an
21+
* extra getDb method for accessing a reference to the database instance the
22+
* Store is being persisted to.
23+
* @category Persister
24+
* @since v6.4.0
25+
*/
26+
/// ReactNativeSqlitePersister
27+
28+
/**
29+
* The getDb method returns a reference to the database instance the Store is
30+
* being persisted to.
31+
* @returns A reference to the database instance.
32+
* @example
33+
* This example creates a Persister object against a newly-created Store and
34+
* then gets the database instance back out again.
35+
*
36+
* ```js yolo
37+
* import {enablePromise, openDatabase} from 'react-native-sqlite-storage';
38+
* import {createStore} from 'tinybase';
39+
* import {createReactNativeSqlitePersister} from 'tinybase/persisters/persister-react-native-sqlite';
40+
*
41+
* enablePromise(true);
42+
* const db = await openDatabase({name: 'my.db', location: 'default'});
43+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
44+
* const persister = createReactNativeSqlitePersister(
45+
* store,
46+
* db,
47+
* 'my_tinybase',
48+
* );
49+
* console.log(persister.getDb() == db);
50+
* // -> true
51+
*
52+
* await persister.destroy();
53+
* ```
54+
* @category Getter
55+
* @since v6.4.0
56+
*/
57+
/// ReactNativeSqlitePersister.getDb
58+
59+
/**
60+
* The createReactNativeSqlitePersister function creates an
61+
* ReactNativeSqlitePersister object that can persist the Store to a local
62+
* `react-native-sqlite-storage` database.
63+
*
64+
* An ReactNativeSqlitePersister supports regular Store objects, and can also be
65+
* used to persist the metadata of a MergeableStore when using the JSON
66+
* serialization mode, as described below.
67+
*
68+
* As well as providing a reference to the Store to persist, you must provide a
69+
* `db` parameter which identifies the database instance.
70+
*
71+
* **Important note:** the `react-native-sqlite-storage` module must have had
72+
* promises enabled before the database instance is passed in. Use
73+
* `SQLite.enablePromise(true)` before initializing the Persister.
74+
*
75+
* A database Persister uses one of two modes: either a JSON serialization of
76+
* the whole Store stored in a single row of a table (the default), or a tabular
77+
* mapping of Table Ids to database table names and vice-versa).
78+
*
79+
* The third argument is a DatabasePersisterConfig object that configures which
80+
* of those modes to use, and settings for each. If the third argument is simply
81+
* a string, it is used as the `storeTableName` property of the JSON
82+
* serialization.
83+
*
84+
* See the documentation for the DpcJson and DpcTabular types for more
85+
* information on how both of those modes can be configured.
86+
* @param store The Store or MergeableStore to persist.
87+
* @param db The database instance that was returned from
88+
* `await SQLite.openDatabase(...)`.
89+
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
90+
* persistence mode (or a string to set the `storeTableName` property of the
91+
* JSON serialization).
92+
* @param onSqlCommand An optional handler called every time the Persister
93+
* executes a SQL command or query. This is suitable for logging persistence
94+
* behavior in a development environment.
95+
* @param onIgnoredError An optional handler for the errors that the Persister
96+
* would otherwise ignore when trying to save or load data. This is suitable for
97+
* debugging persistence issues in a development environment.
98+
* @returns A reference to the new ReactNativeSqlitePersister object.
99+
* @example
100+
* This example creates a ReactNativeSqlitePersister object and persists the
101+
* Store to a local SQLite database as a JSON serialization into the
102+
* `my_tinybase` table. It makes a change to the database directly and then
103+
* reloads it back into the Store.
104+
*
105+
* ```js yolo
106+
* import {enablePromise, openDatabase} from 'react-native-sqlite-storage';
107+
* import {createStore} from 'tinybase';
108+
* import {createReactNativeSqlitePersister} from 'tinybase/persisters/persister-react-native-sqlite';
109+
*
110+
* enablePromise(true);
111+
* const db = await openDatabase({name: 'my.db', location: 'default'});
112+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
113+
* const persister = createReactNativeSqlitePersister(
114+
* store,
115+
* db,
116+
* 'my_tinybase',
117+
* );
118+
*
119+
* await persister.save();
120+
* // Store will be saved to the database.
121+
*
122+
* console.log(
123+
* (await db.executeSql('SELECT * FROM my_tinybase;'))[0].rows.raw(),
124+
* );
125+
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
126+
*
127+
* await db.executeSql(
128+
* 'UPDATE my_tinybase SET store = ' +
129+
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
130+
* );
131+
* await persister.load();
132+
* console.log(store.getTables());
133+
* // -> {pets: {felix: {species: 'cat'}}}
134+
*
135+
* await persister.destroy();
136+
* ```
137+
* @example
138+
* This example creates a ReactNativeSqlitePersister object and persists the
139+
* Store to a local SQLite database with tabular mapping.
140+
*
141+
* ```js yolo
142+
* import {enablePromise, openDatabase} from 'react-native-sqlite-storage';
143+
* import {createStore} from 'tinybase';
144+
* import {createReactNativeSqlitePersister} from 'tinybase/persisters/persister-react-native-sqlite';
145+
*
146+
* enablePromise(true);
147+
* const db = await openDatabase({name: 'my.db', location: 'default'});
148+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
149+
* const persister = createReactNativeSqlitePersister(store, db, {
150+
* mode: 'tabular',
151+
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
152+
* });
153+
*
154+
* await persister.save();
155+
* console.log((await db.executeSql('SELECT * FROM pets;'))[0].rows.raw());
156+
* // -> [{_id: 'fido', species: 'dog'}]
157+
*
158+
* await db.executeSql(
159+
* `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
160+
* );
161+
* await persister.load();
162+
* console.log(store.getTables());
163+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
164+
*
165+
* await persister.destroy();
166+
* ```
167+
* @category Creation
168+
* @since v6.4.0
169+
*/
170+
/// createReactNativeSqlitePersister
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// persister-react-native-sqlite
2+
import {type SQLiteDatabase} from 'react-native-sqlite-storage';
3+
import type {MergeableStore} from '../../mergeable-store/index.d.ts';
4+
import type {Store} from '../../store/index.d.ts';
5+
import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.ts';
6+
7+
/// ReactNativeSqlitePersister
8+
export interface ReactNativeSqlitePersister
9+
extends Persister<Persists.StoreOrMergeableStore> {
10+
/// ReactNativeSqlitePersister.getDb
11+
getDb(): SQLiteDatabase;
12+
}
13+
14+
/// createReactNativeSqlitePersister
15+
export function createReactNativeSqlitePersister(
16+
store: Store | MergeableStore,
17+
db: SQLiteDatabase,
18+
configOrStoreTableName?: DatabasePersisterConfig | string,
19+
onSqlCommand?: (sql: string, params?: any[]) => void,
20+
onIgnoredError?: (error: any) => void,
21+
): ReactNativeSqlitePersister;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// persister-react-native-sqlite
2+
import type {SQLiteDatabase} from 'react-native-sqlite-storage';
3+
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.ts';
4+
import type {
5+
OptionalSchemas,
6+
Store,
7+
} from '../../../store/with-schemas/index.d.ts';
8+
import type {
9+
DatabasePersisterConfig,
10+
Persister,
11+
Persists,
12+
} from '../../with-schemas/index.d.ts';
13+
14+
/// ReactNativeSqlitePersister
15+
export interface ReactNativeSqlitePersister<Schemas extends OptionalSchemas>
16+
extends Persister<Schemas, Persists.StoreOrMergeableStore> {
17+
/// ReactNativeSqlitePersister.getDb
18+
getDb(): SQLiteDatabase;
19+
}
20+
21+
/// createReactNativeSqlitePersister
22+
export function createReactNativeSqlitePersister<
23+
Schemas extends OptionalSchemas,
24+
>(
25+
store: Store<Schemas> | MergeableStore<Schemas>,
26+
db: SQLiteDatabase,
27+
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
28+
onSqlCommand?: (sql: string, params?: any[]) => void,
29+
onIgnoredError?: (error: any) => void,
30+
): ReactNativeSqlitePersister<Schemas>;

0 commit comments

Comments
 (0)