How Alpyne works
You declare an App: what it stores, what it does and who may do it. The Runtime runs it. To see what that means, follow one call from start to finish: Bob, a manager, approves Alice’s leave request.
The path of one approval
Section titled “The path of one approval”flowchart TD
call(["1. Bob calls approve"]) --> transaction
subgraph transaction ["One transaction"]
direction LR
grant["2. Grant checked"] --> move["3. Request approved"] --> event["4. Event added"]
end
transaction --> commit(["5. Commit"])
commit --> listener["6. Listener runs"]
commit --> metric["7. Metric counts it"]
Steps 2 to 4 run in one transaction, so they all happen or none of them do. Steps 6 and 7 start from the saved Event, after the commit. Bob does not wait for them.
Step by step
Section titled “Step by step”The App is the leave App from the Quickstart, with two Roles and a Metric added. Alice has asked for five days of leave, and her request is pending.
The whole App
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface RequestInput { readonly days: number; readonly reason: string;}
interface ApproveInput { readonly id: string;}
// Stands in for your own code: an email, a chat message, a calendar entry.const notify = async (message: string): Promise<void> => { console.log(message);};
export const leaveApp: AppSpec = (app) => { app.configure('leave');
const employee = app.addRole('employee', { label: 'Employee' }); const manager = app.addRole('manager', { label: 'Manager' }); manager.extend(employee);
const leaveRequest = app.addTemplate('leaveRequest'); const reason = leaveRequest.addProperty('reason', 'string'); const days = leaveRequest.addProperty('days', 'integer'); const staff = leaveRequest.addRole('staff', async () => [employee.prn]); leaveRequest.addGrant(staff, [reason, days]);
const pending = leaveRequest.addState('pending').setDefault(); const approved = leaveRequest.addState('approved'); pending.addTransition('approve', approved);
const approvedEvent = leaveRequest.addEvent('approved'); approvedEvent.addListener('tellRequester', async (_event, item) => { await notify(`Your leave "${item.properties.reason}" is approved`); });
const request = app.addMutation('request', async (input: RequestInput, current, ctx) => { const workspace = await ctx.getWorkspaceItem(); return await workspace.createChild(current.templates.leaveRequest, { properties: { days: input.days, reason: input.reason }, }); }); request.input.addProperty('reason', 'string').mandatory(); request.input.addProperty('days', 'integer').mandatory(); request.output.item(leaveRequest).mandatory(); request.addGrant(employee);
const approve = app.addMutation('approve', async (input: ApproveInput, current) => { const item = await current.templates.leaveRequest.getItem(input.id); if (item === undefined || item.state?.value !== 'pending') { throw new Error('Leave request is not pending'); } const updated = await item.state.transitions.approve.execute({}); await updated.events.approved.publish({}); return updated; }); approve.input.addProperty('id', 'id').mandatory(); approve.output.item(leaveRequest).mandatory(); approve.addGrant(manager);
const approvals = app.addMetric('approvals'); approvals.addEvent(approvedEvent); const byRequest = app.addMetricDimension('request', [leaveRequest]); approvals.addDimension(byRequest, 1, async (event) => event.itemId);};-
The call arrives. Bob calls
approvewith the Identifier of Alice’s request. The Runtime opens a transaction for the call.{ id: '7784dd59-c898-4a3e-9a1c-a31e6aaddc3f' } -
The Runtime checks the Grant. Only a manager may approve:
approve.addGrant(manager);Bob holds the
managerRole, so the call goes ahead. Alice does not. When she tries the same call, the Runtime refuses it before your handler runs, and her request stayspending:Grant assertion failed for the current principal: none of [prn::leave:role/manager] held -
Your handler approves the request. It loads the request and runs the
approvetransition. The Runtime makes the move only if the request is stillpending.const item = await current.templates.leaveRequest.getItem(input.id);if (item === undefined || item.state?.value !== 'pending') {throw new Error('Leave request is not pending');}const updated = await item.state.transitions.approve.execute({});{id: '7784dd59-c898-4a3e-9a1c-a31e6aaddc3f',properties: { reason: 'Summer holiday', days: 5 },state: { value: 'pending' },state: { value: 'approved' },} -
Your handler publishes the Event. The Runtime adds the Event to the same transaction. No Listener has seen it.
await updated.events.approved.publish({});{id: '01a1d1b6-bb53-750c-90c8-aaa6fb22f9c5',eventPrn: 'prn::leave:template/leaveRequest/event/approved',itemId: '7784dd59-c898-4a3e-9a1c-a31e6aaddc3f',payload: {},} -
The Runtime commits. The approved request and the Event are saved together. If your handler had thrown, neither would be saved. Bob gets the approved request back.
-
The Listener runs. The Runtime picks up the saved Event and runs
tellRequesterin a transaction of its own. The approval is already saved, so a Listener that fails does not undo it.const approvedEvent = leaveRequest.addEvent('approved');approvedEvent.addListener('tellRequester', async (_event, item) => {await notify(`Your leave "${item.properties.reason}" is approved`);});Your leave "Summer holiday" is approved -
The Metric counts the approval. The Runtime reads the same Event and adds one to
approvals. TherequestDimension files the approval under Alice’s request. Your handler never touches the count.const approvals = app.addMetric('approvals');approvals.addEvent(approvedEvent);const byRequest = app.addMetricDimension('request', [leaveRequest]);approvals.addDimension(byRequest, 1, async (event) => event.itemId);approvals: 1

