TinyBase logoTinyBase

createRemotePersister

The createRemotePersister function creates a RemotePersister object that can persist the Store to a remote server.

createRemotePersister(
  store: Store,
  loadUrl: string,
  saveUrl: string,
  autoLoadIntervalSeconds?: number,
  onIgnoredError?: (error: any) => void,
): RemotePersister
TypeDescription
storeStore

The Store to persist.

loadUrlstring

The endpoint that supports a GET method to load JSON.

saveUrlstring

The endpoint that supports a POST method to save JSON.

autoLoadIntervalSeconds?number

How often to poll the loadUrl when automatically loading changes from the server, defaulting to 5.

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.

returnsRemotePersister

A reference to the new RemotePersister object.

A RemotePersister only supports regular Store objects, and cannot be used to persist the metadata of a MergeableStore.

As well as providing a reference to the Store to persist, you must provide loadUrl and saveUrl parameters. These identify the endpoints of the server that support the GET method (to fetch the Store JSON to load) and the POST method (to send the Store JSON to save) respectively.

For when you choose to enable automatic loading for the Persister (with the startAutoLoad method), it will poll the loadUrl for changes, using the ETag HTTP header to identify if things have changed. The autoLoadIntervalSeconds method is used to indicate how often to do this.

If you are implementing the server portion of this functionality yourself, remember to ensure that the ETag header changes every time the server's Store content does - otherwise changes will not be detected by the client.

Example

This example creates a RemotePersister object and persists the Store to a remote server.

import {createRemotePersister} from 'tinybase/persisters/persister-remote';
import {createStore} from 'tinybase';

const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createRemotePersister(
  store,
  'https://example.com/load',
  'https://example.com/save',
  5,
);

await persister.save();
// Store JSON will be sent to server in a POST request.

await persister.load();
// Store JSON will be fetched from server with a GET request.

persister.destroy();

Since

v1.0.0