Skip to content
VerificationWould you notice if your agent drifted in production?Explore the 5-stage harness›
Specification Language

Models, Entities & Relations

In Aegil, data models declared in specs/<feature>/*.tsp define both application data structures and relational PostgreSQL database tables. Using @entity and @relation decorators, you declare primary keys, foreign-key constraints, cascading deletes, and indexes in a single unified specification.

Here is a complete example modeling a Helpdesk ticket and its associated comments:

namespace Helpdesk;
@entity({ table: "tickets" })
model Ticket {
@pk
id: uuid;
@owner
reporterId: uuid;
@index
assignedAgentId?: uuid;
title: string;
description?: string;
@relation({ name: "ticket_comments", target: "TicketComment", kind: "oneToMany" })
comments?: TicketComment[];
createdAt: utcDateTime;
}
@entity({ table: "ticket_comments" })
model TicketComment {
@pk
id: uuid;
@owner
authorId: uuid;
@index
ticketId: uuid;
body: string;
@relation({ name: "ticket_comments", target: "Ticket", kind: "manyToOne", onDelete: "cascade" })
ticket?: Ticket;
createdAt: utcDateTime;
}

Every @entity model must have a primary key:

  • Use @pk id: uuid; to generate a UUID primary key (recommended for distributed and multi-tenant apps).
  • Use @pk id: int; if an integer auto-incrementing key is required.

Annotating a field with @owner (e.g. @owner reporterId: uuid;) designates the resource owner. On @autoMutation({ action: "create" }), Aegil automatically sets the owner field to auth.id without requiring the client to pass it.


Use @index on foreign keys and frequently queried fields:

@index
ticketId: uuid;
@index
status: TicketStatus;

Aegil compiles @index into a PostgreSQL B-Tree index in the generated database schema.


To connect two entities, declare matching @relation decorators on both sides sharing the same name:

  1. Parent model (oneToMany):

    @relation({ name: "ticket_comments", target: "TicketComment", kind: "oneToMany" })
    comments?: TicketComment[];
  2. Child model (manyToOne):

    @index
    ticketId: uuid;
    @relation({ name: "ticket_comments", target: "Ticket", kind: "manyToOne", onDelete: "cascade" })
    ticket?: Ticket;

Adding onDelete: "cascade" instructs PostgreSQL to automatically delete all child comments when the parent ticket is deleted.


Aegil organizes specs into feature subfolders under specs/:

  • Only global files (app.tsp and platforms.tsp) belong directly at specs/ root.
  • Domain models and operations belong inside a feature folder (e.g. specs/helpdesk/tickets.tsp, specs/helpdesk/comments.tsp). Placing domain specs directly at the root triggers ERR_SPEC_MISPLACED.
  • Every .tsp file is merged deterministically in alphabetical path order.
  • Every file declaring a namespace must use the exact namespace defined in aegil.yaml.
  • All model, enum, and operation names must be unique across all files.

What you see Why What to do
ERR_SPEC_MISPLACED A domain spec file was placed directly at specs/*.tsp. Move domain specs into specs/<feature>/*.tsp.
ERR_RELATION_TARGET_UNKNOWN A @relation references a target model that does not exist. Check the spelling of target: "..." in the relation decorator.
ERR_RELATION_MISMATCH Two matching relations have conflicting name or kind values. Ensure the relation name matches on both models, and one is oneToMany while the other is manyToOne.
ERR_PK_MISSING An @entity model has no @pk field. Add @pk id: uuid; to the model.