zanith

Enums

Enums define a fixed set of valid values for a field — like roles, statuses, or categories. They're enforced at the database level, not just in your code.

Why enums?

Without enums, a "role" field is just a string. Anyone can insert "ADMN" (typo) or "superadmin" (wrong value) and it silently succeeds. With enums, the database rejects invalid values — your data stays clean regardless of where the write comes from.

Defining enums

Use defineEnum() to create an enum. Each enum has a name, a list of valid values, and an optional comment for documentation.

TSenums.ts
import { defineEnum } from 'zanith';
 
export const Role = defineEnum({
name: 'Role',
values: ['ADMIN', 'USER', 'MODERATOR'],
comment: 'User access levels',
});
 
export const OrderStatus = defineEnum({
name: 'OrderStatus',
values: ['PENDING', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED'],
comment: 'Order lifecycle states',
});

Using enums in fields

Reference your enum in a field definition with m.enum(). You can set a default value from the enum's values.

TS
const User = defineModel((m) => ({
name: 'User', table: 'users',
fields: {
...m.id(),
email: m.string().unique(),
role: m.enum(Role).default('USER'), // defaults to 'USER'
},
}));
 
const Order = defineModel((m) => ({
name: 'Order', table: 'orders',
fields: {
...m.id(),
status: m.enum(OrderStatus).default('PENDING'),
total: m.float(),
},
}));

Passing enums to createZanith

Enums must be passed separately when creating the client. This registers them in the schema graph so they're available for validation and introspection.

TS
const db = await createZanith({
models: { User, Order },
enums: [Role, OrderStatus], // ← pass all enums here
adapter: new PgAdapter({ ... }),
});

In the .zn DSL

Enums in the .zn DSL syntax look like this:

TSenums.zn
/// User access levels
enum Role {
ADMIN
USER
MODERATOR
}