Skip to content

Define data

Declare a Template for each kind of Item your App persists, then create Items below the Workspace Item or another parent. Use this guide when a new kind of data enters your App.

  • A configured App, as built in Your first application.
  • zod imported in the App module when a Property holds structured JSON.
  1. Declare the Template. On the App, call addTemplate(key) once per kind of Item. The key names the Template in every PRN beneath it, for example prn::directory:template/team. A key starts with a lowercase letter, continues with letters and digits, and runs to 64 characters; root is reserved.

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };
  2. Add primitive and temporal Properties. On the Template, call addProperty(key, type, options). Primitive types are 'string', 'integer', 'number', 'boolean' and 'id'. Temporal types are 'date', 'time', 'datetime', 'instant' and 'duration'. A datetime or instant value is a Luxon DateTime, so ctx.now writes straight into one. The options carry a label for generated assets and a format such as 'email' for a string. Chain .index() on a Property that selectors filter or sort by. The Property keys id and state are reserved.

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };
  3. Add select and JSON Properties. A 'single-select' or 'multi-select' Property returns a builder with addOption(key, { color, icon, label }). A stored value that names no option reads as null for a single select and as an empty array for a multi select. Pass a Zod object type such as z.object({ ... }) instead of a type name to declare a JSON Property parsed by it.

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };
  4. Add Workspace-level Properties. Call app.getWorkspaceTemplate().addProperty(key, type) for a value that belongs to the Workspace as a whole, such as a time zone. Read and write it through the Workspace Item from ctx.getWorkspaceItem().

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };
  5. Create Items in a Mutation. In a write execution, parent.createChild(template, { key, properties, state }) places the new Item below parent, and current.templates.employee.createItem(parent, options) does the same from the Template side. Top-level Items hang off ctx.getWorkspaceItem(). A key is unique among one parent’s children: creating another child with the same key moves that key to the new child.

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };
  6. Address Items by key path. ctx.getItemByKeyPath('platform', 'ada') walks from the Workspace Item one key per level and returns undefined when any level is missing. ctx.getItem(id) and current.templates.employee.getItem(id) address an Item by Identifier; parent.getChildByKey(key) descends one level.

    examples/define-data/directory.ts
    import type { AppSpec } from '@alpynehq/alpyne-types/builder';
    import { z } from 'zod';
    interface CreateTeamInput {
    readonly key: string;
    readonly name: string;
    }
    interface HireInput {
    readonly email: string;
    readonly key: string;
    readonly level: string;
    readonly name: string;
    readonly teamKey: string;
    }
    interface FindInput {
    readonly employeeKey: string;
    readonly teamKey: string;
    }
    export const directoryApp: AppSpec = (app) => {
    app.configure('directory');
    const team = app.addTemplate('team');
    team.addProperty('name', 'string').index();
    team.addProperty('headcount', 'integer');
    team.addProperty('founded', 'date');
    team.addProperty('remote', 'boolean');
    const employee = app.addTemplate('employee');
    employee.addProperty('name', 'string', { label: 'Full name' });
    employee.addProperty('email', 'string', { format: 'email' }).index();
    employee.addProperty('startedAt', 'datetime');
    const level = employee.addProperty('level', 'single-select');
    level.addOption('junior', { label: 'Junior' });
    level.addOption('senior', { label: 'Senior' });
    level.addOption('lead', { label: 'Lead' });
    const skills = employee.addProperty('skills', 'multi-select');
    skills.addOption('typescript', {});
    skills.addOption('postgresql', {});
    employee.addProperty('address', z.object({ city: z.string(), country: z.string() }));
    app.getWorkspaceTemplate().addProperty('timezone', 'string');
    const createTeam = app.addMutation('createTeam', async (input: CreateTeamInput, current, ctx) => {
    const workspace = await ctx.getWorkspaceItem();
    return await workspace.createChild(current.templates.team, {
    key: input.key,
    properties: { headcount: 0, name: input.name, remote: false },
    });
    });
    createTeam.input.addProperty('key', 'string').mandatory();
    createTeam.input.addProperty('name', 'string').mandatory();
    createTeam.output.item(team);
    const hire = app.addMutation('hire', async (input: HireInput, current, ctx) => {
    const parent = await ctx.getItemByKeyPath(input.teamKey);
    if (parent === undefined) throw new Error(`Team ${input.teamKey} not found`);
    return await current.templates.employee.createItem(parent, {
    key: input.key,
    properties: { email: input.email, level: input.level, name: input.name, startedAt: ctx.now },
    });
    });
    hire.input.addProperty('teamKey', 'string').mandatory();
    hire.input.addProperty('key', 'string').mandatory();
    hire.input.addProperty('name', 'string').mandatory();
    hire.input.addProperty('email', 'string').mandatory();
    hire.input.addProperty('level', 'string').mandatory();
    hire.output.item(employee);
    const find = app.addQuery('find', async (input: FindInput, _current, ctx) => {
    return await ctx.getItemByKeyPath(input.teamKey, input.employeeKey);
    });
    find.input.addProperty('teamKey', 'string').mandatory();
    find.input.addProperty('employeeKey', 'string').mandatory();
    find.output.item(employee);
    };

