Connecting
Open a session with fusion.Open, a Dialector, and optional Config options.
fusion.Open(dialector, opts...) is the single entry point. It takes a
Dialector — sqlite.Open(dsn) or postgres.Open(dsn) — plus zero or more
Option values, and returns a root *DB.
// Open returns a Dialector that will open dsn (a file path, ":memory:", or a
// modernc DSN such as "file:test.db?_pragma=busy_timeout(5000)").
func Open(dsn string) dialect.DialectorThe PostgreSQL dialector has the same shape; its DSN is a URL such as
postgres://user:pass@localhost/db or a keyword/value string.
// Open returns a Dialector that will open dsn (a URL such as
// "postgres://user:pass@localhost/db" or a keyword/value string).
func Open(dsn string) dialect.DialectorWhat Open does, in order
// Open initializes a session over d. Options apply before defaults: any nil
// Config field falls back to its default (Warn-level logger, time.Now,
// empty plugin map), then the dialector opens its pool and Open pings it
// (unless DisableAutomaticPing).Concretely:
- Options apply first. Each
Optionmutates a fresh*Config. - Defaults fill nil fields —
schema.NamingStrategy{}, the default (Warn-level) logger,NowFuncdefaulting totime.Now().Local(), and an empty plugin map. - The pool opens. Unless you supplied
Config.ConnPoolyourself, the dialector'sInitialize()runs and its*sql.DBbecomes the session'sConnPool(fusion.go:199-205). PrepareStmtwraps the pool in the LRU+TTL prepared-statement cache (fusion.go:206-208).- Plugins initialize — every entry in
Config.PluginsrunsInitialize(db); a failure closes the pool and aborts the open (fusion.go:229-234). - Ping — unless
DisableAutomaticPingis set, Open pings the pool if it implementsPing(); failure closes the pool and returns an error (fusion.go:236-243).
A mismatch is also caught early: if TranslateError is set but the dialector
does not implement ErrorTranslator, Open logs a warning instead of failing
silently later (fusion.go:222-227).
Passing a Config
Option is a one-method interface, and *Config implements it — so a full
config can be passed directly to Open:
// Option configures Open. *Config implements Option, so a full config can be
// passed directly:
//
// db, err := fusion.Open(sqlite.Open("app.db"), &fusion.Config{Logger: l})type Option interface {
Apply(*Config)
}Config.Apply shallow-copies the config onto the working copy — the same
copy-on-write rule that governs session derivatives later.
A full Open example
db, err := fusion.Open(sqlite.Open("app.db"), &fusion.Config{
TranslateError: true,
PrepareStmt: true,
PreparedStmtCacheSize: 500,
StrictTags: true,
FullSaveAssociations: true,
})
if err != nil {
panic(err)
}Fields relevant at open time (the full reference lives in /docs/core-concepts/config):
Logger— receives info/warn/error messages and SQL traces.NowFunc— replaces the timestamp source (defaulttime.Now().Local()).TranslateError— maps driver constraint errors onto sentinels.PrepareStmt/PreparedStmtCacheSize/PreparedStmtTTL— enable and bound the prepared-statement cache (default cache size 1000, no TTL).StrictTags— reject unknownfusiontag keys instead of warning.FullSaveAssociations— association saves useSave, notCreate, for records that already carry a primary key.AllowGlobalUpdate— permitUpdate/Deletewithout a WHERE clause.SkipDefaultTransaction— drop the implicit per-write transaction.DisableAutomaticPing— skip Open's ping.DisableNestedTransaction— run nestedTransactioncalls inline instead of via savepoints.NamingStrategy— maps model/field names to table/column names (schema.Namer).ConnPool— bring your own pool; when set, the dialector never opens one.Plugins— registered before the session is handed to you; each is initialized during Open.
Getting the *sql.DB back
Need raw database/sql access — connection limits, a native ping, driver
statistics? The session keeps the pool reachable:
// DB returns the underlying *sql.DB. On a transaction instance it returns
// the root pool the transaction was begun from.func (db *DB) DB() (*sql.DB, error)It unwraps any wrapper (including the prepared-statement cache) before the
type assertion; if the pool is not a *sql.DB underneath, you get
fusion.ErrInvalidDB.
Ping is skipped for non-pingable pools
Open only pings pools that expose Ping() error at runtime. Custom
ConnPool wrappers without that method are simply not pinged — set
DisableAutomaticPing intentionally rather than relying on silence.