TinyBase

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,
): Persister
TypeDescription
storeStore

The Store to persist.

filePathstring

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

returnsPersister

A reference to the new Persister object.

As well as providing a reference to the Store to persist, you must provide filePath parameter which identifies the file to persist it to.

Example

This example creates a Persister 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();