Skip to content

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.

You need Node.js 24 and an empty project that runs TypeScript:

Terminal window
mkdir leave-app && cd leave-app
npm init -y && npm pkg set type=module
npm install --save-dev tsx
Terminal window
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-logger

You write an App with @alpynehq/alpyne-types. The other packages run it on SQLite.

Create leave.ts. This file is the whole App.

leave.ts
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 leaveRequest with two Properties, reason and days.
  • How a request moves. Two states, pending and approved, and one transition between them, approve.
  • What a caller does. Two Mutations: request creates a leave request, and approve runs the transition.

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.

run.ts
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:

Terminal window
PYN_ENV=local npx tsx run.ts

You should see:

Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469

Your App now runs on SQLite, in a Workspace of its own. It holds no leave requests so far.

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.

run.ts
// 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:

Terminal window
PYN_ENV=local npx tsx run.ts

You should see:

Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Requested: "Summer holiday" is pending
Approved: "Summer holiday" is approved

The 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.

Make the App react when a request is approved. In leave.ts, declare an Event with a Listener, below the transition:

leave.ts
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:

leave.ts
const updated = await item.state.transitions.approve.execute({});
await updated.events.approved.publish({});
return updated;

Run it once more:

Terminal window
PYN_ENV=local npx tsx run.ts

You should see:

Leave App is running in Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Requested: "Summer holiday" is pending
Approved: "Summer holiday" is approved
Listener: "Summer holiday" is approved

Nothing 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.