Collections
host.collection(name): list, get, create, update and delete the typed records the extension declares, the same records the assistant's tools see.
A collection is a list of typed records the extension declares in its manifest (collections, see
manifest.md). The host stores them, validates every write against the declared schema, gives the
assistant five tools for each collection, and records an event for every change. The extension's view reads and
writes the same records through host.collection.
This is where an extension's records go: every kind of thing it stores is a collection, and each collection is a
table of its own on the server. host.storage is only for small values like settings and a selection (see
storage.md).
interface SpacemixHost {
collection<T extends object = Record<string, unknown>>(name: string): CollectionApi<T>;
}
interface CollectionApi<T extends object = Record<string, unknown>> {
list(options?: ListOptions): Promise<(T & RecordBase)[]>;
get(id: string): Promise<(T & RecordBase) | undefined>;
create(fields: T): Promise<T & RecordBase>;
update(id: string, patch: Partial<T>): Promise<T & RecordBase>;
delete(id: string): Promise<void>;
}
interface RecordBase {
id: string;
createdAt: number; // ms since the epoch
updatedAt: number;
}
interface ListOptions {
query?: string;
where?: Record<string, unknown>;
limit?: number;
}
Permission: none. An extension reaches only the collections its own manifest declares. There is no API for
another extension's collections (the data:<ns>:read and data:<ns>:write permissions are declared in the
Permission type but grant nothing today; see permissions.md).
Scope. Like storage, records belong to the server: everyone on the team who may use the
extension sees the same records. The server refuses them to anyone else with You don't have access to <id>. (see
Who can use an extension). While a paid extension is turned off on the
server, create, update and delete reject with <Name> is turned off on this server until it's paid for.; its
records stay readable and kept (see Turned off until it's paid for).
In a React view, use useCollection(name) from @spacemix/sdk/react, which adds live items on top of this API
(see react.md).
host.collection
collection<T extends object = Record<string, unknown>>(name: string): CollectionApi<T>;
Returns the API for one declared collection. Throws synchronously with
<extension id> declares no collection "<name>" when the manifest doesn't declare it. The same object is returned
for the same name. T describes the declared fields only; id, createdAt and updatedAt come from
RecordBase.
Validation
Every create, update and list({ where }) is checked against the collection's schema:
- Only declared fields are accepted. Anything else fails with
unknown field(s) "x"; allowed: a, b, c. Never declare or passid,createdAt,updatedAt. requiredfields must be present oncreate.updateandwhereaccept any subset.- Types are checked (
string,numberfinite,integer,boolean,array,object, andenum). The host is lenient where models slip:"true"/"false"become booleans, numeric strings become numbers, and a comma-separated string becomes an array forarrayfields. nullorundefinedfor a field counts as absent.- On
create, fields left out that have adefaultin the schema get it.
Problems are joined into one message, for example "title" is required; "done" must be true or false.
Each write is applied atomically on the server, one record at a time, so a click, an assistant tool call and another person's edit never lose each other's change.
list
list(options?: ListOptions): Promise<(T & RecordBase)[]>;
Records, newest first by updatedAt, then createdAt, then id.
where: exact match on each given field, after validation (so{ done: "false" }matchesfalse). Arrays and objects never match bywhere.query: case-insensitive substring match on the collection'ssearchFields(default: everystringfield).limit: at most this many. Omitted or0: all of them. (The assistant's<ns>_<collection>_listtool defaults to 20; this API has no default.)
Bridge method: collection.list with the collection name first, then the options.
get
get(id: string): Promise<(T & RecordBase) | undefined>;
The record, or undefined when there is none with that id. Bridge method: collection.get.
create
create(fields: T): Promise<T & RecordBase>;
Validates fields, fills in defaults, assigns a new id and createdAt = updatedAt = now, and stores the record
first in the list. Resolves with the stored record. Records the <collection>_created event. Bridge method:
collection.create.
update
update(id: string, patch: Partial<T>): Promise<T & RecordBase>;
Validates patch (any subset of fields), and the server merges it into the record as it is stored now and sets
updatedAt. Two people changing different fields of the same record at the same time both land; for the same
field, the last write wins. Resolves with the whole record after the change. Records <collection>_updated with changed (the names of the fields whose value
changed); a patch that changes nothing records no event. Rejects with
No <itemName> with id "<id>". Use list to find valid ids. when there is no such record. To clear a field, set
it to an empty value the schema allows; null means "leave as is". Bridge method: collection.update.
delete
delete(id: string): Promise<void>;
Removes the record and records <collection>_deleted with the record as it was. Rejects with
No <itemName> with id "<id>". Use list to find valid ids. when there is none. Bridge method:
collection.delete.
Events
Every successful write is recorded as the collection's automatic event, which other extensions with the events
permission receive: <collection>_created, <collection>_updated, <collection>_deleted. The payload is
{ id, ...declared fields } (long strings are cut to fit 16 KB), plus changed for updates; the target is the
record's id. Recording an event never fails the write. See events.md.
The assistant's tools
For each collection the assistant gets <ns>_<collection>_list|get|create|update|delete (filtered by
operations, delete asks for approval unless requireApproval says otherwise). They call this same API, so
what the assistant changes shows in the view live and the other way round. See
giving-the-assistant-data.md.
Limits
Each record is stored on its own and every write touches only that record, so a collection can hold many records. In a browser, each write goes to the server with its default 2 MB body limit, so keep a single record well under 2 MB as JSON; larger blobs belong in files (fs.md) or outside SpaceMIX. There is no limit on the size of a collection as a whole.
How it's stored
Informational, not part of the contract. Each declared collection is its own table in the server's encrypted
database, named <extension id>.<collection> (extension ai.spacemix.tasks, collection items:
ai.spacemix.tasks.items). It has one row per record and one column per declared field. SpaceMIX's own features
keep their tables under plain names (widgets), except the team chat, whose tables are
ai.spacemix.chat.conversations, .messages and .prefs. The server's owner can look at all of them in
Settings → Database.
Because of that, the collection names conversations, messages and prefs are reserved under the extension id
ai.spacemix.chat, in any letter case and with any __ suffix: declaring or using one fails with
ai.spacemix.chat / <name> is reserved for the team chat.
See chat.md.
Example
import { host } from "@spacemix/sdk";
interface Task {
title: string;
done: boolean;
}
export async function completeAll(): Promise<number> {
const tasks = host.collection<Task>("items");
const open = await tasks.list({ where: { done: false } });
for (const t of open) await tasks.update(t.id, { done: true });
return open.length;
}
Stability
Stable. How records are laid out in the database is not part of the contract; use this API or useCollection.