VibORM

Schema API Reference

Complete reference for schema definition APIs

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

Relations

VibORM uses a config-first, getter-last pattern for relations:

s.relation.config().type(() => targetModel)

One-to-One

s.relation.oneToOne(() => targetModel)

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

One-to-Many

s.relation.oneToMany(() => targetModel)

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

Many-to-One

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

Many-to-Many

s.relation.manyToMany(() => targetModel)

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

Relation Config Methods

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

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