createDurableObjectStoragePersister
The createDurableObjectStoragePersister
function creates a DurableObjectStoragePersister
object that can persist the Store
to and from Cloudflare Durable Object storage.
createDurableObjectStoragePersister(
store: MergeableStore,
storage: DurableObjectStorage,
storagePrefix?: string,
onIgnoredError?: (error: any) => void,
): DurableObjectStoragePersister
Type | Description | |
---|---|---|
store | MergeableStore | The |
storage | DurableObjectStorage | The Durable Object storage to persist the |
storagePrefix? | string | An optional prefix to use on the keys in storage, which is useful if you want to ensure the |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
returns | DurableObjectStoragePersister | A reference to the new |
You will mostly use this within the createPersister
method of a WsServerDurableObject
.
A DurableObjectStoragePersister
only supports MergeableStore
objects, and cannot be used to persist a regular Store
.
Durable Objects have limitations on the data that can be stored in each key of their key-value structure. The DurableObjectStoragePersister
uses one key per TinyBase Value
, one key per Cell
, one key per Row
, and one key per Table
. Mostly this is CRDT metadata, but the main caution is to ensure that each individual TinyBase Cell
and Value
data does not exceed the (128 KiB) limit.
As well as providing a reference to the MergeableStore
to persist, you must provide a storage
parameter which identifies the Durable Object storage to persist it to.
Example
This example creates a Persister
object against a newly-created MergeableStore
(within the createPersister
method of a WsServerDurableObject
instance) and then gets the storage reference back out again.
import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
import {createDurableObjectStoragePersister} from 'tinybase/persisters/persister-durable-object-storage';
import {createMergeableStore} from 'tinybase';
export class MyDurableObject extends WsServerDurableObject {
createPersister() {
const store = createMergeableStore();
const persister = createDurableObjectStoragePersister(
store,
this.ctx.storage,
);
return persister;
}
}
Since
v5.4.0