Skip to content

TemplateBuilder

Describes a kind of persisted Item: its Properties, Relations, Events, states and access.

Items of a Template live in the Workspace hierarchy and share one storage model, so a Template needs no storage of its own. Declare Properties, then Relations to other Templates or Unions, then Events, states and Roles as the App needs them.

readonly events: TemplateEventsBuilder

Item-level Events, keyed by Event key.


readonly key: string

Template key within its App.


readonly parent: AppBuilder

App that declares this Template.


readonly prn: string

Platform Resource Name of the declared resource, derived from the App key and resource key.

BaseBuilder.prn


readonly properties: Record<string, JsonPropertyBuilder | TemplatePropertyBuilder>

Declared Properties, keyed by Property key.

TemplatePropertiesBuilder.properties


readonly relations: Record<string, RelationBuilder>

Relations declared from this Template, keyed by Relation key.


readonly roles: Record<string, BaseRoleBuilder>

Template Roles, keyed by Role key.


readonly states: Record<string, StateBuilder>

States of the Item state machine, keyed by state key.

addEvent(key): EventBuilder

Declares an Event that is published against Items of this Template.

string

Event key within the Template.

EventBuilder

The Event builder.

Declare the payload on the returned builder, then register Listeners with EventBuilder.addListener. Publish it at run time through item.events[key].

Declaring and listening to an Event

const completed = task.addEvent('completed');
completed.addListener('notify', async (event, item, current, ctx) => {
await ctx.log('completed', item.id);
});

Handle events


addGrant(role, properties): TemplateGrantBuilder

Grants a Template Role access to the given Properties on Items of this Template.

BaseRoleBuilder

Template Role receiving access.

PropertyBuilder[]

Properties the Role may access.

TemplateGrantBuilder

The Grant builder.

The Role must be one declared on this Template with TemplateBuilder.addRole. Add further Properties with TemplateGrantBuilder.addProperty. Operation Grants and Template Grants are separate: permission to call a Mutation does not bypass Property access.

Granting a Role every Property

template.addGrant(
template.addRole('user', async (item) => [String(item.properties.owner), userRole.prn]),
Object.values(template.properties),
);

Grant access


addParentListener(key, childEvent, handler): EventParentListenerBuilder

Registers a Listener on this Template for an Event published against a descendant Item.

string

Listener key within the Template.

EventBuilder

Event declared on the child Template.

EventParentListener

Listener invoked with the Event, the ancestor Item and the child Item.

EventParentListenerBuilder

The Listener builder.

When the child Event is processed, the Runtime walks up the Item’s ancestors, stopping at the Workspace, and invokes the Listener with each ancestor as parent and the source Item as child. Parent Listeners are registered per App, so a Listener can receive an ancestor of another Template of the same App; check parent.templatePrn when only this Template applies.

Completing a step once its deliveries are delivered

step.addParentListener('delivered', delivery.events.delivered, async (_event, parent, _child, _current, ctx) => {
if (parent.templatePrn !== step.prn) return;
await parent.state?.transitions.complete.execute({});
});

addProperty<TType>(key, type, options?): TType extends keyof TemplatePropertyBuilderMap ? TemplatePropertyBuilderMap[TType] : TemplatePropertyBuilder

Declares a Property of the given primitive, temporal, select or Zod type.

TType extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> | PropertyDefinitionType

Declared type, which selects the returned builder.

string

Property key within the Template.

TType

Data type name or Zod type for the value.

PropertyDefinitionOptions<TType>

Label and format options.

TType extends keyof TemplatePropertyBuilderMap ? TemplatePropertyBuilderMap[TType] : TemplatePropertyBuilder

A select builder for select types, a JSON builder for schemas, otherwise a Property builder.

A string type declares a typed Property; a Zod type declares a json Property whose values that type parses. Property keys follow the resource key rules and may not be id or state. A declaration does not guarantee the value is present or visible in every Item.

When key is not a valid Property key.

Declaring Properties of different types

location.addProperty('code', 'string', { label: 'Code' }).index();
poll.addProperty('fields', zPropertyDefinition.array());
const tier = workspace.addProperty('tier', 'single-select');
tier.addOption('free', {});
tier.addOption('team', {});

Handle incomplete data

TemplatePropertiesBuilder.addProperty


addRelation(key, cardinality, to, options?): RelationBuilder

Declares a Relation of the given cardinality from Items of this Template to Items of to.

string

Relation key on this Template.

RelationCardinality

Cardinality read from this Template to to.

TemplateBuilder | UnionBuilder

Target Template or Union.

RelationOptions

Naming of the reverse side.

RelationBuilder

The Relation builder.

key names the Relation on this side; options.toKey names it on the other side and defaults to key. When the Definition is built, the Relation is registered on every target Template and the build fails if toKey collides with a Relation key already declared there. Call evented() on the result to publish an Event whenever the Relation changes.

A self-referencing manager Relation

employee.addRelation('manager', 'many-to-one', employee, { toKey: 'manages' }).evented();

addRole(key, callback): TemplateRoleBuilder

Declares a Role that is resolved per Item, such as the collaborators of a document.

string

Role key within the Template.

TemplateRoleCallback

Resolver returning principal ids and App Role PRNs for an Item.

TemplateRoleBuilder

The Role builder.

The callback runs when access to an Item is checked and returns the identifiers that hold the Role for that Item. Give the Role Property access with TemplateBuilder.addGrant.


addState(key): StateBuilder

Declares a state in the state machine of this Template’s Items.

string

State key within the Template.

StateBuilder

The state builder.

The first state added is the default until StateBuilder.setDefault names another. Declare transitions between states with StateBuilder.addTransition.

A two-state lifecycle

const active = checklist.addState('active').setDefault();
const archived = checklist.addState('archived');
active.addTransition('archive', archived);

Add states and transitions


setDescription(description): this

Sets the description emitted into the definition and rendered in generated reference and application assets.

string

Human-readable description of the resource.

this

This builder for chaining.

BaseBuilder.setDescription


setExtensible(): TemplateBuilder

Allows a parent Item to define additional Properties for its child Items of this Template.

TemplateBuilder

This builder for chaining.

Extension Properties are stored on the parent Item and merged with the declared Properties when its children are read or written.


toDefinition(): TemplateDefinition

Emits the Definition of this Template.

TemplateDefinition

The Template Definition. Reverse Relations and Union membership are filled in when the whole composition is built.