TemplateBuilder
Describes a kind of persisted Item: its Properties, Relations, Events, states and access.
Remarks
Section titled “Remarks”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.
- Data model
- TemplateDefinition for the emitted Definition
- ReadTemplate for the Template inside an execution
Extends
Section titled “Extends”Properties
Section titled “Properties”events
Section titled “events”
readonlyevents:TemplateEventsBuilder
Item-level Events, keyed by Event key.
readonlykey:string
Template key within its App.
parent
Section titled “parent”
readonlyparent:AppBuilder
App that declares this Template.
readonlyprn:string
Platform Resource Name of the declared resource, derived from the App key and resource key.
Inherited from
Section titled “Inherited from”properties
Section titled “properties”
readonlyproperties:Record<string,JsonPropertyBuilder|TemplatePropertyBuilder>
Declared Properties, keyed by Property key.
Inherited from
Section titled “Inherited from”TemplatePropertiesBuilder.properties
relations
Section titled “relations”
readonlyrelations:Record<string,RelationBuilder>
Relations declared from this Template, keyed by Relation key.
readonlyroles:Record<string,BaseRoleBuilder>
Template Roles, keyed by Role key.
states
Section titled “states”
readonlystates:Record<string,StateBuilder>
States of the Item state machine, keyed by state key.
Methods
Section titled “Methods”addEvent()
Section titled “addEvent()”addEvent(
key):EventBuilder
Declares an Event that is published against Items of this Template.
Parameters
Section titled “Parameters”string
Event key within the Template.
Returns
Section titled “Returns”The Event builder.
Remarks
Section titled “Remarks”Declare the payload on the returned builder, then register Listeners with
EventBuilder.addListener. Publish it at run time through item.events[key].
Example
Section titled “Example”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);});addGrant()
Section titled “addGrant()”addGrant(
role,properties):TemplateGrantBuilder
Grants a Template Role access to the given Properties on Items of this Template.
Parameters
Section titled “Parameters”Template Role receiving access.
properties
Section titled “properties”Properties the Role may access.
Returns
Section titled “Returns”The Grant builder.
Remarks
Section titled “Remarks”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.
Example
Section titled “Example”Granting a Role every Property
template.addGrant( template.addRole('user', async (item) => [String(item.properties.owner), userRole.prn]), Object.values(template.properties),);addParentListener()
Section titled “addParentListener()”addParentListener(
key,childEvent,handler):EventParentListenerBuilder
Registers a Listener on this Template for an Event published against a descendant Item.
Parameters
Section titled “Parameters”string
Listener key within the Template.
childEvent
Section titled “childEvent”Event declared on the child Template.
handler
Section titled “handler”Listener invoked with the Event, the ancestor Item and the child Item.
Returns
Section titled “Returns”The Listener builder.
Remarks
Section titled “Remarks”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.
Example
Section titled “Example”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()
Section titled “addProperty()”addProperty<
TType>(key,type,options?):TTypeextends keyofTemplatePropertyBuilderMap?TemplatePropertyBuilderMap[TType] :TemplatePropertyBuilder
Declares a Property of the given primitive, temporal, select or Zod type.
Type Parameters
Section titled “Type Parameters”TType extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> | PropertyDefinitionType
Declared type, which selects the returned builder.
Parameters
Section titled “Parameters”string
Property key within the Template.
TType
Data type name or Zod type for the value.
options?
Section titled “options?”PropertyDefinitionOptions<TType>
Label and format options.
Returns
Section titled “Returns”TType extends keyof TemplatePropertyBuilderMap ? TemplatePropertyBuilderMap[TType] : TemplatePropertyBuilder
A select builder for select types, a JSON builder for schemas, otherwise a Property builder.
Remarks
Section titled “Remarks”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.
Throws
Section titled “Throws”When key is not a valid Property key.
Example
Section titled “Example”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', {});Inherited from
Section titled “Inherited from”TemplatePropertiesBuilder.addProperty
addRelation()
Section titled “addRelation()”addRelation(
key,cardinality,to,options?):RelationBuilder
Declares a Relation of the given cardinality from Items of this Template to Items of to.
Parameters
Section titled “Parameters”string
Relation key on this Template.
cardinality
Section titled “cardinality”Cardinality read from this Template to to.
TemplateBuilder | UnionBuilder
Target Template or Union.
options?
Section titled “options?”Naming of the reverse side.
Returns
Section titled “Returns”The Relation builder.
Remarks
Section titled “Remarks”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.
Example
Section titled “Example”A self-referencing manager Relation
employee.addRelation('manager', 'many-to-one', employee, { toKey: 'manages' }).evented();addRole()
Section titled “addRole()”addRole(
key,callback):TemplateRoleBuilder
Declares a Role that is resolved per Item, such as the collaborators of a document.
Parameters
Section titled “Parameters”string
Role key within the Template.
callback
Section titled “callback”Resolver returning principal ids and App Role PRNs for an Item.
Returns
Section titled “Returns”The Role builder.
Remarks
Section titled “Remarks”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()
Section titled “addState()”addState(
key):StateBuilder
Declares a state in the state machine of this Template’s Items.
Parameters
Section titled “Parameters”string
State key within the Template.
Returns
Section titled “Returns”The state builder.
Remarks
Section titled “Remarks”The first state added is the default until StateBuilder.setDefault names another. Declare transitions between states with StateBuilder.addTransition.
Example
Section titled “Example”A two-state lifecycle
const active = checklist.addState('active').setDefault();const archived = checklist.addState('archived');active.addTransition('archive', archived);setDescription()
Section titled “setDescription()”setDescription(
description):this
Sets the description emitted into the definition and rendered in generated reference and application assets.
Parameters
Section titled “Parameters”description
Section titled “description”string
Human-readable description of the resource.
Returns
Section titled “Returns”this
This builder for chaining.
Inherited from
Section titled “Inherited from”setExtensible()
Section titled “setExtensible()”setExtensible():
TemplateBuilder
Allows a parent Item to define additional Properties for its child Items of this Template.
Returns
Section titled “Returns”TemplateBuilder
This builder for chaining.
Remarks
Section titled “Remarks”Extension Properties are stored on the parent Item and merged with the declared Properties when its children are read or written.
toDefinition()
Section titled “toDefinition()”toDefinition():
TemplateDefinition
Emits the Definition of this Template.
Returns
Section titled “Returns”The Template Definition. Reverse Relations and Union membership are filled in when the whole composition is built.

