Relatude Help

19 The TypeScript SDK

@relatude/sdk is the client for the CMS API (chapter 17): Relatude.DB's NodeStore and query builder in TypeScript, typed by your model's interfaces. Queries run on the server as the signed-in user, so they return only what that user can read. Writes run through the store's transaction plugins. The CMS interface and its plugins (chapter 20) are built on it.

import { db } from '@relatude/sdk';                      // the data side
  • Connecting — A store needs the API's address and a token. A TokenSession gets the token and keeps it fresh:
  • Describing the model — Describe each node type as an interface named by its CodeName, with the property names exactly as in C#. The interface is for the compiler only: it decides which conditions a query offers, what rows look like and which…
  • Reading nodes — A row is a db.Node<T>: the node's properties, and system fields whose names start with $:
  • Queries — store.query(type) starts a query over a type and its subtypes, and store.queryAll() one over all nodes. The builder is the C# one and writes the same query text. Nothing is sent until the query runs. Queries are…
  • Writing — Every write runs on the server as the user, through NodeStore.Execute, so the store's transaction plugins run as they do for a write from C#. A plugin that gives new pages an address gives one to a page made in…
  • The content tree — The CMS interface's content tree is the page tree RTree, or the relation that the CMS setting Content.Tree names. These methods work on it as the interface does. Each also takes a relation, to work on another tree.
  • Changes from other editors — onChanges(handler) reports the nodes that clients of the API change, as { id, type, kind }, where kind is inserted, updated or deleted. A connection hears about the nodes its user can read, and about every deletion…
  • Processes — A process is work on the server that goes on after the command that started it, such as a copy in the content tree. It belongs to the user who started it, who sees it again at the next sign-in until they dismiss it.
  • Who has a node open — The CMS interface uses collabJoin, collabLive, collabLeave and collabSend to show who else has a node open and to let editors edit it together, and hears the others through onCollabPresence and onCollab. Other clients…
  • Revisions and cultures — To read or write one revision, name it in the context: store.withContext({ revisions: { [id]: revision } }). A node you read says which version it is in $revisionId, $revisionType and $culture, and which site it is in in…
  • Errors, stopping and signing out — A command that fails throws a db.CommandError, with the server's message and a status:
  • Commands of your own — Send a module's own commands (chapter 18) with store.send(type, payload). Payloads and answers are camelCase JSON, so events.cancel, which reads CancelPayload.Id, takes { id }: