TinyBase logoTinyBase

createOpfsPersister

The createOpfsPersister function creates an OpfsPersister object that can persist the Store to a file in an origin private file system (OPFS).

createOpfsPersister(
  store: Store | MergeableStore,
  handle: FileSystemFileHandle,
  onIgnoredError?: (error: any) => void,
): OpfsPersister
TypeDescription
storeStore | MergeableStore

The Store or MergeableStore to persist.

handleFileSystemFileHandle

The handle of an existing OPFS file to persist the Store to.

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.

returnsOpfsPersister

A reference to the new OpfsPersister object.

An OpfsPersister supports both regular Store and MergeableStore objects.

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

Example

This example creates an OpfsPersister object and persists the Store to a local file.

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

const opfs = await navigator.storage.getDirectory();
const handle = await opfs.getFileHandle('tinybase.json', {create: true});

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

await persister.save();
// Store JSON will be saved to the file.

await persister.load();
// Store JSON will be loaded from the file.

await persister.destroy();

Since

v6.7.0