TinyBase logoTinyBase

save

The save method takes data from the Store with which the Persister is associated and persists it into storage, once.

save(): Promise<Persister>
returnsPromise<Persister>

A Promise containing a reference to the Persister object.

This method is asynchronous because the persisted data may be on a remote machine or a filesystem. Even for those storage types that are synchronous (like browser storage) it is still recommended that you await calls to this method or handle the return type natively as a Promise.

Example

This example creates a Store with some data, and saves into the browser's session storage.

const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createSessionPersister(store, 'pets');

await persister.save();
console.log(sessionStorage.getItem('pets'));
// -> '[{"pets":{"fido":{"species":"dog"}}},{}]'

persister.destroy();
sessionStorage.clear();