Types, Nullability & Enums
Aegil TypeSpec provides a statically typed schema language that maps primitive scalars, nullability semantics, and enums directly to Dart language types and PostgreSQL column definitions.
Primitive types
Section titled “Primitive types”Aegil includes built-in primitive types specifically chosen for full-stack Dart and PostgreSQL applications:
| TypeSpec Type | Dart Type | PostgreSQL Column Type | Description |
|---|---|---|---|
string |
String |
text |
UTF-8 variable-length text. |
int |
int |
bigint |
64-bit signed integer. |
float |
double |
double precision |
64-bit IEEE 754 floating-point number. |
boolean |
bool |
boolean |
Logical true or false. |
uuid |
UuidValue |
uuid |
Canonical 128-bit RFC 4122 Universally Unique Identifier. |
utcDateTime |
DateTime |
timestamp with time zone |
Instant in UTC time with microsecond resolution. |
Example model using primitives
Section titled “Example model using primitives”namespace Helpdesk;
@entity({ table: "customers" })model Customer { @pk id: uuid;
name: string; loginCount: int; satisfactionScore: float; isActive: boolean; createdAt: utcDateTime;}Nullability
Section titled “Nullability”By default, every field in an Aegil model is non-nullable (required). To declare that a field can be null in Dart and NULL in the database, append a question mark ?:
model UserProfile { @pk id: uuid;
// Required field username: string;
// Optional (nullable) fields bio?: string; avatarUrl?: string; lastLoginAt?: utcDateTime;}Compiler guarantees for nullability
Section titled “Compiler guarantees for nullability”- Database constraints: Nullable fields compile to nullable columns (
NULL). Non-nullable fields compile with a strictNOT NULLconstraint. - Dart type promotion: The Serverpod ORM classes and Flutter models generate
String?,DateTime?, etc., respecting Dart’s sound null safety. - Client validation: Missing non-nullable fields are rejected at the client boundary before any network request is sent.
Collections and Lists
Section titled “Collections and Lists”To declare an array or list of values, use the bracket syntax T[]:
model Article { @pk id: uuid;
title: string; tags: string[];}Lists compile to List<T> in Dart. When stored in PostgreSQL, scalar lists map to native PostgreSQL array types or JSONB serialization as managed by Serverpod.
Enums define a closed set of symbolic values. They ensure data integrity by guaranteeing that only recognized enum cases can be saved to the database or transmitted across the wire.
Declaring an Enum
Section titled “Declaring an Enum”Declare enums at the top level of your spec file:
namespace Helpdesk;
enum TicketStatus { Open, InProgress, Resolved, Closed,}
enum Priority { Low, Medium, High, Urgent,}Using Enums in Models
Section titled “Using Enums in Models”Reference the enum directly as a field type:
@entity({ table: "tickets" })model Ticket { @pk id: uuid;
title: string; status: TicketStatus; priority: Priority; createdAt: utcDateTime;}How Enums compile
Section titled “How Enums compile”- Serverpod Server: Aegil generates an enum in
server/lib/src/generated/with serialized values stored as integers or strings in PostgreSQL. - Flutter Client: Aegil generates matching Dart enums in the client protocol (
client/lib/api.dart), giving complete compile-time type safety in Flutter widgets:
// Flutter UI codefinal ticket = state.items.first;
switch (ticket.status) { case TicketStatus.Open: return const Chip(label: Text('Open'), backgroundColor: Colors.blue); case TicketStatus.InProgress: return const Chip(label: Text('In Progress'), backgroundColor: Colors.orange); case TicketStatus.Resolved: return const Chip(label: Text('Resolved'), backgroundColor: Colors.green); case TicketStatus.Closed: return const Chip(label: Text('Closed'), backgroundColor: Colors.grey);}Next steps
Section titled “Next steps”- Learn how to structure tables and foreign keys in Models, Entities & Relations.
- Add validation bounds to strings and numbers in Validation & Constraints.
- Explore API query and mutation declarations in Operations: Queries, Mutations & Streams.