Skip to content

Application model

An App is a unit of TypeScript that declares Templates, operations, Events, Roles and Metrics through an AppBuilder. The Builder evaluates that declaration and produces a Definition: an immutable description of every App in the composition, including its executable handlers. A Runtime loads one Definition and executes it against the Items of one Workspace.

The diagram shows the flow from design time to run time: Apps compose into a Definition, the Definition feeds the compiler and the Runtime, and each Runtime instance is bound to one Workspace.

flowchart LR
  Root[Root App] --> Builder
  Imported[Imported Apps] --> Builder
  Builder --> Definition
  Definition --> Compiler[Compiler: generated types]
  Definition --> Runtime[Runtime: PostgreSQL, SQLite or test]
  Runtime --> Workspace[Workspace]

Every Definition has exactly one root App. The root App imports other Apps with addDependency(key, spec), and each imported App may import Apps of its own. The Builder resolves the whole graph once. An App that two importers share appears once in the Definition, so its Templates hold one set of Items rather than a copy per importer.

Two names identify an imported App. Its canonical name is the name it gives itself with configure; that name appears in PRNs and keys ctx.apps at run time. The dependency key is the local name the importer chooses; it keys app.dependencies and the handle that addDependency returns. In the tutorial App, tasks is the dependency key and tutorialTasks is the canonical name.

examples/task-app.ts
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface TaskInput {
readonly id: string;
}
export const tasksApp: AppSpec = (app) => {
app.configure('tutorialTasks');
const task = app.addTemplate('task');
task.addProperty('title', 'string');
task.addProperty('completionNoted', 'boolean');
const open = task.addState('open').setDefault();
const done = task.addState('done');
open.addTransition('complete', done);
const completed = task.addEvent('completed');
completed.addListener('noteCompletion', async (_event, item) => {
await item.updateProperties({ completionNoted: true });
});
const complete = app.addMutation('complete', async (input: TaskInput, current) => {
const item = await current.templates.task.getItem(input.id);
if (item === undefined) throw new Error('Task not found');
if (item.state?.value !== 'open') return;
const updated = await item.state.transitions.complete.execute({});
await updated.events.completed.publish({});
});
complete.input.addProperty('id', 'id').mandatory();
complete.output.void();
const title = app.addQuery('title', async (input: TaskInput, current) => {
const item = await current.templates.task.getItem(input.id);
const value = item?.properties.title;
return typeof value === 'string' ? value : undefined;
});
title.input.addProperty('id', 'id').mandatory();
title.output.value('string');
};
export const taskApp: AppSpec = (app) => {
app.configure('taskTutorial');
const tasks = app.addDependency('tasks', tasksApp);
app.addMetric('completedTasks').addEvent(tasks.templates.task.events.completed);
};

Handlers reach an imported App through its canonical name: ctx.apps.tutorialTasks.templates.task. The importer’s declaration code reaches it through the handle: tasks.templates.task.events.completed.

toDefinition() on the Builder returns a RuntimeDefinition: the root App’s name, every resolved App keyed by canonical name, the resolution order and the Workspace Template. Each AppDefinition carries its Templates, Unions, Events with their Listeners, Queries, Mutations, Roles, Grants, App Hooks, Metrics and search indexes. The structure is data; the handlers are functions. A Definition is complete only when both travel together, so packaging a Definition means packaging code.

A Runtime takes a Definition and an auth context and returns a PlatformInstance bound to one Workspace. Three Runtimes exist: PostgreSQL, SQLite and the test Runtime. Each provides the same interface: execute for read and write executions, runCreate and runSeed for App Hooks, and publishExternalEvent to append an Event from outside a handler. Application code never depends on which Runtime is running.

A Workspace is the tenant boundary, and a PlatformInstance is bound to one Workspace for its whole life. Creating a Workspace does not copy application code; it selects a Definition and creates the Workspace Item that anchors that Workspace’s Item hierarchy. The root App’s getWorkspaceTemplate() declares the Properties that Workspace Item carries. Workspaces and access covers what the boundary contains and who may act inside it.

PRNs name Definition resources: prn::<app> for an App and prn::<app>:<type>/<path> for every entity it declares. The tutorial’s Template is prn::tutorialTasks:template/task, and its Event nests below it as prn::tutorialTasks:template/task/event/completed. A PRN identifies a declaration; an Identifier identifies one Item, Event or Workspace at run time. The two never substitute for each other.

The Alpyne compiler derives generated types from a Definition: an AppBuilderTypes mapping that gives each Template, Mutation input and Query output its own TypeScript type. A generated AppBuilder<Types> makes current.templates.task.getItem(id) return a typed Item and turns an unknown key into a compile error. The compiler also emits a structural definition.json, an OpenAPI document and typed client contracts. Every derived asset is a view of the Definition; the Definition stays the source of truth.

  • An App is not a deployable service. It has no process, port or storage of its own; a Runtime supplies all three.
  • A Definition is not a serializable document. JSON.stringify keeps its structure and drops its handlers.
  • A Workspace is not a Definition of its own. Many Workspaces run one deployed Definition.
  • A dependency key is not a name the Runtime knows. Handlers address imported Apps by canonical name.