|
| 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 |
0 commit comments