|
| 1 | +/** |
| 2 | + * The persister-expo-sqlite module of the TinyBase project lets you save and |
| 3 | + * load Store data to and from a local Expo-SQLite database (in an appropriate |
| 4 | + * React Native environment). |
| 5 | + * |
| 6 | + * Note that this Persister is currently experimental as Expo themselves iterate |
| 7 | + * on the underlying database library API. |
| 8 | + * @see Persisting Data guide |
| 9 | + * @packageDocumentation |
| 10 | + * @module persister-expo-sqlite |
| 11 | + */ |
| 12 | +/// persister-expo-sqlite |
| 13 | +/** |
| 14 | + * The createExpoSqlitePersister function creates a Persister object that can |
| 15 | + * persist the Store to a local Expo-SQLite database (in an appropriate React |
| 16 | + * Native environment). |
| 17 | + * |
| 18 | + * Note that this Persister is currently experimental as Expo themselves iterate |
| 19 | + * on the underlying database library API. |
| 20 | + * |
| 21 | + * As well as providing a reference to the Store to persist, you must provide a |
| 22 | + * `db` parameter which identifies the database instance. |
| 23 | + * |
| 24 | + * A database Persister uses one of two modes: either a JSON serialization of |
| 25 | + * the whole Store stored in a single row of a table (the default), or a tabular |
| 26 | + * mapping of Table Ids to database table names and vice-versa). |
| 27 | + * |
| 28 | + * The third argument is a DatabasePersisterConfig object that configures which |
| 29 | + * of those modes to use, and settings for each. If the third argument is simply |
| 30 | + * a string, it is used as the `storeTableName` property of the JSON |
| 31 | + * serialization. |
| 32 | + * |
| 33 | + * See the documentation for the DpcJson and DpcTabular types for more |
| 34 | + * information on how both of those modes can be configured. |
| 35 | + * @param store The Store to persist. |
| 36 | + * @param db The database instance that was returned from |
| 37 | + * `SQLite.openDatabase(...)`. |
| 38 | + * @param configOrStoreTableName A DatabasePersisterConfig to configure the |
| 39 | + * persistence mode (or a string to set the `storeTableName` property of the |
| 40 | + * JSON serialization). |
| 41 | + * @returns A reference to the new Persister object. |
| 42 | + * @example |
| 43 | + * This example creates a Persister object and persists the Store to a local |
| 44 | + * SQLite database as a JSON serialization into the `my_tinybase` table. It |
| 45 | + * makes a change to the database directly and then reloads it back into the |
| 46 | + * Store. |
| 47 | + * |
| 48 | + * ```js yolo |
| 49 | + * const db = SQLite.openDatabase('my.db'); |
| 50 | + * const store = createStore().setTables({pets: {fido: {species: 'dog'}}}); |
| 51 | + * const persister = createExpoSqlitePersister(store, db, 'my_tinybase'); |
| 52 | + * |
| 53 | + * await persister.save(); |
| 54 | + * // Store will be saved to the database. |
| 55 | + * |
| 56 | + * console.log( |
| 57 | + * await new Promise((resolve) => |
| 58 | + * db.all('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)), |
| 59 | + * ), |
| 60 | + * ); |
| 61 | + * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}] |
| 62 | + * |
| 63 | + * await new Promise((resolve) => |
| 64 | + * db.all( |
| 65 | + * 'UPDATE my_tinybase SET store = ' + |
| 66 | + * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`, |
| 67 | + * resolve, |
| 68 | + * ), |
| 69 | + * ); |
| 70 | + * await persister.load(); |
| 71 | + * console.log(store.getTables()); |
| 72 | + * // -> {pets: {felix: {species: 'cat'}}} |
| 73 | + * |
| 74 | + * persister.destroy(); |
| 75 | + * ``` |
| 76 | + * @example |
| 77 | + * This example creates a Persister object and persists the Store to a local |
| 78 | + * SQLite database with tabular mapping. |
| 79 | + * |
| 80 | + * ```js yolo |
| 81 | + * const db = SQLite.openDatabase('my.db'); |
| 82 | + * const store = createStore().setTables({pets: {fido: {species: 'dog'}}}); |
| 83 | + * const persister = createExpoSqlitePersister(store, db, { |
| 84 | + * mode: 'tabular', |
| 85 | + * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}}, |
| 86 | + * }); |
| 87 | + * |
| 88 | + * await persister.save(); |
| 89 | + * console.log( |
| 90 | + * await new Promise((resolve) => |
| 91 | + * db.all('SELECT * FROM pets;', (_, rows) => resolve(rows)), |
| 92 | + * ), |
| 93 | + * ); |
| 94 | + * // -> [{_id: 'fido', species: 'dog'}] |
| 95 | + * |
| 96 | + * await new Promise((resolve) => |
| 97 | + * db.all( |
| 98 | + * `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`, |
| 99 | + * resolve, |
| 100 | + * ), |
| 101 | + * ); |
| 102 | + * await persister.load(); |
| 103 | + * console.log(store.getTables()); |
| 104 | + * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}} |
| 105 | + * |
| 106 | + * persister.destroy(); |
| 107 | + * ``` |
| 108 | + * @category Creation |
| 109 | + */ |
| 110 | +/// createExpoSqlitePersister |
0 commit comments