@skaile/forge-editor-directus
v0.1.1
Published
Directus storage for @skaile/forge-editor: documents, collaboration state and per-user settings over the Directus REST items API (fetch only, no Directus SDK).
Readme
@skaile/forge-editor-directus
The storage of @skaile/forge-editor in a Directus instance: documents (TipTap JSON with a content version), the collaboration server's Yjs state and per-user editor settings, over the Directus REST items API. The transport is fetch; there is no dependency on @directus/sdk.
import { createDirectusAdapter } from '@skaile/forge-editor-directus'
const ports = createDirectusAdapter({
url: 'http://localhost:8055',
token: process.env.DIRECTUS_TOKEN!,
authenticate: async ({ headers }) => userOfSession(headers), // null = not signed in
})
// provideEditorServer({ ports: { ...ports, models, index, ... }, blockTypes })createDirectusAdapter returns Omit<StoredPorts, 'assets'> (see @skaile/forge-editor/adapters): identity, directory, documents, collabState, settings. The identity hooks (authenticate, authorize, users) behave as in createJsonAdapter: without authenticate there is a development user outside NODE_ENV=production and nobody in production.
documents:storeContentonly updates (create withputDocument, a missing document is not an error); every write sets a new randomversionandupdated_at,contentVersionreturns it.deleteDocumentremoves content and collaboration state. There is nolistTree/createDocument/moveDocument, so the document-tree routes answer 501.assets: none, the image routes answer 501.- JSON is written as text and read both ways, so it works whether the collection's
jsonfields are registered in Directus or not.
Collections
Three collections with these columns (SQLite, in the style of Directus' own JSON columns); the adapter does not create them:
CREATE TABLE editor_documents (id TEXT PRIMARY KEY NOT NULL, content JSON NOT NULL, version TEXT NOT NULL, updated_at TEXT NOT NULL);
CREATE TABLE editor_states (id TEXT PRIMARY KEY NOT NULL, state TEXT NOT NULL); -- the state bytes as base64
CREATE TABLE editor_settings (user_id TEXT PRIMARY KEY NOT NULL, json JSON NOT NULL);Other names: collections: { documents, states, settings }. The token needs full access to the three collections; an admin token works, and the tables need no registration in Directus (untracked tables are reachable through /items for admins). Directus 11 answers GET /items/<c>/<id> for a missing item with 403, not 404, and PATCH of a missing item with 204 and no body, so the adapter never relies on a status to detect a missing item: it reads through GET /items/<c>?filter[<key>][_eq]=<id>&limit=1 (empty list = missing) and reads a PATCH answer without data as "not updated" before it creates the row. A 403 on a list read stays an error (token without access).
Payload limit
Directus caps request bodies at 1 MB by default (MAX_PAYLOAD_SIZE). The adapter writes a document's content and a collaboration state in one request each, so a host with large documents must raise the limit, e.g. MAX_PAYLOAD_SIZE=50mb. Otherwise Directus answers 413 and the write (storeContent, putDocument, storeState) throws.
Transport
restItemsClient({ url, token, fetch? }) implements DirectusItemsClient (readOne is a filtered list query, empty → null; readMany with limit=-1; updateOne PATCH with fields=<key>, 204/404/no data → false; createOne; deleteOne 204/404 is fine; any other non-2xx throws). Ids are URL-encoded (gutachten:<uuid>). Pass client instead to use your own transport.
