TinyBase logoTinyBase

getStats

The getStats method provides a set of statistics about the Persister, and is used for debugging purposes.

getStats(): PersisterStats
returnsPersisterStats

A PersisterStats object containing Persister load and save statistics.

The PersisterStats object contains a count of the number of times the Persister has loaded and saved data.

The method is intended to be used during development to ensure your persistence layer is acting as expected, for example.

Example

This example gets the load and save statistics of a Persister object. Remember that the startAutoLoad method invokes an explicit load when it starts, and the startAutoSave method invokes an explicit save when it starts - so those numbers are included in addition to the loads and saves invoked by changes to the Store and to the underlying storage.

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

const store = createStore();
const persister = createSessionPersister(store, 'pets');

await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
await persister.startAutoSave();

store.setTables({pets: {felix: {species: 'cat'}}});
// ...

sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
// -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
// ...

console.log(persister.getStats());
// -> {loads: 2, saves: 2}

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

Since

v1.0.0