Schema Definition
Define type-safe database schemas with a fluent, chainable API for models, fields, and relations
Overview
import { s } from "viborm";
const user = s.model({
id: s.string().id().ulid(),
email: s.string().unique(),
name: s.string().nullable(),
posts: s.oneToMany(() => post),
});Key Concepts
The s Builder
All schema definitions use the s builder object:
import { s } from "viborm";
s.model({ ... }) // Create a model
s.string() // String field
s.int() // Integer field
s.oneToMany(() => post) // One-to-many relation
s.manyToOne(() => user) // Many-to-one relation
// ... and moreChainable API
Every method returns a new instance, enabling fluent definitions:
s.string() // Basic string
.nullable() // Can be null
.unique() // Unique constraint
.default("value") // Default value
.map("column_name") // Custom column nameType Inference
Types are inferred automatically from your schema:
const user = s.model({
id: s.string().id(),
email: s.string(),
age: s.int().nullable(),
});
// Inferred type:
// { id: string; email: string; age: number | null }