tinybase 6.6.0 → 6.7.0

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.
@@ -13,6 +13,7 @@
13
13
  * |-|-|-|-|
14
14
  * |SessionPersister|Browser session storage|Yes|Yes
15
15
  * |LocalPersister|Browser local storage|Yes|Yes
16
+ * |OpfsPersister|Browser origin private file system (OPFS)|Yes|Yes
16
17
  * |FilePersister|Local file (where possible)|Yes|Yes
17
18
  * |IndexedDbPersister|Browser IndexedDB|Yes|No
18
19
  * |RemotePersister|Remote server|Yes|No
@@ -1,14 +1,17 @@
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
@@ -185,3 +188,92 @@ export function createLocalPersister(
185
188
  storageName: string,
186
189
  onIgnoredError?: (error: any) => void,
187
190
  ): LocalPersister;
191
+
192
+ /**
193
+ * The OpfsPersister interface represents a Persister that lets you save and
194
+ * load Store data to and from a file in an origin private file system (OPFS).
195
+ *
196
+ * You should use the createOpfsPersister function to create an OpfsPersister
197
+ * object.
198
+ *
199
+ * It is a minor extension to the Persister interface and simply provides an
200
+ * extra getHandle method for accessing the file the Store is being
201
+ * persisted to.
202
+ * @category Persister
203
+ * @since v6.7.0
204
+ */
205
+ export interface OpfsPersister
206
+ extends Persister<Persists.StoreOrMergeableStore> {
207
+ /**
208
+ * The getHandle method returns the handle of the file the Store is being
209
+ * persisted to.
210
+ * @returns The handle of the file.
211
+ * @example
212
+ * This example creates a Persister object against a newly-created Store and
213
+ * then gets the file handle back out again.
214
+ *
215
+ * ```js
216
+ * import {createStore} from 'tinybase';
217
+ * import {createOpfsPersister} from 'tinybase/persisters/persister-browser';
218
+ *
219
+ * const opfs = await navigator.storage.getDirectory();
220
+ * const handle = await opfs.getFileHandle('tinybase.json', {create: true});
221
+ *
222
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
223
+ * const persister = createOpfsPersister(store, handle);
224
+ *
225
+ * console.log(persister.getHandle().name);
226
+ * // -> 'tinybase.json'
227
+ *
228
+ * await persister.destroy();
229
+ * ```
230
+ * @category Getter
231
+ * @since v6.7.0
232
+ */
233
+ getHandle(): FileSystemFileHandle;
234
+ }
235
+
236
+ /**
237
+ * The createOpfsPersister function creates an OpfsPersister object that can
238
+ * persist the Store to a file in an origin private file system (OPFS).
239
+ *
240
+ * An OpfsPersister supports both regular Store and MergeableStore objects.
241
+ *
242
+ * As well as providing a reference to the Store to persist, you must provide a
243
+ * `handle` parameter which identifies an existing OPFS file to persist it to.
244
+ * @param store The Store or MergeableStore to persist.
245
+ * @param handle The handle of an existing OPFS file to persist the Store to.
246
+ * @param onIgnoredError An optional handler for the errors that the Persister
247
+ * would otherwise ignore when trying to save or load data. This is suitable for
248
+ * debugging persistence issues in a development environment.
249
+ * @returns A reference to the new OpfsPersister object.
250
+ * @example
251
+ * This example creates an OpfsPersister object and persists the Store to a
252
+ * local file.
253
+ *
254
+ * ```js
255
+ * import {createStore} from 'tinybase';
256
+ * import {createOpfsPersister} from 'tinybase/persisters/persister-browser';
257
+ *
258
+ * const opfs = await navigator.storage.getDirectory();
259
+ * const handle = await opfs.getFileHandle('tinybase.json', {create: true});
260
+ *
261
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
262
+ * const persister = createOpfsPersister(store, handle);
263
+ *
264
+ * await persister.save();
265
+ * // Store JSON will be saved to the file.
266
+ *
267
+ * await persister.load();
268
+ * // Store JSON will be loaded from the file.
269
+ *
270
+ * await persister.destroy();
271
+ * ```
272
+ * @category Creation
273
+ * @since v6.7.0
274
+ */
275
+ export function createOpfsPersister(
276
+ store: Store | MergeableStore,
277
+ handle: FileSystemFileHandle,
278
+ onIgnoredError?: (error: any) => void,
279
+ ): OpfsPersister;
@@ -1,14 +1,17 @@
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
@@ -103,6 +106,50 @@ export interface LocalPersister<Schemas extends OptionalSchemas>
103
106
  getStorageName(): string;
104
107
  }
105
108
 
