createFilePersister
The createFilePersister
function creates a FilePersister
object that can persist the Store
to a local file.
createFilePersister(
store: Store | MergeableStore,
filePath: string,
onIgnoredError?: (error: any) => void,
): FilePersister
Type | Description | |
---|---|---|
store | Store | MergeableStore | The |
filePath | string | The location of the local file to persist the |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
returns | FilePersister | A reference to the new |
A FilePersister
supports both regular Store
and MergeableStore
objects.
As well as providing a reference to the Store
to persist, you must provide a filePath
parameter which identifies the file to persist it to.
Example
This example creates a FilePersister
object and persists the Store
to a local file.
import {createFilePersister} from 'tinybase/persisters/persister-file';
import {createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createFilePersister(store, '/app/persisted.json');
await persister.save();
// Store JSON will be saved to the file.
await persister.load();
// Store JSON will be loaded from the file.
persister.destroy();
Since
v1.0.0