Overview
Zero provides five built-in column types for defining table schemas. Each type maps to both TypeScript types and PostgreSQL storage types.Column Type Functions
string()
Defines a string column.T- (Optional) A custom TypeScript string literal type or union
ColumnBuilder for a string column
Example:
number()
Defines a numeric column.T- (Optional) A custom TypeScript number type
ColumnBuilder for a number column
Example:
boolean()
Defines a boolean column.T- (Optional) A custom TypeScript boolean type
ColumnBuilder for a boolean column
Example:
json()
Defines a JSON column for storing structured data.T- (Optional) A custom TypeScript type for the JSON structure
ColumnBuilder for a JSON column
Example:
enumeration()
Defines a string column with TypeScript enum-like type constraints.T- A TypeScript string literal type or union (required)
ColumnBuilder for an enumeration column
Example:
ColumnBuilder Methods
optional()
Marks a column as optional (nullable).from()
Maps the client-side column name to a different server-side column name.serverName- The server-side column name in PostgreSQL
Column Type Namespace
All column types are also available via thecolumn namespace:
Type Mapping
| Zero Type | TypeScript Type | PostgreSQL Type |
|---|---|---|
string() | string | TEXT, VARCHAR, CHAR |
number() | number | INTEGER, BIGINT, NUMERIC, FLOAT |
boolean() | boolean | BOOLEAN |
json() | ReadonlyJSONValue | JSON, JSONB |
enumeration<T>() | T extends string | TEXT, VARCHAR |
Custom Types
You can provide custom TypeScript types to any column for better type safety:Complete Example
Here’s a comprehensive example showing all column types:Best Practices
- Use enumeration() for constrained strings: Prefer
enumeration<T>()overstring()when values are limited to a specific set - Mark optional fields explicitly: Use
.optional()for nullable columns to match your PostgreSQL schema - Type JSON structures: Always provide a type parameter to
json()for better type safety - Use number for timestamps: Store Unix timestamps as
number()columns - Consider branded types: Use TypeScript branded types for IDs to prevent mixing different ID types