TinyBase logoTinyBase β

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
TypeDescription
storeStore | MergeableStore

The Store or MergeableStore to persist.

filePathstring

The location of the local file to persist the Store to.

onIgnoredError?(error: any) => void

An optional handler for the errors that the Persister would otherwise ignore when trying to save or load data. This is suitable for debugging persistence issues in a development environment, since v4.0.4.

returnsFilePersister

A reference to the new FilePersister object.

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