Fields
Fields Overview
Scalar field types for defining database columns
Fields
Fields represent scalar database columns. Each field type has specific chainable methods and maps to appropriate database types.
Field Types
| Field | TypeScript | PostgreSQL | MySQL | SQLite |
|---|---|---|---|---|
s.string() | string | TEXT | VARCHAR(191) | TEXT |
s.int() | number | INTEGER | INT | INTEGER |
s.float() | number | DOUBLE PRECISION | DOUBLE | REAL |
s.decimal() | number | DECIMAL(65,30) | DECIMAL(65,30) | REAL |
s.bigInt() | bigint | BIGINT | BIGINT | INTEGER |
s.boolean() | boolean | BOOLEAN | TINYINT(1) | INTEGER |
s.dateTime() | Date | TIMESTAMP | DATETIME | TEXT |
s.json() | unknown / T | JSONB | JSON | TEXT |
s.blob() | Uint8Array | BYTEA | BLOB | BLOB |
s.enum([...]) | Union type | ENUM | ENUM | TEXT |
s.vector() | number[] | vector | - | - |
Common Modifiers
All field types support these chainable methods:
s.string()
.nullable() // Allow NULL values
.array() // Make it an array (native on PG, JSON on MySQL/SQLite)
.id() // Mark as primary key
.unique() // Unique constraint
.default(value) // Static default
.default(() => value) // Runtime default function
.validator(schema) // Custom StandardSchema validator
.map("column_name") // Custom database column nameModifier Reference
| Method | Description | Affects Type |
|---|---|---|
.nullable() | Allow NULL | T | null |
.array() | Array type | T[] |
.id() | Primary key | - |
.unique() | Unique constraint | - |
.default(v) | Default value | Optional in create |
.validator(s) | Custom validation | - |
.map(name) | Column name | - |
Field State
Every field tracks its state through generics:
interface FieldState {
type: ScalarFieldType;
nullable: boolean;
array: boolean;
hasDefault: boolean;
isId: boolean;
isUnique: boolean;
defaultValue: any;
autoGenerate: AutoGenerateType | undefined;
customValidator: StandardSchemaV1 | undefined;
columnName: string | undefined;
}