Callbacks & plugins
The six default callback chains, how to register around them, and the Plugin interface for session extensions.
Finishers don't execute SQL directly — they trigger a callback processor.
Callbacks are func(*DB) steps in an ordered, name-addressable list, and
everything the ORM does on a finisher is expressed through six default
chains you can extend, replace, or remove.
// Callback is one step in a CRUD processor.
type Callback func(*DB)The six default chains
db.Callback() returns the session's CallbackManager, which exposes one
processor per operation kind (callbacks.go:236): Create(), Query(),
Update(), Delete(), Row(), and Raw().
Each chain is a fixed list of named steps (callbacks/chain.go):
| Processor | Steps, in order |
|---|---|
Create() | fusion:begin_tx, fusion:before_create, fusion:save_before_associations, fusion:create, fusion:save_after_associations, fusion:after_create, fusion:commit_or_rollback |
Query() | fusion:build_query, fusion:before_query, fusion:query, fusion:preload, fusion:after_query |
Update() | fusion:begin_tx, fusion:before_update, fusion:save_before_associations, fusion:update, fusion:save_after_associations, fusion:after_update, fusion:commit_or_rollback |
Delete() | fusion:begin_tx, fusion:before_delete, fusion:delete_before_associations, fusion:delete, fusion:after_delete, fusion:commit_or_rollback |
Row() | fusion:build_query, fusion:row |
Raw() | fusion:raw |
Three steps do the heavy lifting: fusion:begin_tx opens the implicit
transaction when the operation needs one, the fusion:create /
fusion:update / fusion:delete / fusion:query steps build and execute
the SQL, and fusion:commit_or_rollback closes the transaction. That last
step is marked a finalizer: it runs even when the chain stopped early
because of an error or a recovered panic, so the transaction boundary is
never skipped (callbacks.go:22, callbacks.go:53).
Model hooks are callbacks too
The fusion:before_* / fusion:after_* steps dispatch your model's
BeforeCreate(tx *DB) error-style methods — but only when the schema
detected them at parse time, so models without hooks pay nothing
(callbacks_default.go:125). The typed interfaces are BeforeCreater,
AfterCreate, BeforeUpdater, AfterUpdater, BeforeDeleter,
AfterDeleter, BeforeFinder, and AfterFinder (hook.go:11).
Extending a chain
Registration is name-addressed with anchor-based insertion. To run a step immediately before the insert itself:
err := db.Callback().Create().Before("fusion:create", "my:audit", func(tx *fusion.DB) {
// runs after hooks, before the INSERT is built and executed
})The full CallbackProcessor API (callbacks.go:37):
| Method | Effect |
|---|---|
Register(name, fn) | Appends a step; duplicate names are an error. |
Before(anchor, name, fn) / After(anchor, name, fn) | Inserts a step next to an anchor. |
Replace(name, fn) | Swaps a step's implementation. |
Remove(name) | Deletes a step. |
Match(pred) | Attaches a func(*DB) bool predicate to the most recently registered step; it runs only when the predicate holds. |
MarkFinalizer(name) | Flags a step to run even after an error or panic. |
Execute(db) | Runs the chain (called by finishers, not by you). |
Failed registrations are reported through errors, not panics:
ErrDuplicateCallback for taken names, ErrCallbackNotFound for missing
anchors (callbacks.go:10).
Registration is shared
The processors live on the shared Config, so changes made through
db.Callback() affect every derivative of the session. Register callbacks
at startup, before serving traffic.
When defaults are registered
Open builds the manager and walks the six default chains, registering
each named step and marking fusion:commit_or_rollback as the finalizer —
so a fresh session is ready to run before you touch it
(callbacks.go:203-232).
How execution works
Execute walks the chain in order and stops at the first step that leaves
the error slot set (callbacks.go:132). A panic in a step is recovered only
long enough to set ErrCallbackPanic on the error slot (if empty), run the
finalizers, and re-throw the original value (callbacks.go:136-144):
// ErrCallbackPanic records a callback panic on db.Error so the chain's
// finalizers release the transaction before the panic is re-thrown.Plugins
For larger extensions, the Plugin interface hooks into session lifecycle
(fusion.go:43):
type Plugin interface {
Name() string
Initialize(*DB) error
}Register at runtime with db.Use(plugin) — Initialize runs first, then
the plugin is stored under its name; a duplicate name is
fusion.ErrRegistered (fusion.go:315). Or pre-register through
Config.Plugins, a map[string]Plugin whose entries are initialized during
Open (fusion.go:91, fusion.go:229):
db, err := fusion.Open(sqlite.Open("app.db"), &fusion.Config{
Plugins: map[string]fusion.Plugin{
"tracer": &TracerPlugin{},
},
})Inside Initialize, a plugin receives the live *DB: it can register
callbacks, wrap Config.ConnPool, add QueryHooks, or store state on
Statement.Settings. If Initialize fails during Open, the pool is
closed and the error is wrapped with the plugin's name (fusion.go:230).
What a callback must honor
Callbacks are plain functions over the chain's *DB, so the usual rules
apply: read clauses from tx.Statement, add errors with tx.AddError
(never panic), and don't stash the statement for later — a *DB is valid
for the duration of the operation, and the finalizers run regardless of how
the chain exits.