What is fusion-orm?
A Go ORM that pairs a SQL-first query engine with a fluent chaining API, a callback pipeline, and first-class generics.
fusion-orm is a Go ORM built on database/sql. It pairs bun's SQL-first
query engine — zero-allocation byte-slice builders, dialect feature flags,
precompiled field closures, cached schema reflection — with gorm's fluent
chaining and callback pipeline: copy-on-write sessions, a clause registry,
sentinel errors, and a generics facade.
db, err := fusion.Open(sqlite.Open("app.db"))
user := User{Name: "Ada"}
db.Create(&user)
var got User
db.Model(&User{}).Where("name = ?", "Ada").First(&got)Every chainable method returns a derivative *DB; nothing mutates the
receiver. Every finisher reports through exactly two fields — Error and
RowsAffected — and every branchable failure is an errors.Is-compatible
sentinel.
The mental model
A query moves through four layers, and the docs follow the same path:
- Chain —
Where,Order,Preload, … accumulate clauses on a per-chainStatement(copy-on-write). - Finish —
Create,Find,Delete, … trigger a callback processor. - Build — the processor renders registered clauses into a reused
[]bytethrough per-dialect appenders. - Execute — the statement runs through
database/sql(or the prepared statement cache), and rows scan back through compiled per-field closures.
Because SQL is built as bytes and rendered by the dialect, you can always ask
what will run: ToSQL interpolates a chain into a string, DryRun captures
SQL and vars without executing, and the logger traces every statement.
Feature highlights
- Query first — chainable
Where/Or/Order/Limit/Offset/Select/Distinct, struct and map conditions,Scopes, rawClauses, stableToSQL. - CRUD parity —
Create/Save/Updates/Delete/First/Take/Last/Find,FindInBatches,FirstOrInit/FirstOrCreate,Pluck,Row/Rows/Scan. - Relations — batched nested
Preload, inlineJoins, anAssociationAPI, and association callbacks for has-one, has-many, belongs-to, many-to-many, and polymorphic relations. - Generics —
fusion.G[T]with checked chain and finisher interfaces. - Soft delete — embed
fusion.DeletedAt;UnscopedandRestore. - Prepared statements — optional LRU+TTL cache with
ErrBadConnrecovery. - Error translation — driver constraint errors map onto sentinels.
- Migrations —
AutoMigrateplus versioned Go and numbered SQL migrations guarded by a lock table. - Observability —
QueryHookwrappers,SlogQueryHook, slog logger adapter, slow-query tracing.
Dialects
SQLite (pure Go, modernc.org/sqlite) and PostgreSQL (pgx/v5) ship
in-tree. Dialects declare capabilities as a feature bitset — query builders
branch on capability, never on dialect-name strings.