The test below creates a keyed team, hires a keyed employee below it, and reads both back by key path and Identifier through the App’s Query.

examples/define-data/directory.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 { directoryApp } from './directory';
describe('define data', () => {
it('creates keyed Items in the hierarchy and reads them back by key path', 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 }, directoryApp);
const workspace = await harness.createWorkspace();
// The harness runs every execution as system.
const ids = await workspace.write(async (ctx) => {
const current = ctx.apps.directory;
const team = await current.mutations.createTeam.handler({ key: 'platform', name: 'Platform' }, current, ctx);
const hired = await current.mutations.hire.handler(
{ email: 'ada@example.com', key: 'ada', level: 'lead', name: 'Ada Lovelace', teamKey: 'platform' },
current,
ctx,
);
await hired.updateProperties({ address: { city: 'London', country: 'GB' }, skills: ['typescript'] });
const root = await ctx.getWorkspaceItem();
await root.updateProperties({ timezone: 'Europe/London' });
return { employee: hired.id, team: team.id };
});
await workspace.read(async (ctx) => {
const current = ctx.apps.directory;
const found = await current.queries.find.handler({ employeeKey: 'ada', teamKey: 'platform' }, current, ctx);
const team = await ctx.getItem(ids.team);
const root = await ctx.getWorkspaceItem();
expect(found?.id).toBe(ids.employee);
expect(found?.key).toBe('ada');
expect(found?.parentId).toBe(ids.team);
expect(found?.templatePrn).toBe('prn::directory:template/employee');
expect(found?.properties.level).toBe('lead');
expect(found?.properties.skills).toStrictEqual(['typescript']);
expect(found?.properties.address).toStrictEqual({ city: 'London', country: 'GB' });
expect(found?.propertyValues.startedAt.type).toBe('datetime');
expect(team?.properties.headcount).toBe(0);
expect(root?.properties.timezone).toBe('Europe/London');
});
expect.assertions(10);
});
});

From the repository root, run:

Terminal window
pnpm run cli test alpynehq/alpyne/website/examples/define-data/

You should see:

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

The assertions confirm that the child’s parentId is the team and its templatePrn is prn::directory:template/employee. Each Property reads back with its declared type: the select as an option key, the JSON as the parsed object and the datetime as a tagged value in propertyValues.

  • parent.upsertChildByKey(template, { key, properties }) creates the child when the key is free and writes the supplied Properties to the existing child otherwise. upsertChildrenByKey does the same for a map of keys.
  • template.setExtensible() lets each parent Item declare additional Properties for its children through item.extensions(template).
  • property.unique() declares a Property whose value is unique for the Template within a Workspace.
  • setDescription(text) on a Template or Property and setLabel(text) on a Property feed the generated reference and application assets.