Skip to Content
fusion-orm
API Reference

Chainable methods

Every clause-accumulating method on *DB — exact signatures extracted from chainable_api.go and fusion.go.

Chainable methods accumulate clauses on a derivative *DB and never mutate the receiver, so a root *DB stays safe for concurrent use. Each method below lists its exact signature with a src: provenance comment, then its source doc comment (trimmed).

Setup chainables

These configure the session itself rather than the SQL statement.

Session(config *Session)

func (db *DB) Session(config *Session) *DB

Returns a derivative configured per config. Flags on config are one-way overrides onto a copied Config; the receiver's Config is never mutated. With NewDB false (default) the derivative inherits the current chain's statement. See Config reference for the Session fields.

WithContext(ctx context.Context)

func (db *DB) WithContext(ctx context.Context) *DB

Returns a derivative that runs every statement under ctx. A nil ctx becomes context.TODO().

Debug()

func (db *DB) Debug() *DB

Returns a derivative that logs every SQL trace at Info level.

QueryHook(hook QueryHook)

func (db *DB) QueryHook(hook QueryHook) *DB

Registers an observability hook on a derivative session. The hook is appended to the copied config's QueryHooks, so the receiver is untouched.

Statement chainables

All live in chainable_api.go.

Preload(query string, args ...any)

func (db *DB) Preload(query string, args ...any) *DB

Requests a relation (or nested dotted path) to be loaded in one batched query after the main query. Args may be a func(*DB) *DB apply function, a SQL condition with args, or primary-key values.

db.Model(&User{}).Preload("Posts").Preload("Posts.Comments").Find(&users)

Unscoped()

func (db *DB) Unscoped() *DB

Disables soft-delete filtering and DELETE rewriting for the statement, including relation preloads issued from it.

Joins(query string, args ...any)

func (db *DB) Joins(query string, args ...any) *DB

Adds a raw JOIN expression, or LEFT JOINs a named has-one/belongs-to relation and selects its columns as alias__column for dotted scanning. A query beginning with JOIN/LEFT/RIGHT/INNER/CROSS is raw; anything else is a relation name.

db.Model(&User{}).Joins("Profile").Find(&users)
db.Model(&User{}).Joins("LEFT JOIN profiles ON profiles.user_id = users.id").Find(&users)

Model(value any)

func (db *DB) Model(value any) *DB

Sets the model used to derive table/column metadata.

Table(name string)

func (db *DB) Table(name string) *DB

Overrides the table name for the statement.

Select(columns ...string)

func (db *DB) Select(columns ...string) *DB

Restricts the selected columns. A single comma-separated string is split into columns.

db.Model(&User{}).Select("id", "name").Find(&users)

Distinct()

func (db *DB) Distinct() *DB

Adds DISTINCT to the SELECT clause.

Where(query any, args ...any)

func (db *DB) Where(query any, args ...any) *DB

Adds an AND-joined condition. Struct and map values build equality expressions from their non-zero fields.

db.Model(&User{}).Where("name = ?", "Ada").Find(&users)
db.Model(&User{}).Where(map[string]any{"name": "Ada"}).Find(&users)
db.Model(&User{}).Where(&User{Name: "Ada"}).Find(&users)

Or(query any, args ...any)

func (db *DB) Or(query any, args ...any) *DB

Adds an OR-joined condition.

Order(value string)

func (db *DB) Order(value string) *DB

Appends an ORDER BY expression (e.g. name DESC). Comma-separated values are split into columns.

Limit(n int)

func (db *DB) Limit(n int) *DB

Caps the number of rows.

Offset(n int)

func (db *DB) Offset(n int) *DB

Skips rows.

Locking(enabled bool)

func (db *DB) Locking(enabled bool) *DB

Adds FOR UPDATE when enabled.

Clauses(conds ...clause.Interface)

func (db *DB) Clauses(conds ...clause.Interface) *DB

Registers custom clause builders, replacing same-named clauses. See Clause types for the interface.

Scopes(funcs ...func(*DB) *DB)

func (db *DB) Scopes(funcs ...func(*DB) *DB) *DB

Applies chain functions lazily at finisher time (gorm-compatible).

scope := func(tx *fusion.DB) *fusion.DB { return tx.Where("active = ?", true) }
db.Scopes(scope).Find(&users)

CreateBatchSize(size int)

func (db *DB) CreateBatchSize(size int) *DB

Sets the maximum rows per INSERT statement.

Attrs(values any)

func (db *DB) Attrs(values any) *DB

Supplies defaults used by FirstOrInit when no row is found.

Assign(values any)

func (db *DB) Assign(values any) *DB

Supplies values applied by FirstOrInit and FirstOrCreate.

Group and Having are not implemented

Group() and Having() appear in design notes but are not implemented in fusion-orm. Chaining them will not compile. Aggregates are expressed through Select with raw expressions (e.g. Select("COUNT(*) AS n")) and Pluck / Count.

Next

On this page