Skip to content

AppBuilder

Declares one App in a composition, whether it is the root App or an imported App.

Call AppBuilder.configure first to give the App its canonical key, then declare data and behavior. Resource keys must start with a lowercase letter, contain only letters and digits, be at most 64 characters long, and be unique within their collection regardless of case. The TTypes mapping supplies generated application-specific types; the Extract helpers select the matching input, output, Item or Template type from that mapping.

TTypes extends AppBuilderTypes = AppBuilderTypes

Generated type mapping of this App.

readonly dependencies: Record<string, DependencyAppBuilder<any>>

Imported Apps, keyed by the local dependency key passed to AppBuilder.addDependency.


readonly events: Record<string, EventBuilder>

App-level Events, keyed by Event key.

DependencyAppBuilder.events


readonly hooks: AppHookDefinition

App Hooks registered with AppBuilder.onCreate and AppBuilder.onSeed.


readonly key: string

Canonical App key set by AppBuilder.configure.

DependencyAppBuilder.key


readonly mutations: Record<string, MutationBuilder>

Mutations, keyed by Mutation key.


readonly prn: string

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

BaseBuilder.prn


readonly queries: Record<string, QueryBuilder>

Queries, keyed by Query key.


readonly roles: Record<string, AppRoleBuilder>

App Roles, keyed by Role key.

DependencyAppBuilder.roles


readonly searches: Record<string, SearchIndexBuilder>

Search indexes, keyed by index key.

DependencyAppBuilder.searches


readonly templates: Record<string, TemplateBuilder>

Templates, keyed by Template key.

DependencyAppBuilder.templates


readonly unions: Record<string, UnionBuilder>

Unions, keyed by Union key.

DependencyAppBuilder.unions

addDependency<TKey>(key, spec): DependencyAppBuilder<any>

Imports another App under a key local to this App and returns its resources.

TKey extends string

Dependency key, used to select the generated type of the imported App.

TKey

Dependency key this App uses for the imported App.

AppSpec | ((builder) => void)

Spec of the App to import.

DependencyAppBuilder<any>

The imported App’s resources, specialized by generated App types when available.

The imported spec supplies its own canonical key through configure. A canonical App is built once per composition, so two importers of the same spec share one builder. Importing the same key twice returns the existing imported App without evaluating the spec again.

Importing the core App

const core = app.addDependency('core', coreSpec);
const employeeProfile = app.addDependency('employeeProfile', employeeProfileSpec);
task.addRelation('assignee', 'many-to-one', employeeProfile.templates.employee);

addEvent(key): EventBuilder

Declares an App-level Event, which the Runtime publishes against the Workspace Item.

string

Event key within this App.

EventBuilder

The Event builder for payload, Listeners, child Events and schedules.

Use Template Events for Events about a particular Item. Declare the payload on the returned builder, then register Listeners and schedules.


addMetric(key): MetricBuilder

Declares a Metric projected from the Events added to it.

string

Metric key within this App.

MetricBuilder

The Metric builder for selecting Events and Dimensions.

Every selected Event contributes one entry to the Metric’s time series, valued by its calculation handler and tagged with the Dimension values resolved for that Event. Query the Metric by interval and Dimension at run time.

Counting deliveries by channel

const deliveries = app.addMetric('deliveries');
deliveries.addEvent(slack.templates.delivery.events.delivered);
deliveries.addDimension(channelDimension, 1, channelDimensionHandler);

addMetricDimension(key, subject): MetricDimensionBuilder

Declares a Dimension that Metrics of this App can be broken down by.

string

Dimension key within this App.

(TemplateBuilder | UnionBuilder)[]

Templates or Unions whose Items are the Dimension values.

MetricDimensionBuilder

The Dimension builder.

A Dimension value is an Item of one of the subject Templates or Unions, for example a country Item when the subject is the country Template. Attach the Dimension to a Metric with MetricBuilder.addDimension, supplying the handler that maps each Event to its Item.

