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.
Defining entities and relations
Section titled “Defining entities and relations”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;}Primary keys (@pk) and ownership (@owner)
Section titled “Primary keys (@pk) and ownership (@owner)”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.
Indexes (@index)
Section titled “Indexes (@index)”Use @index on foreign keys and frequently queried fields:
@indexticketId: uuid;
@indexstatus: TicketStatus;Aegil compiles @index into a PostgreSQL B-Tree index in the generated database schema.
One-to-many and many-to-one relations
Section titled “One-to-many and many-to-one relations”To connect two entities, declare matching @relation decorators on both sides sharing the same name:
-
Parent model (
oneToMany):@relation({ name: "ticket_comments", target: "TicketComment", kind: "oneToMany" })comments?: TicketComment[]; -
Child model (
manyToOne):@indexticketId: uuid;@relation({ name: "ticket_comments", target: "Ticket", kind: "manyToOne", onDelete: "cascade" })ticket?: Ticket;
Cascade deletes (onDelete: "cascade")
Section titled “Cascade deletes (onDelete: "cascade")”Adding onDelete: "cascade" instructs PostgreSQL to automatically delete all child comments when the parent ticket is deleted.
Organizing specs into feature folders
Section titled “Organizing specs into feature folders”Aegil organizes specs into feature subfolders under specs/:
- Only global files (
app.tspandplatforms.tsp) belong directly atspecs/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 triggersERR_SPEC_MISPLACED. - Every
.tspfile is merged deterministically in alphabetical path order. - Every file declaring a namespace must use the exact
namespacedefined inaegil.yaml. - All model, enum, and operation names must be unique across all files.
What could go wrong?
Section titled “What could go wrong?”| 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. |
Next steps
Section titled “Next steps”- Learn Validation Bounds & Invariants.
- Learn Row-Level Security & Policies.
- Learn State Machines & Workflows.