Schema API Reference
Complete reference for schema definition APIs
Complete reference for all schema definition APIs.
Create a new model:
const user = s.model({
id: s.string().id(),
name: s.string(),
});
| Method | Description |
|---|
.map(name) | Set table name |
.index(fields, options?) | Add index |
.id(fields, options?) | Compound primary key |
.unique(fields, options?) | Compound unique constraint |
.extends(fields) | Add fields |
s.string()
s.string(TYPES.PG.STRING.VARCHAR(255))
| Method | Description |
|---|
.id() | Mark as primary key |
.unique() | Unique constraint |
.nullable() | Allow NULL |
.array() | Array type |
.default(value) | Default value |
.validator(schema) | Custom validation |
.map(name) | Column name |
.uuid() | Auto-generate UUID |
.ulid() | Auto-generate ULID |
.nanoid() | Auto-generate NanoID |
.cuid() | Auto-generate CUID |
s.int()
s.float()
s.decimal()
s.bigInt()
| Method | Description |
|---|
.id() | Primary key |
.unique() | Unique constraint |
.nullable() | Allow NULL |
.array() | Array type |
.default(value) | Default value |
.increment() | Auto-increment (int only) |
| Method | Description |
|---|
.nullable() | Allow NULL |
.default(value) | Default value |
| Method | Description |
|---|
.nullable() | Allow NULL |
.default(value) | Default value |
.now() | Default to current time |
.updatedAt() | Auto-update on change |
s.json()
s.json(zodSchema)
| Method | Description |
|---|
.nullable() | Allow NULL |
.default(value) | Default value |
s.enum(["VALUE1", "VALUE2"])
| Method | Description |
|---|
.nullable() | Allow NULL |
.default(value) | Default value |
| Method | Description |
|---|
.nullable() | Allow NULL |
| Method | Description |
|---|
.nullable() | Allow NULL |
VibORM uses a config-first, getter-last pattern for relations:
s.relation.config().type(() => targetModel)
s.relation.oneToOne(() => targetModel)
// With config
s.relation
.fields("userId")
.references("id")
.optional()
.oneToOne(() => targetModel)
s.relation.oneToMany(() => targetModel)
No FK config needed — FK is on the many side.
s.relation
.fields("authorId")
.references("id")
.manyToOne(() => targetModel)
s.relation.manyToMany(() => targetModel)
// With junction table config
s.relation
.through("post_tags")
.A("post_id")
.B("tag_id")
.manyToMany(() => targetModel)
| Method | Description |
|---|
.fields(...names) | FK fields on current model |
.references(...names) | Referenced fields on target |
.optional() | Nullable relation (to-one only) |
.onDelete(action) | Delete behavior |
.onUpdate(action) | Update behavior |
.through(tableName) | Junction table (many-to-many) |
.A(columnName) | Source FK column (many-to-many) |
.B(columnName) | Target FK column (many-to-many) |
| Action | Description |
|---|
"cascade" | Delete/update related |
"setNull" | Set FK to null |
"restrict" | Prevent operation |
"noAction" | Database default |
.index(fields, {
name: "idx_name",
type: "btree",
unique: false,
where: "condition",
})
| Option | Values |
|---|
name | Custom index name |
type | "btree", "hash", "gin", "gist" |
unique | true, false |
where | Partial index condition |