[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/orm](../README) / index

# index

A Bun-native Active Record ORM built on `Bun.sql`.

Models extend [BaseModel](classes/BaseModel): you declare columns with [\`@column\`](functions/column),
relationships with decorators like [\`@hasMany\`](functions/hasMany) and
[\`@belongsTo\`](functions/belongsTo), and then query and persist through the model's
static and instance methods. Under the hood a [QueryBuilder](classes/QueryBuilder) routes every
value through parameterised bindings and forces interpolated identifiers through
an allowlist; [ModelQueryBuilder](classes/ModelQueryBuilder) adds model hydration, eager loading, and
relationship-existence queries on top. Schema changes are authored as migrations
using the [Schema](variables/Schema) facade and the [Blueprint](classes/Blueprint) table builder.

Mass assignment is **guarded by default** — a model with neither `fillable` nor
`guarded` declared rejects all attributes in [\`fill()\`](classes/BaseModel#fill).
Soft deletes and state machines are opt-in mixins composed via
[BaseModelWith](functions/BaseModelWith). The ORM's CLI commands (`migrate`, `make:model`, …) live
under the `@zerotal/orm/commands` subpath.

## Examples

**Define a model**

```ts
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**

```ts
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**

```ts
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

- [CastContract](interfaces/CastContract)
- [Cast](classes/Cast)
- [JsonCast](classes/JsonCast)
- [ArrayCast](classes/ArrayCast)
- [json](functions/json)
- [objectOf](functions/objectOf)
- [arrayOf](functions/arrayOf)

## Database

- [QueryExecuted](classes/QueryExecuted)
- [NPlusOneDetected](classes/NPlusOneDetected)
- [TransactionStarted](classes/TransactionStarted)
- [TransactionCommitted](classes/TransactionCommitted)
- [TransactionRolledBack](classes/TransactionRolledBack)
- [MigrationRan](classes/MigrationRan)
- [ModelChanged](classes/ModelChanged)

## Other

### Model

Renames and re-exports [BaseModel](classes/BaseModel)

## Relationships

- [belongsTo](functions/belongsTo)
- [hasMany](functions/hasMany)
- [hasManyThrough](functions/hasManyThrough)
- [hasOne](functions/hasOne)
- [hasOneThrough](functions/hasOneThrough)
- [manyToMany](functions/manyToMany)
- [morphMany](functions/morphMany)
- [morphOne](functions/morphOne)
- [morphTo](functions/morphTo)
- [morphToMany](functions/morphToMany)
- [morphedByMany](functions/morphedByMany)

## Select

- [QueryBuilder](classes/QueryBuilder)
