TinyBase logoTinyBase

createFilePersister

The createFilePersister function creates a Persister object that can persist the Store to a local file (in an appropriate environment).

createFilePersister(
  store: Store,
  filePath: string,
  onIgnoredError?: (error: any) => void,
): FilePersister
TypeDescription
storeStore

The Store 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.

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.

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();