Collections and documents

Creating databases and collections, and creating, reading, listing and deleting documents from a backend.

This page covers the data model from a backend: databases and collections, which the server's administration API makes, and documents, which the engine SDK reads and writes. The samples use an sdk created as in the SDK overview. To change a document's content, see Transactions from a backend.

Databases

A database is known by an id of 32 lowercase hexadecimal characters, which its creator draws; the server keeps no name for it. A superuser creates one through the administration API, with its budget group and its number of partitions, which is fixed for its life:

PUT /v1/admin/databases/0123456789abcdef0123456789abcdef
Authorization: Basic …
Content-Type: application/json

{ "budget_group": "default", "partitions": 1 }

Creating it again the same way is done, so a call whose answer was lost can be sent again; another way, or an id that was ever removed, is refused already_exists. GET /v1/admin/databases lists the databases and DELETE /v1/admin/databases/{db} removes one. See Administration for the rest.

On the hosted service, you create databases in the dashboard or with the management API instead.

Collections

A collection is created with a name and its first schema, which becomes its schema version 1, under an id you draw (32 lowercase hexadecimal characters, never used before in the database). It needs the admin permission on the database.

import { serializeSchema } from "@voidhash/mimic-core";
import { BoardDocument } from "./shared";

const response = await fetch(
  `${server}/v1/admin/databases/${databaseId}/collections/${collectionId}`,
  {
    method: "PUT",
    headers: { authorization: `Bearer ${adminKey}`, "content-type": "application/json" },
    body: JSON.stringify({ name: "boards", schema: serializeSchema(BoardDocument.schema) }),
  },
);

Creating it again the same way is done; another name or schema under the same id, or a name in use, is refused already_exists. The name is 1 to 64 characters of letters, digits and - . _ ~.

RequestDoes
GET /v1/admin/databases/{db}/collectionsLists the database's collections
GET /v1/admin/databases/{db}/collections/{c}Reads a collection: its name, state, active version and schema
GET /v1/admin/databases/{db}/collections/{c}/versionsLists its schema versions, with their fingerprints
DELETE /v1/admin/databases/{db}/collections/{c}Removes it, only if it holds no live document

Reading needs read, changing needs admin. A collection's later schema versions come from migrations.

The engine SDK's handles do not check that your primitive matches the collection's schema. Keep the primitive in a module shared with your client, and change it only together with a migration.

Creating documents

createFrom takes the document's input, typed by your schema, and its id, which your application chooses: 1 to 128 characters of letters, digits and - . _ ~ : @.

const boards = sdk.database(databaseId).collection(collectionId, { primitive: BoardDocument });

const outcome = await boards.createFrom("roadmap", [
  { type: "board", name: "Roadmap", children: [] },
]);

console.log(outcome.result); // { kind: "committed", incarnation: 1n, revision: 1n }

The input is encoded and checked against your primitive before any request is made; input that does not fit fails with validation_failed. Fields with defaults can be left out. create(id, value) does the same with a value already in its encoded form.

The answer is the request's outcome: committed, or rejected with a code, such as already_exists when the document exists. A rejection is an answer, not an error; see Transactions from a backend.

Reading a document

const board = await boards.get("roadmap");

board.live; // whether the document exists now
board.snapshot; // Option of the decoded document, typed by your schema
board.value; // Option of the encoded value
board.revision; // the document's revision, a bigint
board.schema; // its schema version

snapshot and value are Effect Options: Option.getOrUndefined(board.snapshot) reads one. A document that was deleted reads as not live, with no value. A document that never existed fails with the code not_found, as does one the credential may not see. A read reflects every write the server has answered.

Listing documents

Listings are ordered by document id and read in pages.

import * as Option from "effect/Option";

// One page at a time.
let after: string | undefined;
do {
  const page = await boards.listPage({ after, limit: 100 });
  for (const listed of page.documents) {
    console.log(listed.id, listed.revision, listed.schema);
  }
  after = Option.getOrUndefined(page.next);
} while (after !== undefined);
// Every document, fetching pages as the loop needs them.
for await (const listed of boards.listAll({ pageSize: 100 })) {
  console.log(listed.id);
}
MethodReturns
listPage({ after?, limit? }){ documents, next }: up to limit live documents (1 to 1,000; 100 by default) after after
listAll({ pageSize? })An AsyncIterable over every live document

Each listed document is { id, incarnation, revision, schema }, without its content: read the ones you need with get. next is the id to continue after, absent on the last page.

A listing is not a snapshot: a document created or deleted while it runs may or may not appear. Listing needs a user or a database key with read; a document token is refused.

Deleting a document

await boards.delete("roadmap");

delete ends the document's live incarnation: it reads as not live, and its sessions see it deleted. Creating it again later starts a new incarnation, and edits made on the old one are rejected incarnation_mismatch. A client that has the deleted document open reads it as absent (exists() is false).