|
1 | 1 | /** |
2 | 2 | * The persister-browser module of the TinyBase project lets you save and load |
3 | | - * Store data to and from browser storage. |
| 3 | + * Store data to and from browser storage, including the origin private file |
| 4 | + * system (OPFS). |
4 | 5 | * |
5 | | - * Two entry points are provided, each of which returns a new Persister object |
| 6 | + * Three entry points are provided, each of which returns a new Persister object |
6 | 7 | * that can load and save a Store: |
7 | 8 | * |
8 | 9 | * - The createSessionPersister function returns a Persister that uses the |
9 | 10 | * browser's session storage. |
10 | 11 | * - The createLocalPersister function returns a Persister that uses the |
11 | 12 | * browser's local storage. |
| 13 | + * - The createOpfsPersister function returns a Persister that uses a file in |
| 14 | + * an origin private file system (OPFS). |
12 | 15 | * @see Persistence guides |
13 | 16 | * @packageDocumentation |
14 | 17 | * @module persister-browser |
|
95 | 98 | */ |
96 | 99 | /// LocalPersister.getStorageName |
97 | 100 | } |
| 101 | +/** |
| 102 | + * The OpfsPersister interface represents a Persister that lets you save and |
| 103 | + * load Store data to and from a file in an origin private file system (OPFS). |
| 104 | + * |
| 105 | + * You should use the createOpfsPersister function to create an OpfsPersister |
| 106 | + * object. |
| 107 | + * |
| 108 | + * It is a minor extension to the Persister interface and simply provides an |
| 109 | + * extra getHandle method for accessing the file the Store is being |
| 110 | + * persisted to. |
| 111 | + * @category Persister |
| 112 | + * @since v6.7.0 |
| 113 | + */ |
| 114 | +/// OpfsPersister |
| 115 | +{ |
| 116 | + /** |
| 117 | + * The getHandle method returns the handle of the file the Store is being |
| 118 | + * persisted to. |
| 119 | + * @returns The handle of the file. |
| 120 | + * @example |
| 121 | + * This example creates a Persister object against a newly-created Store and |
| 122 | + * then gets the file handle back out again. |
| 123 | + * |
| 124 | + * ```js |
| 125 | + * import {createStore} from 'tinybase'; |
| 126 | + * import {createOpfsPersister} from 'tinybase/persisters/persister-browser'; |
| 127 | + * |
| 128 | + * const opfs = await navigator.storage.getDirectory(); |
| 129 | + * const handle = await opfs.getFileHandle('tinybase.json', {create: true}); |
| 130 | + * |
| 131 | + * const store = createStore().setTables({pets: {fido: {species: 'dog'}}}); |
| 132 | + * const persister = createOpfsPersister(store, handle); |
| 133 | + * |
| 134 | + * console.log(persister.getHandle().name); |
| 135 | + * // -> 'tinybase.json' |
| 136 | + * |
| 137 | + * await persister.destroy(); |
| 138 | + * ``` |
| 139 | + * @category Getter |
| 140 | + * @since v6.7.0 |
| 141 | + */ |
| 142 | + /// OpfsPersister.getHandle |
| 143 | +} |
98 | 144 | /** |
99 | 145 | * The createSessionPersister function creates a SessionPersister object that |
100 | 146 | * can persist the Store to the browser's session storage. |
|
171 | 217 | * @since v1.0.0 |
172 | 218 | */ |
173 | 219 | /// createLocalPersister |
| 220 | +/** |
| 221 | + * The createOpfsPersister function creates an OpfsPersister object that can |
| 222 | + * persist the Store to a file in an origin private file system (OPFS). |
| 223 | + * |
| 224 | + * An OpfsPersister supports both regular Store and MergeableStore objects. |
| 225 | + * |
| 226 | + * As well as providing a reference to the Store to persist, you must provide a |
| 227 | + * `handle` parameter which identifies an existing OPFS file to persist it to. |
| 228 | + * @param store The Store or MergeableStore to persist. |
| 229 | + * @param handle The handle of an existing OPFS file to persist the Store to. |
| 230 | + * @param onIgnoredError An optional handler for the errors that the Persister |
| 231 | + * would otherwise ignore when trying to save or load data. This is suitable for |
| 232 | + * debugging persistence issues in a development environment. |
| 233 | + * @returns A reference to the new OpfsPersister object. |
| 234 | + * @example |
| 235 | + * This example creates an OpfsPersister object and persists the Store to a |
| 236 | + * local file. |
| 237 | + * |
| 238 | + * ```js |
| 239 | + * import {createStore} from 'tinybase'; |
| 240 | + * import {createOpfsPersister} from 'tinybase/persisters/persister-browser'; |
| 241 | + * |
| 242 | + * const opfs = await navigator.storage.getDirectory(); |
| 243 | + * const handle = await opfs.getFileHandle('tinybase.json', {create: true}); |
| 244 | + * |
| 245 | + * const store = createStore().setTables({pets: {fido: {species: 'dog'}}}); |
| 246 | + * const persister = createOpfsPersister(store, handle); |
| 247 | + * |
| 248 | + * await persister.save(); |
| 249 | + * // Store JSON will be saved to the file. |
| 250 | + * |
| 251 | + * await persister.load(); |
| 252 | + * // Store JSON will be loaded from the file. |
| 253 | + * |
| 254 | + * await persister.destroy(); |
| 255 | + * ``` |
| 256 | + * @category Creation |
| 257 | + * @since v6.7.0 |
| 258 | + */ |
| 259 | +/// createOpfsPersister |
0 commit comments