Data model
A Template describes a kind of persisted Item: its Properties, Relations, states, Events, Roles and Grants. An Item is one instance of a Template inside one Workspace, with an Identifier, a parent and an optional key. An App declares Templates with addTemplate, and handlers create and read Items through the Template’s instance on current.templates.
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface CreateDepartmentInput { readonly key: string; readonly name: string;}
interface MoveInput { readonly departmentId: string; readonly employeeId: string;}
export const organizationApp: AppSpec = (app) => { app.configure('organization');
const department = app.addTemplate('department'); department.addProperty('name', 'string').index();
const employee = app.addTemplate('employee'); employee.addProperty('email', 'string'); employee.addProperty('startedOn', 'date'); employee.addRelation('department', 'many-to-one', department, { toKey: 'members' });
const contractor = app.addTemplate('contractor'); contractor.addProperty('email', 'string');
const worker = app.addUnion('worker', [employee, contractor]); department.addRelation('lead', 'one-to-one', worker);
const createDepartment = app.addMutation('createDepartment', async (input: CreateDepartmentInput, current, ctx) => { const workspace = await ctx.getWorkspaceItem(); const created = await workspace.createChild(current.templates.department, { key: input.key, properties: { name: input.name }, }); return created.id; // ctx.getItemByKeyPath(input.key) resolves the same Item }); createDepartment.input.addProperty('key', 'string').mandatory(); createDepartment.input.addProperty('name', 'string').mandatory(); createDepartment.output.value('id');
const move = app.addMutation('move', async (input: MoveInput, current) => { const target = await current.templates.department.getItem(input.departmentId); const person = await current.templates.employee.getItem(input.employeeId); if (target === undefined || person === undefined) throw new Error('Department or employee not found');
const startedOn = person.propertyValues.startedOn; // Tagged: { type: 'date', value: '2024-03-01' } if (startedOn === undefined || startedOn.type === 'invalid') throw new Error('Employee has no usable start date'); if (typeof person.properties.email !== 'string') throw new Error('Employee has no email'); // Interpreted value
const relation = person.relations.department; if (relation.type !== 'one') throw new Error('Unexpected cardinality'); await relation.set(target);
const members = target.relations.members; if (members.type !== 'many') throw new Error('Unexpected cardinality'); const page = await members.select().limit(50).execute(); // RelatedItem<WriteItem>[], each with underlyingItem return page.results.length; }); move.input.addProperty('departmentId', 'id').mandatory(); move.input.addProperty('employeeId', 'id').mandatory(); move.output.value('integer');};Properties and incomplete data
Section titled “Properties and incomplete data”A Property is a typed value declared with addProperty(key, type). Types are primitive (string, integer, number, boolean, id), temporal (date, time, datetime, instant, duration), select (single-select, multi-select) or a Zod type for structured JSON. The keys id and state are reserved. index() makes a Property available to selector filters and sorts; unique() adds a uniqueness constraint among Items of the Template in a Workspace.
An Item exposes each value twice. item.properties.email is the interpreted value, ready to use. item.propertyValues.email is the tagged form, { type: 'string', value: 'ada@example.com' }. The tagged form says how the Runtime read the stored value. It reports { type: 'invalid', baseType } when a stored value does not fit the declared type, for example after a Property changed type.
A value can be absent for three reasons: it was never set, it does not fit the declared type, or the active principal holds no Grant that covers it. Handler code checks for the values it needs before acting, as move does with startedOn. A missing value means unavailable, not proof that nothing is stored. Prefer propertyValues when you need the reason and properties when you need only the value.
Hierarchy and keys
Section titled “Hierarchy and keys”Items form one tree per Workspace. The Workspace Item is the root; every other Item has exactly one parent, set at creation with parent.createChild(template, options) or template.createItem(parent, options) and movable with assignParent. Children are read with selectChildren() and the parent with getParent().
A key is an optional identifier for an Item relative to its parent, unique among that parent’s children. Keys turn the tree into an address space: ctx.getItemByKeyPath('engineering', 'platform') walks from the Workspace Item through the child keyed engineering to its child keyed platform. upsertChildByKey creates or updates a child by its key in one call. The key root is reserved.
An Item is addressed three ways. Its Identifier is global and opaque. Its Template PRN says what kind of Item it is. Its key path says where it sits. Use the Identifier for references you store, the PRN for type checks such as template.isType(item), and the key path for well-known singletons such as a settings Item.
Relations
Section titled “Relations”A Relation is a declared, directional link between a Template and another Template or Union. addRelation(key, cardinality, to, options) names the link on the from side; options.toKey names it on the to side. Cardinality is one of one-to-one, one-to-many, many-to-one and many-to-many, read from the from side to the to side. In the example, an employee has one department and a department has many members.
Navigation depends on the side you stand on. A side with cardinality one exposes get, set, clear and setIfNotExists; a side with cardinality many exposes select, add, remove, clear and addIfNotExists. Both expose type, 'one' or 'many', so a handler narrows before it calls. setIfNotExists and addIfNotExists apply their check and their write atomically.
Every target comes back as a RelatedItem: the target Item plus relation metadata (fromId, toId, relationPrn, createdAt) and underlyingItem, the target without its wrapper. Pass underlyingItem where an API expects a plain Item.
A Relation links two Items that each have their own parent and lifecycle. Archiving one side leaves the other untouched. Prefer a Relation when both Items have their own lifecycle, and the hierarchy when a child has no meaning without its parent.
Unions
Section titled “Unions”A Union is a named group of Templates declared with addUnion(key, templates). It serves as a target for Relations, as an owner of Events and as a selection scope. current.unions.worker.selectItems() returns Items of every member Template, and isType narrows an unknown Item to a member. In the example, department.lead may point at an employee or a contractor.
A Union groups; it does not inherit. Member Templates keep their own Properties, states and Grants, and the Union declares no Properties of its own. A handler that needs a Property checks which member Template the Item belongs to first.
Data shape versus suitability
Section titled “Data shape versus suitability”The type system answers “is email a string?”. It cannot answer “does this employee have enough data to be moved?”. The second question belongs to the handler: move requires a usable start date and an email before it changes anything. Declare the shape on the Template and check suitability in the operation that needs it. Prefer a thrown error with a domain message over a silent partial write.
Boundaries
Section titled “Boundaries”- A Template owns no storage of its own. Runtimes store Items generically, discriminated by Workspace and Template PRN.
- A Property declaration is not a presence guarantee. Values can be unset, unreadable under the declared type, or filtered by Grants.
- A Relation is not ownership. Lifecycle follows the hierarchy, not the Relation.
- A Union is not a base class. It adds no Properties to its members.
Related
Section titled “Related”- Define data to declare Templates, Properties, hierarchy and keys.
- Relate items for each cardinality and its navigation API.
- Handle incomplete data to handle absent and invalid values in handlers.
- Data and identity for the normative rules on Items, Properties and hierarchy.
- Glossary for the canonical definition of each term.

