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| 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 |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
| returns | RemotePersister | A reference to the new |
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 {createStore} from 'tinybase';
import {createRemotePersister} from 'tinybase/persisters/persister-remote';
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.
await persister.destroy();
Since
v1.0.0