Skip to content

Compose apps

Split behavior across Apps by giving each one a canonical name and importing it where it is needed. Use this guide when two Apps share data or when one App reacts to another’s Events.

  • Two App specs, each an AppSpec function, as in Your first application.
  • A Template in the imported App that the importer relates to or listens on.
  1. Name the imported App. In its spec, call app.configure(key) before any other declaration. The key is the App’s canonical name: it appears in every PRN the App owns, such as prn::people:template/person, and it is the key under ctx.apps at run time. Declare the Templates, Roles and Events the importer needs on this App.

    examples/compose-apps/people.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    export const peopleApp: AppSpec = (app) => {
    app.configure('people');
    app.addRole('admin', { label: 'People administrator' });
    const person = app.addTemplate('person');
    person.addProperty('name', 'string');
    person.addProperty('email', 'string');
    person.addEvent('joined');
    };
  2. Import it with addDependency(key, spec). In the root App, the first argument is the dependency key, a name local to the importer; the second is the imported spec. The return value is a DependencyAppBuilder exposing the imported App’s templates, roles, events, unions and searches. Importing the same spec from several Apps yields one shared App in the Definition, never a copy per importer.

    examples/compose-apps/onboarding.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { peopleApp } from './people';
    interface JoinInput {
    readonly email: string;
    readonly name: string;
    }
    interface PersonInput {
    readonly personId: string;
    }
    export const onboardApp: AppSpec = (app) => {
    app.configure('onboarding');
    const people = app.addDependency('directory', peopleApp);
    const checklist = app.addTemplate('checklist');
    checklist.addProperty('title', 'string');
    checklist.addProperty('complete', 'boolean');
    checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });
    people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {
    const name = person.properties.name;
    const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {
    properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },
    });
    const owner = created.relations.owner;
    if (owner.type === 'one') await owner.set(person);
    });
    const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    const person = await ctx.apps.people.templates.person.createItem(workspace, {
    properties: { email: input.email, name: input.name },
    });
    await person.events.joined.publish({});
    return person;
    });
    join.input.addProperty('name', 'string').mandatory();
    join.input.addProperty('email', 'string').mandatory();
    join.output.item(people.templates.person).mandatory();
    join.addGrant(people.roles.admin);
    const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {
    const person = await ctx.apps.people.templates.person.getItem(input.personId);
    const checklists = person?.relations.checklists;
    if (checklists === undefined || checklists.type !== 'many') return undefined;
    return (await checklists.select().first())?.underlyingItem;
    });
    checklistFor.input.addProperty('personId', 'id').mandatory();
    checklistFor.output.item(checklist);
    };
  3. Relate to an imported Template. Pass people.templates.person wherever a Template is expected: as the target of addRelation, as a Union member, or as an operation output. The Relation belongs to the importer, and the imported Template gains the reverse side under toKey.

    examples/compose-apps/onboarding.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { peopleApp } from './people';
    interface JoinInput {
    readonly email: string;
    readonly name: string;
    }
    interface PersonInput {
    readonly personId: string;
    }
    export const onboardApp: AppSpec = (app) => {
    app.configure('onboarding');
    const people = app.addDependency('directory', peopleApp);
    const checklist = app.addTemplate('checklist');
    checklist.addProperty('title', 'string');
    checklist.addProperty('complete', 'boolean');
    checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });
    people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {
    const name = person.properties.name;
    const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {
    properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },
    });
    const owner = created.relations.owner;
    if (owner.type === 'one') await owner.set(person);
    });
    const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    const person = await ctx.apps.people.templates.person.createItem(workspace, {
    properties: { email: input.email, name: input.name },
    });
    await person.events.joined.publish({});
    return person;
    });
    join.input.addProperty('name', 'string').mandatory();
    join.input.addProperty('email', 'string').mandatory();
    join.output.item(people.templates.person).mandatory();
    join.addGrant(people.roles.admin);
    const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {
    const person = await ctx.apps.people.templates.person.getItem(input.personId);
    const checklists = person?.relations.checklists;
    if (checklists === undefined || checklists.type !== 'many') return undefined;
    return (await checklists.select().first())?.underlyingItem;
    });
    checklistFor.input.addProperty('personId', 'id').mandatory();
    checklistFor.output.item(checklist);
    };
  4. Listen on an imported Event. Call addListener(key, handler) on people.templates.person.events.joined. The Listener receives the Event, the Item it was published against, the App that owns the Event, and a write context. Reach your own App through ctx.apps.onboarding to create Items or run Mutations from inside the Listener.

    examples/compose-apps/onboarding.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { peopleApp } from './people';
    interface JoinInput {
    readonly email: string;
    readonly name: string;
    }
    interface PersonInput {
    readonly personId: string;
    }
    export const onboardApp: AppSpec = (app) => {
    app.configure('onboarding');
    const people = app.addDependency('directory', peopleApp);
    const checklist = app.addTemplate('checklist');
    checklist.addProperty('title', 'string');
    checklist.addProperty('complete', 'boolean');
    checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });
    people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {
    const name = person.properties.name;
    const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {
    properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },
    });
    const owner = created.relations.owner;
    if (owner.type === 'one') await owner.set(person);
    });
    const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    const person = await ctx.apps.people.templates.person.createItem(workspace, {
    properties: { email: input.email, name: input.name },
    });
    await person.events.joined.publish({});
    return person;
    });
    join.input.addProperty('name', 'string').mandatory();
    join.input.addProperty('email', 'string').mandatory();
    join.output.item(people.templates.person).mandatory();
    join.addGrant(people.roles.admin);
    const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {
    const person = await ctx.apps.people.templates.person.getItem(input.personId);
    const checklists = person?.relations.checklists;
    if (checklists === undefined || checklists.type !== 'many') return undefined;
    return (await checklists.select().first())?.underlyingItem;
    });
    checklistFor.input.addProperty('personId', 'id').mandatory();
    checklistFor.output.item(checklist);
    };
  5. Grant an imported Role. Pass people.roles.admin to addGrant on an operation so principals holding that Role may call it. A Role keeps its owning App’s PRN, so the same Role secures operations in every App that imports it.

    examples/compose-apps/onboarding.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { peopleApp } from './people';
    interface JoinInput {
    readonly email: string;
    readonly name: string;
    }
    interface PersonInput {
    readonly personId: string;
    }
    export const onboardApp: AppSpec = (app) => {
    app.configure('onboarding');
    const people = app.addDependency('directory', peopleApp);
    const checklist = app.addTemplate('checklist');
    checklist.addProperty('title', 'string');
    checklist.addProperty('complete', 'boolean');
    checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });
    people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {
    const name = person.properties.name;
    const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {
    properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },
    });
    const owner = created.relations.owner;
    if (owner.type === 'one') await owner.set(person);
    });
    const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    const person = await ctx.apps.people.templates.person.createItem(workspace, {
    properties: { email: input.email, name: input.name },
    });
    await person.events.joined.publish({});
    return person;
    });
    join.input.addProperty('name', 'string').mandatory();
    join.input.addProperty('email', 'string').mandatory();
    join.output.item(people.templates.person).mandatory();
    join.addGrant(people.roles.admin);
    const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {
    const person = await ctx.apps.people.templates.person.getItem(input.personId);
    const checklists = person?.relations.checklists;
    if (checklists === undefined || checklists.type !== 'many') return undefined;
    return (await checklists.select().first())?.underlyingItem;
    });
    checklistFor.input.addProperty('personId', 'id').mandatory();
    checklistFor.output.item(checklist);
    };
  6. Reach the imported App at run time through ctx.apps.people. Handler code addresses Apps by canonical name, not by dependency key: ctx.apps.directory does not exist, ctx.apps.people does. Its templates, unions, roles, queries and, in a write execution, mutations and events behave exactly as on current.

    examples/compose-apps/onboarding.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { peopleApp } from './people';
    interface JoinInput {
    readonly email: string;
    readonly name: string;
    }
    interface PersonInput {
    readonly personId: string;
    }
    export const onboardApp: AppSpec = (app) => {
    app.configure('onboarding');
    const people = app.addDependency('directory', peopleApp);
    const checklist = app.addTemplate('checklist');
    checklist.addProperty('title', 'string');
    checklist.addProperty('complete', 'boolean');
    checklist.addRelation('owner', 'many-to-one', people.templates.person, { toKey: 'checklists' });
    people.templates.person.events.joined.addListener('createChecklist', async (_event, person, _current, ctx) => {
    const name = person.properties.name;
    const created = await ctx.apps.onboarding.templates.checklist.createItem(person, {
    properties: { complete: false, title: typeof name === 'string' ? `Welcome ${name}` : 'Welcome' },
    });
    const owner = created.relations.owner;
    if (owner.type === 'one') await owner.set(person);
    });
    const join = app.addMutation('join', async (input: JoinInput, _current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    const person = await ctx.apps.people.templates.person.createItem(workspace, {
    properties: { email: input.email, name: input.name },
    });
    await person.events.joined.publish({});
    return person;
    });
    join.input.addProperty('name', 'string').mandatory();
    join.input.addProperty('email', 'string').mandatory();
    join.output.item(people.templates.person).mandatory();
    join.addGrant(people.roles.admin);
    const checklistFor = app.addQuery('checklistFor', async (input: PersonInput, _current, ctx) => {
    const person = await ctx.apps.people.templates.person.getItem(input.personId);
    const checklists = person?.relations.checklists;
    if (checklists === undefined || checklists.type !== 'many') return undefined;
    return (await checklists.select().first())?.underlyingItem;
    });
    checklistFor.input.addProperty('personId', 'id').mandatory();
    checklistFor.output.item(checklist);
    };

