Fabric.js Document Engine

Search documentation

Find a page or section

中文

Storage API

View as Markdown

Everything in fabricjs-document-engine/storage, and the adapter contract.

The storage adapter API has two parts: the built-in adapters in fabricjs-document-engine/storage, and the DocumentStorage interface your own adapter implements.

Built-in adapters

function createLocalStorage(options?: LocalStorageOptions): ManagedDocumentStorage

The same as createMemoryStorage, backed by localStorage. Also offers listDocuments() and deleteDocument(id).

function createMemoryStorage(): ManagedDocumentStorage

In-memory storage with revisions, listing and versions. Useful for tests and demos.

function createKeyValueStorage(store: KeyValueStore, prefix: string): ManagedDocumentStorage

The same, on any key-value store that has { read, write, remove, keys }.

NameTypeDefaultDescription
prefixstring—Prefix for every key, so several apps can share one origin.
storageStoragelocalStorageAnother Storage object, such as sessionStorage.

The built-in adapters also have listDocuments() and deleteDocument(id), and support versions.

Checking an adapter

function verifyStorageAdapter(storage: DocumentStorage): Promise<AdapterReport>

Runs the storage contract against your adapter and returns { ok, checks }, so you know it follows the save rules.

See verify an adapter for what each check means.

The DocumentStorage interface

NameTypeDefaultDescription
loadDocument*(id: string): Promise<unknown>—Returns the stored document, or plain Fabric JSON.
saveDocument*(document: FabricDocument, context: SaveContext): Promise<void | SaveResult>—Stores the document. Throw an error with code: 'SAVE_CONFLICT' when the stored revision differs from expectedRevision. Return { revision } if your backend assigns its own number.
NameTypeDefaultDescription
expectedRevision*number | null—The revision this editor last saved or loaded. null when the user chose to overwrite.
signal*AbortSignal—Aborted when another document is opened. Pass it to fetch.

Versions

A storage adapter adds these four methods to support versions:

NameTypeDefaultDescription
saveVersion*(version: DocumentVersion): Promise<void>—Stores a full copy of the document as a version.
listVersions*(documentId: string): Promise<VersionSummary[]>—Version summaries for a document, newest first.
loadVersion*(documentId: string, versionId: string): Promise<DocumentVersion>—Returns one version.
deleteVersion*(documentId: string, versionId: string): Promise<void>—Deletes one version.

Conflicts

function createConflictError(documentId: string, expectedRevision: number, actualRevision: number): DocumentEngineError

A SAVE_CONFLICT error for your storage adapter to throw.

The rules behind this storage adapter API, with a full REST example, are in custom backend.

Storage adapter API | Fabric.js Document Engine