createOpfsPersister
The createOpfsPersister function creates an OpfsPersister object that can persist the Store to a file in an origin private file system (OPFS).
createOpfsPersister(
store: Store | MergeableStore,
handle: FileSystemFileHandle,
onIgnoredError?: (error: any) => void,
): OpfsPersister| Type | Description | |
|---|---|---|
store | Store | MergeableStore | The |
handle | FileSystemFileHandle | The handle of an existing OPFS file to persist the |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
| returns | OpfsPersister | A reference to the new |
An OpfsPersister supports both regular Store and MergeableStore objects.
As well as providing a reference to the Store to persist, you must provide a handle parameter which identifies an existing OPFS file to persist it to.
Example
This example creates an OpfsPersister object and persists the Store to a local file.
import {createStore} from 'tinybase';
import {createOpfsPersister} from 'tinybase/persisters/persister-browser';
const opfs = await navigator.storage.getDirectory();
const handle = await opfs.getFileHandle('tinybase.json', {create: true});
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createOpfsPersister(store, handle);
await persister.save();
// Store JSON will be saved to the file.
await persister.load();
// Store JSON will be loaded from the file.
await persister.destroy();
Since
v6.7.0