The layer model
How fusion-orm's packages split responsibilities — dialects, clauses, schema, sessions, and callbacks.
fusion-orm is built as a strict layer split: a high-performance
dialect/clause core (bun's engine model), a reflection-cached schema layer,
and a gorm-style session/callback API on top. The core never knows about
chaining; the API layer never assembles SQL directly. SQL is built as
[]byte through per-dialect appenders and executed through database/sql
(or the prepared-stmt cache wrapper).
[user code] → [chainable_api / finisher_api / generics G[T]]
↓ getInstance() (copy-on-write)
[Statement + clause registry]
↓ finisher triggers
[callback processor: create|query|update|delete|row|raw]
↓ ordered callbacks
[clause/ builders] → []byte via [dialect appenders]
↓ resolveConn()
[ConnPool | PreparedStmtDB cache] → database/sql
↑ scan via compiled ScannerFuncs
[schema layer: cached tables/fields/relations]The layers
dialect/ — the Dialector interface (Initialize, DataTypeOf,
BindVarTo, QuoteTo, ErrorTranslator) plus the feature bitflags. It owns
identifier quoting and placeholder rendering. This is the only package
allowed to contain dialect-specific strings — a ? placeholder or a
RETURNING clause spelling lives here and nowhere else.
clause/ — pure SQL clause builders: byte-append functions that bind vars
through the statement. No I/O, no schema imports. A clause is just "render
my part of the statement".
schema/ — cached per-type reflection: tables, fields, relations, hooks.
It owns tag parsing, naming, and the precompiled Appender/Scanner
closures that make building and scanning allocation-free. It never imports
DB, statement, or callback types.
statement.go + fusion.go — statement assembly, the typed clause
registry, session/config/clone semantics, and the single error slot. This is
the seam between "chainable API" and "engine".
callbacks/ — the only place that knows CRUD order. Each callback is a
func(*DB); the default create/query/update/delete chains are registered
here and remain replaceable.
Supporting packages — logger/, migrator/, hook.go (QueryHook), and
prepared_stmt.go (the LRU+TTL cache) — sit at the edges; no core package
imports their internals.
Why the boundaries matter to you
- Plugins extend, not fork. Observability, custom SQL, or replacement
callback behavior all arrive through
DB.Useanddb.Callback()registration — you never vendor or shadow a package to change behavior. - A new dialect is a package, not a patch. Implement the
Dialectorinterface, declare capability bitflags, add quotes indialect/<name>/— no core changes. - Rendering is deterministic. Because clause builders are pure byte
appenders and the dialect owns every string,
ToSQL/DryRunshow exactly what executes — the same rendering path the executor uses.