Apps, importing, App Hooks
An App is a function that declares, on the Builder it gets, what the App holds and does. An App imports other Apps and uses what they declare, and its App Hooks run when a Workspace is created. Other pages give the entries of the calls that declare Templates, operations, Events, Roles and Metrics, and of ctx.apps, through which a handler reaches each App.
Declare an App
Section titled “Declare an App”app.configure
Section titled “app.configure”app.configure(key: string): AppBuilderapp.key: stringapp.prn: stringapp.setDescription(description: string): AppBuilderGives the App its key: the name by which the Definition and every handler know it. An App is a function that gets a Builder, app, and AppSpec is the type of such a function. Call configure first: app.key is the key and app.prn the PRN of the App, as prn::plans, and reading either before the call throws.
| Argument | Type | Description |
|---|---|---|
key |
string |
A lowercase letter, then letters and digits, 64 characters at most. |
Returns the Builder.
| Modifier | What it declares |
|---|---|
setDescription |
A description of the App, which the Definition holds. Returns the Builder. |
The sample is a whole App: the plans of a club, with one Template. Its parameter has the type of the Builder, AppBuilder.
export const plansApp = (app: AppBuilder) => { app.configure('plans'); app.setDescription('The plans a member of a club joins on');
const plan = app.addTemplate('plan'); plan.addProperty('name', 'string');};Behavior
- The PRN of each Template of an App begins with the PRN of that App: an Item of the Template
planreadsprn::plans:template/planas itstemplatePrn.
See: Root and imported Apps, Compose apps, Tutorial: compose and measure, AppBuilder.configure.
app.addDependency
Section titled “app.addDependency”app.addDependency(key: string, spec: AppSpec): DependencyAppBuilderapp.dependencies: Record<string, DependencyAppBuilder>Imports an App. The imported App is part of the Definition with all that it declares, under the key it gave configure. The call returns it as the importing App reads it, with its Templates, its Roles and its Events. app.dependencies holds each one under the key of its call.
| Argument | Type | Description |
|---|---|---|
key |
string |
The dependency key: a key as for an App, which this App alone uses for the App it imports. A second call with the key returns what the first returned. |
spec |
AppSpec |
The function that declares the App to import. |
In the sample leave imports the App people, and keeps its Template person.
app.configure('leave');
const people = app.addDependency('people', peopleApp); const person = people.templates.person;Behavior
- An imported App runs as it does as the root App: its operations answer the people its Grants name, and its App Hooks run.
- An operation of an imported App checks its own Grants where a handler of the importing App calls it. When it refuses, the call throws and nothing the handler wrote is saved.
- Two Apps that import one App share it: a handler of one reads the Items the other created.
See: Root and imported Apps, Compose apps, Tutorial, AppBuilder.addDependency.
What an import returns
Section titled “What an import returns”imported.templates
Section titled “imported.templates”imported.templates: Record<string, TemplateBuilder>The Templates of an imported App by key, each as addTemplate returns it. The importing App uses one as a Template of its own: as the target of a Relation, in a Role and a Grant, in a Listener on its Events, and as what a Dimension counts by.
In the sample a request of the App leave points at a person of the App people, and employees get the people.
leaveRequest.addRelation('requester', 'many-to-one', person, { toKey: 'leave' });
const staff = leaveRequest.addRole('staff', async () => [employee.prn]); leaveRequest.addGrant(staff, Object.values(leaveRequest.properties));
const colleagues = person.addRole('colleagues', async () => [employee.prn]); person.addGrant(colleagues, Object.values(person.properties));Behavior
- An Item of the imported App reads a Relation that the importing App declares, under its
toKey. - A person gets an Item of an imported Template through a Role and a Grant that the importing App adds to it.
- The Role and the Grant that the imported App declares stay in force: each App gives the Item to the people it names.
See: Root and imported Apps, Tutorial, Employee onboarding checklists, DependencyAppBuilder.templates.
imported.roles
Section titled “imported.roles”imported.roles: Record<string, AppRoleBuilder>The App Roles of an imported App by key, each as app.addRole returns it. The importing App names one as a Role of its own: in the Grant of an operation, in extend on a Role, and by its prn in a Template Role. A handler checks and assigns it through ctx.apps.
In the sample the Role manager extends registrar, a Role of the imported App. A Template Role and two Grants name it.
const registrar = directory.roles.registrar; manager.extend(registrar); const registrars = session.addRole('registrars', async () => [registrar.prn]); session.addGrant(registrars, Object.values(session.properties)); agenda.addGrant(registrar); census.addGrant(registrar);Behavior
- An operation whose Grant names an imported Role answers a person who holds that Role, and refuses a person who holds only Roles of the importing App.
- A person who holds a Role that extends it calls the operations of the imported App and gets its Items.
- A Template Role that names its PRN gives Items to the people who hold it.
See: Roles and Grants, Compose apps, DependencyAppBuilder.roles.
imported.events
Section titled “imported.events”imported.events: Record<string, EventBuilder>The Events of an imported App that are Events of the App, and not of a Template, by key, each as app.addEvent returns it. The importing App adds a Listener of its own to one. The Events of a Template of the imported App are the events of that Template.
In the sample loaded is an Event of the imported App. The Listener welcome creates a session, an Item of the importing App, which it reaches through ctx.apps.
const loaded = directory.events.loaded; loaded.addListener('welcome', async (event, _item, _current, ctx) => { const workspace = await ctx.getWorkspaceItem(); const title = `Welcome, ${String(event.payload.count)} new members`; const template = ctx.apps.club.templates.session; await workspace.createChild(template, { properties: { title } }); });Behavior
- The Listener runs when a handler of the imported App publishes the Event, and when a handler of the importing App publishes it through
ctx.apps. - It has not run when the Mutation that published the Event returns. It runs when the queued work runs.
See: Listeners, parent Listeners and App Hooks, Compose apps, Handle events, DependencyAppBuilder.events.
App Hooks
Section titled “App Hooks”app.onCreate
Section titled “app.onCreate”app.onCreate(fn: AppCreateHookFn): AppBuildercreate( prev: () => Promise<unknown>, current: WriteAppInstance, ctx: SystemWriteRuntimeInstance,): Promise<unknown>Adds an App Hook: a function that runs when a Workspace is created. The second signature is that function, which the Definition holds under create. prev runs the hooks that follow. ctx is the context of a write execution that no Grant limits, with selectEvents.
| Argument | Type | Description |
|---|---|---|
fn |
AppCreateHookFn |
The function to run: the second signature above. |
Returns the Builder. The sample makes the first queue of each Workspace.
app.onCreate(async (prev, current, ctx) => { await prev(); const workspace = await ctx.getWorkspaceItem(); await workspace.createChild(current.templates.queue, { key: 'general', properties: { name: 'General' }, }); });Behavior
- The first call made in a Workspace reads what the hook wrote.
await prev()runs the hooks added to the App before this one, then the hooks of the Apps it imports. What a hook does before that call comes before them, and the rest after them.- Where a hook returns without a call to
prev, those hooks do not run. currentis the App that added the hook, also where another App imports it.
See: Listeners, parent Listeners and App Hooks, Handle events, Support tickets with an SLA, AppBuilder.onCreate.
app.onSeed
Section titled “app.onSeed”app.onSeed(fn: AppSeedHookFn): AppBuilderAdds an App Hook that runs when a Source Workspace is created: a Workspace created with itself as its Source Workspace. The hook is a function as a hook of onCreate is. Save in it, with ctx.seed, the Items that each Workspace made from this one starts with.
| Argument | Type | Description |
|---|---|---|
fn |
AppSeedHookFn |
The function to run, as the hook of onCreate. |
Returns the Builder. In the sample an App seeds the first member of a club.
app.onSeed(async (prev, current, ctx) => { await prev(); const id = ctx.seedId('members/founder'); const templatePrn = current.templates.member.prn; const properties = { name: 'Founder' }; await ctx.seed({ items: [{ id, key: 'founder', properties, templatePrn }] }); });Behavior
- The hooks run for a Workspace that is its own Source Workspace, as the Workspace of
harness.createWorkspace()is, and before the hooks ofonCreate. - A Workspace created from a Source Workspace runs no hook of
onSeed. It starts with a copy of the Items of its Source Workspace. prevchains the hooks ofonSeedas it chains those ofonCreate.
See: Listeners, parent Listeners and App Hooks, A Workspace per customer, AppBuilder.onSeed.
ctx.seed
Section titled “ctx.seed”ctx.seed(options: SeedOptions): Promise<void>options.items: SeedItem[]ctx.seedId(key: string): stringSaves Items in an App Hook, each under a name the other Items of the call use. ctx.seedId makes that name, an Identifier, from a text of your choice. Each Item of items has the members of the table.
| Argument | Type | Description |
|---|---|---|
options |
SeedOptions |
The Items to save, as items. |
| Member | What it sets |
|---|---|
id |
What ctx.seedId gives for the Item. The saved Item has an Identifier of its own: find it by its key, with getItemByKeyPath. |
templatePrn |
The PRN of the Template of the Item. |
properties |
The values of its Properties, by key. |
key |
The key of the Item. |
parentId |
The id of the Item it is a child of. Without it, the Item is a child of the Workspace Item. |
state |
The key of the state it is in. Without it, the Item is in the default state. |
relations |
For a many-to-one Relation of the Template, the id of the Item it leads to, under the key of the Relation. |
isArchived |
true for an Item that is archived. |
In the sample opening is a session below the plan basic, in the state held.
const opening = { id: ctx.seedId('sessions/opening'), key: 'opening', parentId: basic, properties: { title: 'Opening day' }, relations: { plan: basic }, state: 'held', templatePrn, }; await ctx.seed({ items: [opening, trial, first] });Behavior
- Each Item is saved with the key, the parent, the state and the archive mark that the call gives it.
parentIdandrelationsname an Item of the same call, also one that comes after it initems.- An Item that was seeded before stays as it is: a second call with its
idchanges nothing. ctx.seedIdgives one Identifier for one text in every call of a Workspace.- In a call that runs as a person,
seedthrows.
See: Listeners, parent Listeners and App Hooks, Handle events, SystemWriteRuntimeInstance.seed.

