Skip to Content
fusion-orm
Getting Started

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.Dialector

The 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.Dialector

What 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:

  1. Options apply first. Each Option mutates a fresh *Config.
  2. Defaults fill nil fields — schema.NamingStrategy{}, the default (Warn-level) logger, NowFunc defaulting to time.Now().Local(), and an empty plugin map.
  3. The pool opens. Unless you supplied Config.ConnPool yourself, the dialector's Initialize() runs and its *sql.DB becomes the session's ConnPool (fusion.go:199-205).
  4. PrepareStmt wraps the pool in the LRU+TTL prepared-statement cache (fusion.go:206-208).
  5. Plugins initialize — every entry in Config.Plugins runs Initialize(db); a failure closes the pool and aborts the open (fusion.go:229-234).
  6. Ping — unless DisableAutomaticPing is set, Open pings the pool if it implements Ping(); 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 (default time.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 unknown fusion tag keys instead of warning.
  • FullSaveAssociations — association saves use Save, not Create, for records that already carry a primary key.
  • AllowGlobalUpdate — permit Update/Delete without a WHERE clause.
  • SkipDefaultTransaction — drop the implicit per-write transaction.
  • DisableAutomaticPing — skip Open's ping.
  • DisableNestedTransaction — run nested Transaction calls 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.

Next

On this page