Persistence and projection
The PostgreSQL and SQLite Runtimes persist a Workspace in a small set of generic tables; the test Runtime holds the same shapes in memory. Each Runtime stores Items, Relations and Events, assigns a version on every write, applies Grants on every read, and pairs with a projector that writes Metric data. The shared conformance suite checks the same behavior against all three.
PostgreSQL
Section titled “PostgreSQL”Every Item is one row in the item table, whatever its Template. The row carries id, workspaceId, templatePrn, parentId, key, state, properties (a JSON document), archivedAt, naturalOrder, originId, metadata, refreshBy, refreshedAt, version, createdAt and updatedAt. Every query on the table filters by workspaceId, and a lookup by Identifier filters by both id and workspaceId.
An update patches the row. Property changes merge into properties with the JSON || operator, the other columns are set as supplied, and version receives a new sortable identifier. An insert assigns a fresh version as well. When an insert carries a key, the Runtime clears that key from any sibling under the same parent in the same statement sequence, so the newest child holds the key. Searchable Properties are copied to item_index rows after each write.
A transition runs inside a savepoint as one conditional UPDATE ... WHERE id = ? AND state = <from> AND workspace_id = ?. Zero affected rows raises the state transition conflict the executor retries.
Reads and Grants
Section titled “Reads and Grants”ItemInstance.createPermissioned wraps every row a read returns. Under sudo and system it returns the Item as is. Under principal and masquerade it evaluates each Template Grant’s Role for that Item. When none match, the Item is dropped from the result; the Workspace Item is always returned. The Properties exposed are the union of the Properties named by the matching Grants plus any extension Properties.
Selection builds in phases (helpers/item-query.ts): base columns from the right Workspace, coarse WHERE clauses for Template, state, archive and hierarchy, Property predicates, ORDER BY, then optional joins. Grant evaluation happens after the rows return, so totalCount reflects the rows the SQL matched.
Relations and Events
Section titled “Relations and Events”Relations are rows in the relation table with fromId, toId, relationPrn, uniqueDirectionId, createdAt and removedAt. Removal sets removedAt. Two partial unique indexes over live rows enforce one link per pair and, for cardinalities other than many-to-many, one link per direction. addIfNotExists and setIfNotExists rely on them and return false on a violation.
Events are rows in the event table: id, workspaceId, eventPrn, itemId, payload, createdAt, originalPrincipalId, parentId, rootParentId, eventPrnLineage, traceId, reactorKey, claimedAt, completedAt and projectedAt. publish inserts one row per payload inside the write execution’s transaction. Idempotency tokens are rows in idempotency_token keyed by eventId and scope.
Projection
Section titled “Projection”The local projector (src/alpyne/projector/local) runs on a tick. In one transaction it selects up to 10,000 event rows with no projectedAt, newest first, with FOR UPDATE SKIP LOCKED, and stamps projectedAt on them. Newest-first ordering lets a Workspace whose projections were cleared for reprojection catch up without starving current Events.
For each selected Event whose PRN a Metric declares, the projector opens a system write execution in the Event’s Workspace and evaluates the Metric’s calculation handler and every Dimension handler. A calculation handler that returns null excludes the Event from that Metric. A Dimension handler that returns an Item yields that Item’s Identifier, and one that returns a valid Identifier string yields it. One that returns another string, returns undefined or throws yields the null Identifier and a warning.
The projector then inserts one projection row per Metric. The row carries the Dimension values in dimension_1 to dimension_4, eventId, itemId, metricPrn, workspaceId, createdAt, referenceAt set to the Event’s creation time, and value set to the calculation handler’s result or 1 without a handler. The insert ignores a conflict, so an Event projects once per Metric. An error while projecting an Event is written to projection_error and the tick moves on.
A Metric query (helpers/metric-query.ts) aggregates projection rows for the Workspace, Metric PRN and interval: value under the query’s aggregate function and COUNT(DISTINCT <focal dimension>) for the distinct count, grouped by the requested Dimensions with ROLLUP.
SQLite
Section titled “SQLite”The SQLite Runtime keeps the same tables and columns in SQLite. Property patches are a chain of json_set calls, one per key. Full-text search queries run through SQLite FTS. Relation uniqueness is enforced by the same two partial unique indexes, recognized by table and column set rather than by constraint name.
The SQLite projector (src/alpyne/projector/sqlite) selects unprojected Events newest first with a plain SELECT, marks each one projected with a conditional UPDATE ... WHERE projected_at IS NULL, and writes projection rows as the local projector does. The Metric query computes the same aggregation in SQLite SQL.
Test Runtime
Section titled “Test Runtime”The test Runtime holds Items in a map keyed by Identifier, with the same shape as an item row, Relations in a second map, and Events in an array. A write execution works on deep clones of both maps; commitTransaction merges the attempt’s Item and Relation changes back into the shared store and replays the attempt’s transitions against the committed state. Reads go through ItemInstance.createPermissioned, which applies the same Grant evaluation as PostgreSQL. The test Runtime has no projector; Metric declarations compile, and projections and Metric queries are exercised against PostgreSQL or SQLite.
Related
Section titled “Related”- Metrics and projections explains why Metrics are derived from Events and lag the write.
- Metrics states the contribution, Dimension and projection rules the projector implements.
- Runtime implementations compares the three Runtimes and their pairing with reactors and projectors.
- Unit of Work states the versioning and commit rules the write path implements.
MetricQueryBuilderqueries projected data.