The test below joins a person through the root App’s Mutation, confirms the Item carries the imported App’s PRN, drives the reactor, and reads the checklist the Listener created.

examples/compose-apps/onboarding.test.ts
import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
import { createTestPlatformService } from '@pyn-backend/platform-service-test';
import { createTestCronService } from '@pyn-backend/service-cron-test';
import { createTestSystemService } from '@pyn-backend/service-system-test';
import { createTestLoggerService } from '@pyn-backend/test-services-logger';
import { describe, expect, it } from 'vitest';
import { onboardApp } from './onboarding';
describe('compose apps', () => {
it('reaches the imported App by canonical name and reacts to its Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, onboardApp);
const workspace = await harness.createWorkspace();
// The harness runs every execution as system, so the Grant on `join` is not exercised here.
const personId = await workspace.write(async (ctx) => {
const current = ctx.apps.onboarding;
const person = await current.mutations.join.handler({ email: 'grace@example.com', name: 'Grace Hopper' }, current, ctx);
expect(person.templatePrn).toBe('prn::people:template/person');
return person.id;
});
await workspace.read(async (ctx) => {
const current = ctx.apps.onboarding;
expect(await current.queries.checklistFor.handler({ personId }, current, ctx)).toBeUndefined();
});
await harness.tickAll();
await workspace.read(async (ctx) => {
const current = ctx.apps.onboarding;
const checklist = await current.queries.checklistFor.handler({ personId }, current, ctx);
expect(checklist?.templatePrn).toBe('prn::onboarding:template/checklist');
expect(checklist?.parentId).toBe(personId);
expect(checklist?.properties.title).toBe('Welcome Grace Hopper');
});
expect.assertions(5);
});
});

From the repository root, run:

Terminal window
pnpm run cli test alpynehq/alpyne/website/examples/compose-apps/

You should see:

Test Files 1 passed (1)
Tests 1 passed (1)

The checklist is absent before harness.tickAll() and present after it, because publish appends the Event and the reactor runs the Listener after the write commits. The harness runs as system, so the Grant on join is declared but not exercised by this test.

  • app.addDependency returns the existing builder when the same dependency key is added twice, so a spec may import a shared App from a helper without guarding against repeats.
  • Every Definition has one root App: the spec passed to the harness or to the Runtime. An App configured with the same canonical name as an existing import resolves to that import.
  • setDescription(text) after configure describes the App in generated assets.