Enums
String-only named constants with generated TypeScript types
Enums define a fixed set of named string constants for a field. They work like enums in Prisma — you declare the allowed values, and Cerial generates a TypeScript const object for runtime access, a union type for annotations, and a where type for filtering.
Defining an Enum
Declare an enum with the enum keyword followed by a name and a list of values:
enum Status { ACTIVE, INACTIVE, PENDING }enum Role {
ADMIN
EDITOR
VIEWER
}Pick one style per block — comma-separated on a single line, or newline-separated with one value per line. Don't mix both in the same enum.
Rules:
- Values are bare identifiers — no quotes around them
- No casing enforcement — use whatever convention fits your project (
UPPER_CASE,PascalCase,camelCase) - Each value must be a valid identifier (letters, digits, underscores; cannot start with a digit)
- All values are strings only — if you need numbers, booleans, or mixed types, use a Literal instead
Name collisions
Enum names must be unique across your entire schema — they cannot collide with literal, model, object, or tuple names. Cerial will error at generation time if it detects a name conflict.
Generated TypeScript
For each enum, Cerial generates three TypeScript constructs. Given this schema:
enum Status { ACTIVE, INACTIVE, PENDING }Const Object
A const object with each value as both key and value, providing runtime access with autocompletion:
export const StatusEnum = {
ACTIVE: 'ACTIVE',
INACTIVE: 'INACTIVE',
PENDING: 'PENDING',
} as const;Union Type
A string union type derived from the const object:
export type StatusEnumType = typeof StatusEnum[keyof typeof StatusEnum];
// Equivalent to: 'ACTIVE' | 'INACTIVE' | 'PENDING'Where Type
A filter interface for use in where clauses:
export interface StatusEnumWhere {
eq?: StatusEnumType;
neq?: StatusEnumType;
in?: StatusEnumType[];
notIn?: StatusEnumType[];
contains?: string;
startsWith?: string;
endsWith?: string;
}Using Enums in Models
Enum types are used on fields just like any built-in type. They support optionality, nullability, defaults, and arrays:
enum Status { ACTIVE, INACTIVE, PENDING }
enum Role { ADMIN, EDITOR, VIEWER }
model User {
id Record @id
name String
status Status # required enum field
role Role? @default('VIEWER') # optional with default
prevStatus Status? @nullable # optional + nullable
tags Role[] # array of enum values
}const user = await client.db.User.create({
data: {
name: 'Alice',
status: 'ACTIVE',
// role defaults to 'VIEWER'
},
});
console.log(user.status); // 'ACTIVE'
console.log(user.role); // 'VIEWER'Using the Const Object
The generated const object gives you runtime access to enum values with full autocompletion and refactoring support. Use it instead of raw string literals:
import { StatusEnum, RoleEnum } from './generated/client';
// Use in queries
await client.db.User.findMany({
where: { status: StatusEnum.ACTIVE },
});
// Use in application logic
function isEditable(role: string): boolean {
return role === RoleEnum.ADMIN || role === RoleEnum.EDITOR;
}
// Use in data creation
await client.db.User.create({
data: {
name: 'Bob',
status: StatusEnum.PENDING,
role: RoleEnum.VIEWER,
},
});Using the const object (StatusEnum.ACTIVE) instead of string literals ('ACTIVE') means your IDE catches typos immediately and renames propagate automatically when you change an enum value.
Default Values
Use @default with a string value matching one of the enum's variants:
enum Status { ACTIVE, INACTIVE, PENDING }
model User {
id Record @id
name String
status Status @default('ACTIVE')
}When status is omitted during creation, it automatically gets the default value:
const user = await client.db.User.create({
data: { name: 'Alice' },
});
console.log(user.status); // 'ACTIVE'Enums in Objects
Enum fields work identically inside embedded object types:
enum Severity { LOW, MEDIUM, HIGH, CRITICAL }
object TaskMeta {
priority Severity
tags Severity[]
}
model Task {
id Record @id
title String
meta TaskMeta
}await client.db.Task.create({
data: {
title: 'Fix bug',
meta: {
priority: 'HIGH',
tags: ['HIGH', 'CRITICAL'],
},
},
});See Filtering and Ordering for query operations on enum fields.
Inheritance with Extends
Enums support the extends keyword for building on existing enums. The child enum inherits all values from the parent and can add new ones:
enum BaseRole { VIEWER, EDITOR }
enum AdminRole extends BaseRole {
ADMIN
SUPER_ADMIN
}
# AdminRole = VIEWER, EDITOR, ADMIN, SUPER_ADMINDuplicate values are automatically deduplicated. You can also use pick and omit syntax to selectively inherit values. See the Extends page for full details.
Enums vs Literals
If you only need a fixed set of string values with a runtime const object, enums are the right choice. If you need mixed types (numbers, booleans, objects, tuples) or composition via inline references, use a literal instead.
See Enums vs Literals for a detailed comparison.