TinyBase logoTinyBase β

createSessionPersister

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

createSessionPersister(
  store: Store | MergeableStore,
  storageName: string,
  onIgnoredError?: (error: any) => void,
): SessionPersister
TypeDescription
storeStore | MergeableStore

The Store or MergeableStore to persist.

storageNamestring

The unique key to identify the storage location.

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.

returnsSessionPersister

A reference to the new SessionPersister object.

A SessionPersister supports both regular Store and MergeableStore objects.

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 SessionPersister object and persists the Store to the browser's session storage.

import {createSessionPersister} from 'tinybase/persisters/persister-browser';
import {createStore} from 'tinybase';

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

Since

v1.0.0