docs
Connection & Config

Connection & Config

Configure the connection pool, logging, and query behavior.

Options Reference

db, err := gormicx.Open("postgres", dsn,
    gormicx.WithMaxOpenConns(50),              // max total connections
    gormicx.WithMaxIdleConns(10),              // max idle connections
    gormicx.WithConnMaxLifetime(30*time.Minute), // max connection age
    gormicx.WithConnMaxIdleTime(5*time.Minute),  // max idle time
    gormicx.WithLogLevel(logger.Debug),        // log level
    gormicx.WithLogger(myLogger),              // custom logger
    gormicx.WithPrepareStmt(true),             // prepared statement cache
    gormicx.WithDryRun(true),                  // log queries, don't execute
    gormicx.WithTablePrefix("app_"),           // prefix all table names
    gormicx.WithSingularTable(true),           // disable pluralisation
)
OptionDefaultDescription
WithMaxOpenConns(n)driver defaultMax open connections in pool
WithMaxIdleConns(n)driver defaultMax idle connections in pool
WithConnMaxLifetime(d)0 (no limit)Max age of a connection
WithConnMaxIdleTime(d)0 (no limit)Max idle time before close
WithLogLevel(l)logger.WarnMinimum log level
WithLogger(l)built-inCustom logger implementation
WithPrepareStmt(b)falseCache prepared statements
WithDryRun(b)falseLog queries without executing
WithTablePrefix(s)""Prefix added to all table names
WithSingularTable(b)falseUse singular table names

MustOpen

Panics on error — useful in main() init:

db := gormicx.MustOpen("postgres", dsn, gormicx.WithMaxOpenConns(20))

Connection Pool Stats

stats := db.Stats()
fmt.Printf(
    "open=%d idle=%d in_use=%d wait_count=%d\n",
    stats.OpenConns,
    stats.IdleConns,
    stats.InUseConns,
    stats.WaitCount,
)