TinyBase logoTinyBase β

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
TypeDescription
storeMergeableStore

The MergeableStore to persist.

storageDurableObjectStorage

The Durable Object storage to persist the Store to.

storagePrefix?string

An optional prefix to use on the keys in storage, which is useful if you want to ensure the Persister will not affect unrelated Durable Object storage. Defaults to an empty string.

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.

returnsDurableObjectStoragePersister

A reference to the new DurableObjectStoragePersister object.

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