@bunnykit/orm
v0.3.9
Published
An Eloquent-inspired ORM for Bun's native SQL client supporting SQLite, MySQL, and PostgreSQL.
Maintainers
Readme
Bunny
Bun-only package. Install with:
bun add @bunnykit/ormnpm, yarn, pnpm, and Node.js runtime usage are not supported.
An Eloquent-inspired ORM built specifically for Bun's native bun:sql client. It ships with zero runtime dependencies and supports SQLite, MySQL, and PostgreSQL with full TypeScript typing, a chainable query builder, schema migrations, model observers, polymorphic relations, and an interactive REPL.
Features
- 🔥 Bun-native — Built on top of
bun:sqlfor maximum performance - 🪶 Zero runtime dependencies — No package lock-in beyond Bun itself
- 📦 Multi-database — SQLite, MySQL, and PostgreSQL support
- 🔷 Fully Typed —
Model.define<T>()gives attribute access, typedwith()autocomplete, and typed eager-load results with zero codegen - 🏗️ Schema Builder — Programmatic table creation, indexes, foreign keys
- 🔍 Query Builder — Chainable
where,join,orderBy,groupBy, date filters, conditional building, etc. - 🧠 Tagged Cache — Redis-backed cache facade, query
remember(), and exact tag invalidation - 📣 Events — Application-level event dispatcher with function listeners and class handlers
- 🐇 Queue Jobs — Database- and Redis-backed background job queue with named queues, retries, delays, and a
bunny queueworker - 🛠️ Commands — Artisan-style CLI commands with a signature DSL, argument/option parsing, and
bunny run - 🧬 Eloquent-style Models — Property attributes, defaults, casts, dirty tracking, soft deletes, scopes, find-or-fail, first-or-create
- 🧺 Collections — Laravel-style helpers for multi-record query results
- 🔗 Relations — Standard, many-to-many, polymorphic, through, one-of-many, and relation queries
- 👁️ Observers — Lifecycle hooks (
creating,created,updating,updated, etc.) - 🚀 Migrations & CLI — Create, run, reset, refresh, and inspect migrations from the command line
- 🌱 Seeders & Factories — Run all seeders or target one seeder by name/file, plus lightweight model factories
- 💬 REPL — Inspect models and run queries interactively with
bunny repl - ⚡ Streaming —
chunk,chunkById,cursor,each,eachById, andlazyfor memory-efficient large dataset processing - 🏢 Multi-tenant — Database-per-tenant, schema-per-tenant, and RLS strategies with
DB.tenant()andTenantContext
Installation
bun add @bunnykit/ormSee Installation for details.
Quickstart
Define a model with Model.define<T>() to get full IntelliSense on attributes, query builder columns, and eager-load results:
import { Model } from "@bunnykit/orm";
interface UserAttributes {
id: number;
name: string;
email: string | null;
created_at: string;
updated_at: string;
}
class User extends Model.define<UserAttributes>("users") {
posts() {
return this.hasMany(Post);
}
}
class Post extends Model.define<{ id: number; user_id: number; title: string }>("posts") {
author() {
return this.belongsTo(User);
}
}Point the ORM at a database and run a query:
import { Connection, Model } from "@bunnykit/orm";
Model.setConnection(new Connection({ url: "sqlite://app.db" }));
const users = await User.where("email", "[email protected]")
.with("posts")
.get();
for (const user of users) {
console.log(user.name, user.posts.length);
}Or use the DB facade for ad-hoc table access without a model:
import { DB } from "@bunnykit/orm";
const rows = await DB.table("audit_logs")
.where("event", "login")
.orderBy("created_at", "desc")
.limit(10)
.get();See the Quickstart guide for the full walkthrough.
Documentation
Getting Started
| Topic | Summary | |---|---| | Installation | Add the package to your Bun project. | | Configuration | Connection, tenancy, migrations, seeders, type generation. | | Quickstart | End-to-end walkthrough: install → config → migration → model → query. |
Database
| Topic | Summary |
|---|---|
| Schema Builder | Tables, columns, indexes, foreign keys. |
| Migrations | Versioned schema changes, rollback, multi-tenant scopes, auto-create database / schema. |
| Seeders | Populate development and test data. |
| Transactions | connection.transaction() and nested savepoints. |
Querying
| Topic | Summary |
|---|---|
| Query Builder | Chainable where / join / with / aggregates, DB facade, raw queries. |
| Cache | Redis-backed cache API, query remember(), exact tag invalidation. |
| Collections | map, filter, groupBy, and other helpers returned by get(). |
| Models | Defining models, casts, accessors, soft deletes, persistence. |
| Relationships | hasMany, belongsTo, belongsToMany, polymorphic, eager loading. |
| Validation | Typed Laravel-style validator with fluent rules and tenant-aware database checks. |
TypeScript
| Topic | Summary |
|---|---|
| TypeScript | Model.define<T>(), typed builders, scope and accessor typing. |
| Type Generation | Generate attribute interfaces from your database schema. |
Background Processing
| Topic | Summary |
|---|---|
| Queue Jobs | Dispatch jobs to named queues, run workers with bunny queue, retries, delays, failed-job tracking, database and Redis drivers. |
| Commands | Artisan-style CLI commands with signature DSL, argument/option parsing, output helpers, and bunny run. |
Advanced
| Topic | Summary |
|---|---|
| SvelteKit Helper | Typed route model binding and action validation helpers for +page.server.ts. |
| Policies | Register model/resource policies, use can / authorize, and enforce access in RouteBuilder. |
| Observers | Lifecycle hooks for creating, updating, deleting, and more. |
| Events | Application-level events with function listeners, class handlers, and temporary subscriptions. |
| Library Usage | Programmatic API via configureBunny(). |
| Testing | In-memory SQLite and transactional test isolation. |
The full index lives at docs/README.md.
License
MIT
