createRemotePersister
The createRemotePersister
function creates an Persister
object that can persist the Store
to a remote server.
createRemotePersister(
store: Store,
loadUrl: string,
saveUrl: string,
autoLoadIntervalSeconds: number,
): Persister
Type | Description | |
---|---|---|
store | Store | The |
loadUrl | string | The endpoint that supports a |
saveUrl | string | The endpoint that supports a |
autoLoadIntervalSeconds | number | How often to poll the |
returns | Persister | 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.
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();