Serverpod Backend & PostgreSQL
Aegil generates and manages a complete, production-grade Serverpod backend project under .aegil/server/ featuring type-safe ORM models, session-based authentication, and transactional endpoints derived directly from your TypeSpec specifications.
Server directory structure (.aegil/server/)
Section titled “Server directory structure (.aegil/server/)”Aegil compiles your TypeSpec declarations into a standard Serverpod server application:
.aegil/server/ bin/ main.dart # Serverpod server entrypoint config/ development.yaml # Database port (8090) and server port (8080) passwords.yaml # Local database passwords and JWT secrets (mode 0600) lib/ src/ endpoints/ # Generated session-authenticated endpoints generated/ # Serverpod protocol models (*.spy.yaml -> protocol.dart) handlers/ # Generated handler interfaces for custom hooks hooks/ # Mirrored hook implementations from Zone 2 pubspec.yaml # Server dependencies (Serverpod, PostgreSQL driver)Remember:
.aegil/server/belongs to Zone 3 (Compiler-Owned). You should never edit files in this directory directly. If you need to change a schema, editspecs/<feature>/*.tspand runaegil build.
Serverpod ORM models (.spy.yaml)
Section titled “Serverpod ORM models (.spy.yaml)”For every @entity declared in TypeSpec, Aegil generates a Serverpod .spy.yaml file:
# Generated by Aegil compiler from specs/helpdesk/tickets.tspclass: Tickettable: ticketsfields: reporterId: UuidValue title: String status: TicketStatus createdAt: DateTimeServerpod compiles these YAML models into Dart ORM classes with type-safe database queries (Ticket.db.find, Ticket.db.insertRow, Ticket.db.updateRow).
Endpoints and zero-trust authentication
Section titled “Endpoints and zero-trust authentication”Aegil generates authenticated Serverpod endpoints that enforce session security:
class TicketEndpoint extends Endpoint { @override bool get requireLogin => true; // Private by default
Future<Ticket> getTicket(Session session, UuidValue id) async { // 1. Caller identity extracted securely from session (never client parameters) final callerId = await session.auth.authenticatedUserId; final callerRole = await session.auth.authenticatedUserRole;
// 2. Query executed with PostgreSQL RLS filter compiled from @policy final ticket = await Ticket.db.findById(session, id, where: (t) => ...); if (ticket == null) { throw EndpointException('ERR_NOT_FOUND', 'Ticket not found or access denied'); } return ticket; }}Key security guarantees:
Section titled “Key security guarantees:”- No client identity parameters: Endpoints never accept
actingUserIdorcallerRole. Caller ID is extracted strictly from the cryptographic session. - SQL filter pushdown: Row-level access policies compile directly into parameterized PostgreSQL queries, preventing cross-tenant leakage.
Spec-declared health routes (@health)
Section titled “Spec-declared health routes (@health)”Aegil does not generate secret or undocumented HTTP routes. If your cloud deployment (e.g. Kubernetes, AWS ECS, Google Cloud Run) requires a health check, declare it explicitly in your spec:
@health({ path: "/health" })op checkHealth(): string;Aegil registers a lightweight HTTP GET handler at /health returning 200 OK. The BOOT check in aegil verify tests this endpoint automatically.
What could go wrong?
Section titled “What could go wrong?”| What you see | Why | What to do |
|---|---|---|
Cannot connect to Docker |
PostgreSQL container is not running. | Start Docker Desktop or daemon and re-run aegil run or aegil verify. |
ERR_TAMPERED_GENERATED_FILE |
A file in .aegil/server/ was modified manually. |
Discard changes to .aegil/. Modify specs/<feature>/*.tsp and run aegil build. |
Port 8080 already in use |
Another process is occupying the Serverpod port. | Terminate the occupying process or run Serverpod on a custom port. |