TinyBase

createSessionPersister

The createSessionPersister function creates an Persister object that can persist the Store to the browser's session storage.

createSessionPersister(
  store: Store,
  storageName: string,
): Persister
TypeDescription
storeStore

The Store to persist.

storageNamestring

The unique key to identify the storage location.

returnsPersister

A reference to the new Persister object.

As well as providing a reference to the Store to persist, you must provide a storageName parameter which is unique to your application. This is the key that the browser uses to identify the storage location.

Example

This example creates a Persister object and persists the Store to 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();