Schema API Reference
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 |
.dimension(n) | Set vector dimension |
VibORM uses a chainable API where the thunk comes first:
s.manyToOne(() => targetModel).fields("fk").references("id")
s.oneToOne(() => targetModel)
// With config
s.oneToOne(() => targetModel)
.fields("userId")
.references("id")
.optional()
s.oneToMany(() => targetModel)
No FK config needed — FK is on the many side.
s.manyToOne(() => targetModel)
.fields("authorId")
.references("id")
s.manyToMany(() => targetModel)
// With junction table config
s.manyToMany(() => targetModel)
.through("post_tags")
.A("post_id")
.B("tag_id")
| Method | Description |
|---|
.fields(...names) | FK fields on current model |
.references(...names) | Referenced fields on target |
.optional() | Nullable relation |
.onDelete(action) | Delete behavior |
.onUpdate(action) | Update behavior |
.name(relationName) | Custom relation name |
| Method | Description |
|---|
.name(relationName) | Custom relation name |
| Method | Description |
|---|
.through(tableName) | Junction table name |
.A(columnName) | Source FK column |
.B(columnName) | Target FK column |
.onDelete(action) | Delete behavior (both FKs) |
.onUpdate(action) | Update behavior (both FKs) |
.name(relationName) | Custom relation name |
| 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 |