109
+ /**
110
+ * The OpfsPersister interface represents a Persister that lets you save and
111
+ * load Store data to and from a file in an origin private file system (OPFS).
112
+ *
113
+ * You should use the createOpfsPersister function to create an OpfsPersister
114
+ * object.
115
+ *
116
+ * It is a minor extension to the Persister interface and simply provides an
117
+ * extra getHandle method for accessing the file the Store is being
118
+ * persisted to.
119
+ * @category Persister
120
+ * @since v6.7.0
121
+ */
122
+ export interface OpfsPersister<Schemas extends OptionalSchemas>
123
+ extends Persister<Schemas, Persists.StoreOrMergeableStore> {
124
+ /**
125
+ * The getHandle method returns the handle of the file the Store is being
126
+ * persisted to.
127
+ * @returns The handle of the file.
128
+ * @example
129
+ * This example creates a Persister object against a newly-created Store and
130
+ * then gets the file handle back out again.
131
+ *
132
+ * ```js
133
+ * import {createStore} from 'tinybase';
134
+ * import {createOpfsPersister} from 'tinybase/persisters/persister-browser';
135
+ *
136
+ * const opfs = await navigator.storage.getDirectory();
137
+ * const handle = await opfs.getFileHandle('tinybase.json', {create: true});
138
+ *
139
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
140
+ * const persister = createOpfsPersister(store, handle);
141
+ *
142
+ * console.log(persister.getHandle().name);
143
+ * // -> 'tinybase.json'
144
+ *
145
+ * await persister.destroy();
146
+ * ```
147
+ * @category Getter
148
+ * @since v6.7.0
149
+ */
150
+ getHandle(): string;
151
+ }
152
+
106
153
  /**
107
154
  * The createSessionPersister function creates a SessionPersister object that
108
155
  * can persist the Store to the browser's session storage.
@@ -208,3 +255,58 @@ export function createLocalPersister<Schemas extends OptionalSchemas>(
208
255
  storageName: string,
209
256
  onIgnoredError?: (error: any) => void,
210
257
  ): LocalPersister<Schemas>;
258
+
259
+ /**
260
+ * The createOpfsPersister function creates an OpfsPersister object that can
261
+ * persist the Store to a file in an origin private file system (OPFS).
262
+ *
263
+ * This has schema-based typing. The following is a simplified representation:
264
+ *
265
+ * ```ts override
266
+ * createOpfsPersister(
267
+ * store: Store | MergeableStore,
268
+ * handle: FileSystemFileHandle,
269
+ * onIgnoredError?: (error: any) => void,
270
+ * ): OpfsPersister;
271
+ * ```
272
+ *
273
+ * An OpfsPersister supports both regular Store and MergeableStore objects.
274
+ *
275
+ * As well as providing a reference to the Store to persist, you must provide a
276
+ * `handle` parameter which identifies an existing OPFS file to persist it to.
277
+ * @param store The Store or MergeableStore to persist.
278
+ * @param handle The handle of an existing OPFS file to persist the Store to.
279
+ * @param onIgnoredError An optional handler for the errors that the Persister
280
+ * would otherwise ignore when trying to save or load data. This is suitable for
281
+ * debugging persistence issues in a development environment.
282
+ * @returns A reference to the new OpfsPersister object.
283
+ * @example
284
+ * This example creates an OpfsPersister object and persists the Store to a
285
+ * local file.
286
+ *
287
+ * ```js
288
+ * import {createStore} from 'tinybase';
289
+ * import {createOpfsPersister} from 'tinybase/persisters/persister-browser';
290
+ *
291
+ * const opfs = await navigator.storage.getDirectory();
292
+ * const handle = await opfs.getFileHandle('tinybase.json', {create: true});
293
+ *
294
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
295
+ * const persister = createOpfsPersister(store, handle);
296
+ *
297
+ * await persister.save();
298
+ * // Store JSON will be saved to the file.
299
+ *
300
+ * await persister.load();
301
+ * // Store JSON will be loaded from the file.
302
+ *
303
+ * await persister.destroy();
304
+ * ```
305
+ * @category Creation
306
+ * @since v6.7.0
307
+ */
308
+ export function createOpfsPersister<Schemas extends OptionalSchemas>(
309
+ store: Store<Schemas> | MergeableStore<Schemas>,
310
+ handle: FileSystemFileHandle,
311
+ onIgnoredError?: (error: any) => void,
312
+ ): OpfsPersister<Schemas>;
@@ -13,6 +13,7 @@
13
13
  * |-|-|-|-|
14
14
  * |SessionPersister|Browser session storage|Yes|Yes
15
15
  * |LocalPersister|Browser local storage|Yes|Yes
16
+ * |OpfsPersister|Browser origin private file system (OPFS)|Yes|Yes
16
17
  * |FilePersister|Local file (where possible)|Yes|Yes
17
18
  * |IndexedDbPersister|Browser IndexedDB|Yes|No
18
19
  * |RemotePersister|Remote server|Yes|No