createRemotePersister

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

createRemotePersister(
  store: Store,
  loadUrl: string,
  saveUrl: string,
  autoLoadIntervalSeconds: number,
): Persister
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.

autoLoadIntervalSecondsnumber

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

returnsPersister

A reference to the new Persister object.

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. The autoLoadIntervalSeconds method is used to indicate how often to do this.

Example

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

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