Quickstart
Build a leave-request App and run it on your machine. You write the App in one file, start it on SQLite, approve a request and watch the App react. It takes about five minutes.
Before you start
Section titled “Before you start”You need Node.js 24 and an empty project that runs TypeScript:
mkdir leave-app && cd leave-appnpm init -y && npm pkg set type=modulenpm install --save-dev tsx1. Install Alpyne
Section titled “1. Install Alpyne”npm install @alpynehq/alpyne-types \ @pyn-backend/data-knex-sqlite \ @pyn-backend/platform-harness-public \ @pyn-backend/platform-reactor-sqlite \ @pyn-backend/platform-service-sqlite \ @pyn-backend/service-configuration-test \ @pyn-backend/service-cron-test \ @pyn-backend/service-system-test \ @pyn-backend/test-services-dynamic-config \ @pyn-backend/test-services-loggerYou write an App with @alpynehq/alpyne-types. The other packages run it on SQLite.
2. Define the App
Section titled “2. Define the App”Create leave.ts. This file is the whole App.
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface RequestInput { readonly days: number; readonly reason: string;}
interface ApproveInput { readonly id: string;}
export const leaveApp: AppSpec = (app) => { app.configure('leave');
const leaveRequest = app.addTemplate('leaveRequest'); leaveRequest.addProperty('reason', 'string'); leaveRequest.addProperty('days', 'integer');
const pending = leaveRequest.addState('pending').setDefault(); const approved = leaveRequest.addState('approved'); pending.addTransition('approve', 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();
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({}); return updated; }); approve.input.addProperty('id', 'id').mandatory(); approve.output.item(leaveRequest).mandatory();};It declares three things:
- What it stores. A Template named
leaveRequestwith two Properties,reasonanddays. - How a request moves. Two states,
pendingandapproved, and one transition between them,approve. - What a caller does. Two Mutations:
requestcreates a leave request, andapproveruns the transition.
3. Run it on SQLite
Section titled “3. Run it on SQLite”Create run.ts beside it. It opens a SQLite database, loads your App into the SQLite Runtime and creates a Workspace, the space that holds one customer’s data.
import { createSqliteKnexDatabase } from '@pyn-backend/data-knex-sqlite';import { createHarnessInstance } from '@pyn-backend/platform-harness-public';import { createSqliteInstance } from '@pyn-backend/platform-reactor-sqlite';import { createSqlitePlatformService } from '@pyn-backend/platform-service-sqlite';import { createTestConfigurationService } from '@pyn-backend/service-configuration-test';import { createTestCronService } from '@pyn-backend/service-cron-test';import { createTestSystemService } from '@pyn-backend/service-system-test';import { createTestDynamicConfigService } from '@pyn-backend/test-services-dynamic-config';import { createTestLoggerService } from '@pyn-backend/test-services-logger';
import { leaveApp } from './leave';
// A SQLite database, held in memory, with the Alpyne tables.const knex = await createSqliteKnexDatabase(':memory:');await knex.current.migrate.latest();
// The services a Runtime depends on.const system = createTestSystemService();const services = { configuration: createTestConfigurationService(), cron: createTestCronService({ system }), dynamicConfig: createTestDynamicConfigService(), knex, logger: createTestLoggerService(), system,};
// The SQLite Runtime with your App loaded, and a Workspace to hold its data.const platform = createSqlitePlatformService(services);const reactor = createSqliteInstance({ ...services, platform }, {});const runtime = { ...services, platform, reactor };const harness = await createHarnessInstance(runtime, leaveApp);const workspace = await harness.createWorkspace();console.log(`Leave App is running in Workspace ${workspace.workspaceId}`);
// Your calls go here.
// Finish the work the App has queued, then close the database.await harness.tickAll();await knex.destroy();Run it:
PYN_ENV=local npx tsx run.tsYou should see:
Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469Your App now runs on SQLite, in a Workspace of its own. It holds no leave requests so far.
4. Request leave and approve it
Section titled “4. Request leave and approve it”In run.ts, replace the line // Your calls go here. with two calls. Each one runs a Mutation and prints the leave request it returns.
// Request five days of leave.const request = await workspace.write(async (ctx) => { const leave = ctx.apps.leave; const input = { days: 5, reason: 'Summer holiday' }; return await leave.mutations.request.handler(input, leave, ctx);});console.log(`Requested: "${request.properties.reason}" is ${request.state.value}`);
// Approve the request.const approved = await workspace.write(async (ctx) => { const leave = ctx.apps.leave; return await leave.mutations.approve.handler({ id: request.id }, leave, ctx);});console.log(`Approved: "${approved.properties.reason}" is ${approved.state.value}`);Run it again:
PYN_ENV=local npx tsx run.tsYou should see:
Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469Requested: "Summer holiday" is pendingApproved: "Summer holiday" is approvedThe request starts in pending because that state is the default. approve moves it to approved. The Runtime runs each call in one transaction, so a call that throws changes nothing.
5. Add a Listener
Section titled “5. Add a Listener”Make the App react when a request is approved. In leave.ts, declare an Event with a Listener, below the transition:
const approvedEvent = leaveRequest.addEvent('approved'); approvedEvent.addListener('announce', async (_event, item) => { console.log(`Listener: "${item.properties.reason}" is approved`); });Then publish the Event from the approve Mutation:
const updated = await item.state.transitions.approve.execute({}); await updated.events.approved.publish({}); return updated;Run it once more:
PYN_ENV=local npx tsx run.tsYou should see:
Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469Requested: "Summer holiday" is pendingApproved: "Summer holiday" is approvedListener: "Summer holiday" is approvedNothing in run.ts calls the Listener. approve commits the state change and the Event together. The Runtime then runs the Listener in a transaction of its own, when run.ts reaches harness.tickAll().
A Listener is the place for the work that follows an approval, such as sending an email or updating a calendar.