Metrics and projections


addMutation<TKey>(key, handler): MutationBuilder

Declares a Mutation and returns its builder for input, output and Grants.

TKey extends string

Mutation key, used to select generated input and output types.

TKey

Mutation key within this App.

MutationHandler<ExtractMutationInput<TKey, TTypes>, ExtractMutationOutput<TKey, TTypes>, TTypes["WriteAppInstance"], TTypes["WriteRuntimeInstance"]>

Behavior the Runtime invokes when the Mutation is called.

MutationBuilder

The Mutation builder.

The handler receives the parsed input, this App as current and a write context as ctx. Declare input with mutation.input, the result with mutation.output, and which App Roles may call it with mutation.addGrant. With a generated TTypes mapping the handler’s input and result types are selected by key.

Archiving an Item by id

const archive = app.addMutation('archive', async (input, _current, ctx) => {
const item = await ctx.getItem(input.id);
if (!item) throw new Error('Item not found');
return await item.archive();
});
archive.input.addProperty('id', 'id').mandatory();
archive.output.item(task);
archive.addGrant(core.roles.admin);

addMutation<TKey>(key, options, handler): MutationBuilder

Declares a Mutation with shorthand input and output declarations.

TKey extends string

Mutation key, used to select generated input and output types.

TKey

Mutation key within this App.

MutationOptions

Shorthand input and output declarations.

MutationHandler<ExtractMutationInput<TKey, TTypes>, ExtractMutationOutput<TKey, TTypes>, TTypes["WriteAppInstance"], TTypes["WriteRuntimeInstance"]>

Behavior the Runtime invokes when the Mutation is called.

MutationBuilder

The Mutation builder.

The shorthand in options is applied to the Mutation’s input and output before the builder is returned; refine it further through mutation.input and mutation.output.

HandlerOptions for the shorthand forms


addQuery<TKey>(key, handler): QueryBuilder

Declares a Query and returns its builder for input, output and Grants.

TKey extends string

Query key, used to select generated input and output types.

TKey

Query key within this App.

QueryHandler<ExtractQueryInput<TKey, TTypes>, ExtractQueryOutput<TKey, TTypes>, TTypes["ReadAppInstance"], TTypes["ReadRuntimeInstance"]>

Behavior the Runtime invokes when the Query is called.

QueryBuilder

The Query builder.

The handler receives the parsed input, this App as current and a read context as ctx. Declare input with query.input, the result with query.output, and which App Roles may call it with query.addGrant. A Grant permits the call; Item and Property access inside the handler is still checked against the caller.

Listing Items as a cursor

const getFlows = app.addQuery('getFlows', async (_input, current, _ctx) => {
return current.templates.workflow.selectItems().execute();
});
getFlows.output.item(workflow).cursor();
getFlows.addGrant(core.roles.user);

addQuery<TKey>(key, options, handler): QueryBuilder

Declares a Query with shorthand input and output declarations.

TKey extends string

Query key, used to select generated input and output types.

TKey

Query key within this App.

QueryOptions

Shorthand input and output declarations.

QueryHandler<ExtractQueryInput<TKey, TTypes>, ExtractQueryOutput<TKey, TTypes>, TTypes["ReadAppInstance"], TTypes["ReadRuntimeInstance"]>

Behavior the Runtime invokes when the Query is called.

QueryBuilder

The Query builder.

The shorthand in options is applied to the Query’s input and output before the builder is returned; refine it further through query.input and query.output.

HandlerOptions for the shorthand forms


addRole(key, options): AppRoleBuilder

Declares an App Role that principals can be assigned across the whole App.

string

Role key within this App.

AddRoleOptions

Display options for the Role.

AppRoleBuilder

The Role builder, which can extend other App Roles.

App Roles are broad, such as admin or user. Grant them operations with BaseOperationBuilder.addGrant. For access relative to an Item, declare a Template Role with TemplateBuilder.addRole instead.

Two App Roles

