ApiCoreFunctions
Function: collection()
function collection<N, S, L, M>(definition): CollectionDefinition<N, S, L, M>;Defined in: packages/core/src/schema/collection-def.ts:115
A collection definition, kept as written: its validator's type comes along, so querying or writing with it is typed, and its computed fields and contract are typed from its schema, its name and the collections they read.
const todo = collection({
name: 'app.todo',
schema: z.object({ text: z.string() }),
computed: ({ linked }) => {
const thumbs = linked(reaction, 'on').where((r) => r.body.emoji.eq('👍')).count();
return { thumbs, done: thumbs.gte(2) };
},
});
await node.collections.define(space.id, todo);
const { records } = await node.records.query(space.id, { collection: todo });
records[0]?.computed.done; // boolean | nullType Parameters
| Type Parameter | Default type |
|---|---|
N extends string | - |
S extends | JsonSchema | StandardJSONSchemaV1<unknown, unknown> | - |
L extends Readonly<Record<string, LinkDeclaration>> | Record<never, never> |
M extends Readonly<Record<string, CelExpr<unknown> | Literal>> | Record<never, never> |
Parameters
| Parameter | Type |
|---|---|
definition | CollectionDefinition<N, S, L, M> & NoClash<S, M> |
Returns
CollectionDefinition<N, S, L, M>