VibORM

Schema API Reference

Complete reference for all schema definition APIs

Model

s.model(fields)

Create a new model:

const user = s.model({
  id: s.string().id(),
  name: s.string(),
});

Model Methods

MethodDescription
.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

Field Types

String

s.string()
s.string(TYPES.PG.STRING.VARCHAR(255))
MethodDescription
.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

Number Types

s.int()
s.float()
s.decimal()
s.bigInt()
MethodDescription
.id()Primary key
.unique()Unique constraint
.nullable()Allow NULL
.array()Array type
.default(value)Default value
.increment()Auto-increment (int only)

Boolean

s.boolean()
MethodDescription
.nullable()Allow NULL
.default(value)Default value

DateTime

s.dateTime()
MethodDescription
.nullable()Allow NULL
.default(value)Default value
.now()Default to current time
.updatedAt()Auto-update on change

JSON

s.json()
s.json(zodSchema)
MethodDescription
.nullable()Allow NULL
.default(value)Default value

Enum

s.enum(["VALUE1", "VALUE2"])
MethodDescription
.nullable()Allow NULL
.default(value)Default value

Blob

s.blob()
MethodDescription
.nullable()Allow NULL

Vector

s.vector()
MethodDescription
.nullable()Allow NULL
.dimension(n)Set vector dimension

Relations

VibORM uses a chainable API where the thunk comes first:

s.manyToOne(() => targetModel).fields("fk").references("id")

One-to-One

s.oneToOne(() => targetModel)

// With config
s.oneToOne(() => targetModel)
  .fields("userId")
  .references("id")
  .optional()

One-to-Many

s.oneToMany(() => targetModel)

No FK config needed — FK is on the many side.

Many-to-One

s.manyToOne(() => targetModel)
  .fields("authorId")
  .references("id")

Many-to-Many

s.manyToMany(() => targetModel)

// With junction table config
s.manyToMany(() => targetModel)
  .through("post_tags")
  .A("post_id")
  .B("tag_id")

ToOne Methods (oneToOne, manyToOne)

MethodDescription
.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

ToMany Methods (oneToMany)

MethodDescription
.name(relationName)Custom relation name

ManyToMany Methods

MethodDescription
.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

Referential Actions

ActionDescription
"cascade"Delete/update related
"setNull"Set FK to null
"restrict"Prevent operation
"noAction"Database default

Index Options

.index(fields, {
  name: "idx_name",
  type: "btree",
  unique: false,
  where: "condition",
})
OptionValues
nameCustom index name
type"btree", "hash", "gin", "gist"
uniquetrue, false
wherePartial index condition

On this page