tinybase 6.3.1 → 6.4.0-beta.1

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