How composition works
Small Apps compose into bigger applications. Write an App that holds your people once, and every App that needs people imports it and builds on it. The Apps run as one application: they share data, react to each other, and each keeps its own rules.
To see what that means, follow one call across two Apps.
One call across two Apps
Section titled “One call across two Apps”The Apps are the two of the Employee onboarding checklists example. people holds the people of a company. onboarding gives each person who joins a checklist, and imports the people App to do it:
app.configure('onboarding');
const people = app.addDependency('people', peopleApp);const person = people.templates.person;addDependency adds the people App to the application and returns a handle on it. people.ts never names the onboarding App.
Hana is an administrator of the people App. Bob is staff of the onboarding App. Hana adds Dan.
-
Hana calls
join.joinis a Mutation of the people App. The script runs the onboarding App, and reaches the App it imports asctx.apps.people.run.ts const person = await as(callers.hana, async (ctx) => {const people = ctx.apps.people;return await people.mutations.join.handler({ name }, people, ctx);}); -
The people App checks its own Grant. It grants
jointo itsadminRole, and Hana holds it. Bob holdsstaff, a Role of the onboarding App. The Runtime refuses him, and nothing is saved.people.ts join.addGrant(admin); -
The people App saves Dan and publishes
joined. Dan is an Item ofprn::people:template/person.joinreturns with the person and the Event saved. The onboarding App has done nothing so far, and Bob sees no checklist.people.ts const join = app.addMutation('join', async (input: JoinInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const item = await workspace.createChild(current.templates.person, {properties: { name: input.name },});await item.events.joined.publish({});return item;});As Hana: Dan joinedAs Bob: sees no checklist -
The onboarding App’s Listener runs. The Listener is declared in
onboarding.ts, on the imported App’s Event. It runs when the queued work runs, afterjoinhas returned. It runs as the system, so Hana needs no Role of the onboarding App.onboarding.ts person.events.joined.addListener('createChecklist', async (_event, joiner, _current, ctx) => {const templates = ctx.apps.onboarding.templates;const created = await joiner.createChild(templates.checklist, {properties: { done: 0, total: titles.length },});const relation = created.relations.person;if (relation.type === 'one') await relation.set(joiner);for (const title of titles) {await created.createChild(templates.task, { properties: { title } });}});joineris Dan. The Listener creates the checklist as a child of Dan, points the checklist’spersonRelation at him, and creates three tasks below the checklist. Each is an Item of the onboarding App, which the Listener reaches asctx.apps.onboarding. -
Bob reads across both Apps.
listandtasksare Queries of the onboarding App, granted tostaff. Bob gets the tasks and the checklist, follows the Relation to Dan, an Item of the people App, and reads his name.As Bob: sees the task "Order a laptop", todoAs Bob: sees the task "Create an email account", todoAs Bob: sees the task "Book a welcome lunch", todoAs Bob: sees Dan's checklist, inProgress, 0 of 3 done
Hana made one call, and two Apps hold new Items. The people App did its own work under its own Grant, and the onboarding App built on the result.
What the Apps share
Section titled “What the Apps share”One Definition
Section titled “One Definition”The Builder puts the root App and every App it imports into one Definition, and the Runtime runs that Definition in each Workspace. Each App is there once, under the name it gave configure. A handler reaches every App under that name in ctx.apps.
await ctx.apps.people.roles.admin.assign({ id: hana.id, templatePrn });await ctx.apps.onboarding.roles.staff.assign({ id: bob.id, templatePrn });The example’s Definition holds three Apps: onboarding, people and the built-in identity. A PRN begins with the name of its App, so Dan’s checklist is an Item of prn::onboarding:template/checklist.
An App that two Apps import is in the Definition once, with one set of Items. What one of the two saves there, the other reads.
The importing App uses an Item of the imported App as it uses its own. It relates to it, creates children below it, and grants it.
checklist.addRelation('person', 'many-to-one', person, { toKey: 'checklists' });The Relation belongs to the onboarding App. A checklist reads it as person, and a person reads the other side as checklists.
const personStaff = person.addRole('staff', everyStaff);person.addGrant(personStaff, Object.values(person.properties));The people App shows a person to its own administrators. This Role and Grant, added by the onboarding App, show a person to staff as well. Each App gives the Item to the people it names: Hana and Bob both find Dan, and a person who holds neither Role does not.
Grants
Section titled “Grants”An operation keeps its Grants wherever the call comes from. In the example, Bob’s staff Role does not open join, and Hana’s admin Role does not open list.
The same holds when a handler of the importing App makes the call. This Mutation is from the App behind the Apps reference: a club App that imports a members App. The club grants induct to its coaches, and the members App grants enroll to its registrars.
const induct = app.addMutation('induct', async (input: NameInput, current, ctx) => { const workspace = await ctx.getWorkspaceItem(); const item = await workspace.createChild(current.templates.session, { properties: { title: `First session of ${input.name}` } }); const members = ctx.apps.members; const lead = await members.mutations.enroll.handler(input, members, ctx); const relation = item.relations.lead; if (relation.type === 'one') await relation.set(lead); return item;});For a coach who is also a registrar, the call saves the session and the new member. For a coach who is not, enroll refuses, induct throws, and the session it created before that call is not saved.
App Hooks
Section titled “App Hooks”An App Hook runs when a Workspace is created, and the hooks of an imported App run with those of the App that imports it. This one belongs to the members App:
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 }] });});The hook runs where the members App is the root App, and where the club imports it. Either way, a Workspace made with harness.createWorkspace() starts with the founder.
The hooks of all the Apps form one chain. await prev() runs the hooks that follow, those of the imported Apps among them, so the place of that call in a hook decides which runs first. A hook that returns without that call stops the chain.
Who reaches what
Section titled “Who reaches what”| Who | Reaches | Through |
|---|---|---|
| A handler of the importing App | The imported App’s Templates and Items, its operations, its Roles and its Events | The handle that addDependency returns, where the App is declared. ctx.apps, under the imported App’s name, where the handler runs |
| A handler of the imported App | Its own App, and the root App | current, which is its own App also where another App imports it, and ctx.root |
| A person | An operation of either App | A Grant on the operation that names a Role the person holds |
| A person | An Item of either App | A Role and a Grant on its Template, added by either App |
A Mutation or a Query runs as the person who made the call, in either App: it gets the Items that person’s Grants give, and it calls the operations that person’s Roles open. A Listener of a published Event runs as the system.