const userRole = app.addRole('user', { label: 'User' });
const adminRole = app.addRole('admin', { label: 'Admin' });
adminRole.extend(userRole);

addSearch(key): SearchIndexBuilder

Declares a search index that Items of selected Templates are indexed into.

string

Index key within this App.

SearchIndexBuilder

The search index builder.

The Builder also registers a Union with the same key on this App to hold the indexed Templates, so the key must not collide with a Union key. Add Templates and their indexing handlers with SearchIndexBuilder.addTemplate.


addTemplate<TKey>(key): ExtractTemplate<TKey, TTypes>

Declares a Template for Items belonging to this App and returns its builder.

TKey extends string

Template key, used to select the generated Template builder type.

TKey

Template key within this App.

ExtractTemplate<TKey, TTypes>

The Template builder, specialized by the generated TTypes mapping when available.

The key workspace is reserved for the Workspace Template returned by AppBuilder.getWorkspaceTemplate.

When key is workspace or is not a valid key.

Declaring a Template with Properties

const task = app.addTemplate('task').setDescription('A unit of work assigned to an employee');
task.addProperty('title', 'string', { label: 'Title' }).index();
task.addProperty('dueDate', 'date');

Data model


addUnion(key, templates): UnionBuilder

Groups Templates so that Relations, Events and operation outputs can target any member.

string

Union key within this App.

TemplateBuilder[]

Initial member Templates; pass [] to add every member with addTemplate.

UnionBuilder

The Union builder.

A Union is a membership list, not an inheritance mechanism: it does not give members shared Properties or Grants. Add further members with UnionBuilder.addTemplate.


configure(key): this

Sets the canonical key of this App, used in the Definition and in every resource PRN.

string

Canonical App key.

this

This builder for chaining.

The canonical key is distinct from the local key an importer passes to AppBuilder.addDependency. Call it first in the spec: reading AppBuilder.key or any resource prn before it is set throws.

When key is not a valid key.

Resource names


getWorkspaceTemplate(): TemplateBuilder

Returns the Workspace Template, on which Apps declare Workspace-level Properties and Roles.

TemplateBuilder

One Workspace Template instance is shared by every App in a composition, so Properties an App declares on it are visible to every other App through the Workspace Item. A spec imported by several Apps is evaluated once per importer; guard Workspace declarations with a check such as if (!workspace.properties.timeZones) so a second evaluation does not redeclare a key.

Declaring a Workspace setting

const workspace = app.getWorkspaceTemplate();
if (!workspace.properties.dailyLimit) workspace.addProperty('dailyLimit', 'integer');

Workspaces and access


onCreate(fn): this

Registers an App Hook that runs when a Workspace is created for this App.

AppCreateHookFn

App Hook to run on creation.

this

This builder for chaining.

App Hooks form a chain: each receives prev, which runs the remaining App Hooks, along with this App and a system write context. Await prev() to preserve the chain. App Hooks registered after another on the same App run before it.

Assigning the creating principal a Role

app.onCreate(async (prev, _current, ctx) => {
await prev();
await ctx.apps.core.roles.admin.assign('current');
});

onSeed(fn): this

Registers an App Hook that seeds initial Items when a Workspace is created for this App.

AppSeedHookFn

App Hook to run when seeding.

this

This builder for chaining.

App Hooks form a chain: each receives prev, which runs the remaining App Hooks, along with this App and a system write context whose seed operation persists the Items. Await prev() to preserve the chain.

Seeding a fixed Item

app.onSeed(async (prev, _current, ctx) => {
await prev();
await ctx.seed({ items: [{ id: HOME_ID, key: 'home', properties: {}, templatePrn: home.prn }] });
});

AppSeedHookFn


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


toDefinition(): AppDefinition

Emits the Definition of this App, including its executable handlers.

AppDefinition

The App Definition.

The result is memoized and frozen. The root App’s Definition includes the Workspace Template under the key workspace. Serializing the Definition alone does not retain handler functions.

Definitions and serialization