Documentation / @zerotal/orm / index
index
A Bun-native Active Record ORM built on Bun.sql.
Models extend BaseModel: you declare columns with `@column`, relationships with decorators like `@hasMany` and `@belongsTo`, and then query and persist through the model's static and instance methods. Under the hood a QueryBuilder routes every value through parameterised bindings and forces interpolated identifiers through an allowlist; ModelQueryBuilder adds model hydration, eager loading, and relationship-existence queries on top. Schema changes are authored as migrations using the Schema facade and the Blueprint table builder.
Mass assignment is guarded by default — a model with neither fillable nor
guarded declared rejects all attributes in `fill()`.
Soft deletes and state machines are opt-in mixins composed via
BaseModelWith. The ORM's CLI commands (migrate, make:model, …) live
under the @zerotal/orm/commands subpath.
Examples
Define a model
import { BaseModel, column, hasMany, type HasMany } from "@zerotal/orm";
import { Post } from "./Post.ts";
export class User extends BaseModel {
@column({ primary: true }) id!: number;
@column() email!: string;
@column() name!: string;
@hasMany(() => Post) posts!: HasMany<Post>;
}
Query, create, and eager-load
const active = await User.query()
.where("active", true)
.with("posts")
.orderBy("name")
.paginate(20, 1); // 20 per page, page 1
const user = await User.create({ email: "a@b.com", name: "Ada" });
user.name = "Ada L.";
await user.save();
A migration
import { Migration, Schema } from "@zerotal/orm";
export default class extends Migration {
async up() {
await Schema.create("users", (table) => {
table.increments("id");
table.string("email").unique();
table.timestamps();
});
}
async down() {
await Schema.drop("users");
}
}
Remarks
Runs on Bun ≥ 1.1 via Bun.sql; SQLite, Postgres, and MySQL dialects are
supported. Register DatabaseProvider to wire the ORM into an application.
Casts
Database
- QueryExecuted
- NPlusOneDetected
- TransactionStarted
- TransactionCommitted
- TransactionRolledBack
- MigrationRan
- ModelChanged
Other
Model
Renames and re-exports BaseModel
Relationships
- belongsTo
- hasMany
- hasManyThrough
- hasOne
- hasOneThrough
- manyToMany
- morphMany
- morphOne
- morphTo
- morphToMany
- morphedByMany