Documentation
¶
Overview ¶
Package gorm provides a GORM database connection factory with connection pooling, structured logging, and retry support.
Supported dialectors: postgres, mysql, mariadb, sqlite, sqlserver, mssql.
Basic usage ¶
db, err := gorm.New(&gorm.GormConfig{
DSN: "host=localhost user=app password=secret dbname=mydb sslmode=disable",
Dialector: "postgres",
})
if err != nil {
log.Fatal(err)
}
defer func() {
sqlDB, _ := db.DB()
_ = sqlDB.Close()
}()
Starting from defaults ¶
DefaultGormConfig returns a sensible starting point with 50 open/idle connections, 1 h lifetime, prepared statement caching, and info-level logging.
cfg := gorm.DefaultGormConfig()
cfg.DSN = os.Getenv("DATABASE_URL")
cfg.Dialector = "postgres"
db, err := gorm.New(cfg)
Connection pool inspection ¶
stats, err := gorm.GetConnectionStats(db) fmt.Println(stats.OpenConnections)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidDatabaseConfig = errors.New("invalid database configuration") ErrDatabaseDSNRequired = errors.New("database dsn is required") ErrDatabaseDialectorRequired = errors.New("database dialector is required") ErrUnsupportedDialector = errors.New("unsupported database dialector") ErrDatabaseConnectionFailed = errors.New("failed to open database connection") ErrDatabasePoolAccessFailed = errors.New("failed to access database connection pool") )
Functions ¶
func GetConnectionStats ¶
GetConnectionStats returns current connection pool statistics
func New ¶
func New(cfg *GormConfig) (*gorm.DB, error)
New initializes a GORM database connection with the given configuration.
func ParseLoggerLevel ¶
ParseLoggerLevel maps the logger level string to gorm's LogLevel
func UpdateConnectionPool ¶
func UpdateConnectionPool(db *gorm.DB, cfg *GormConfig) error
UpdateConnectionPool updates the connection pool settings for an existing connection
Types ¶
type GormConfig ¶
type GormConfig struct {
DSN string // Data Source Name for the database connection
Dialector string // Database dialector (e.g., "postgres", "mysql", "sqlite", "sqlserver")
LogLevel string // Log level for GORM (e.g., "silent", "info", "error", "warning")
MaxOpenConns int // Maximum number of open connections to the database
MaxIdleConns int // Maximum number of connections in the idle connection pool
SkipDefaultTx bool // Skip default transaction for single create, update, delete operations
PrepareStmt bool // Executes the given query in cached statement
DryRun bool // Generate SQL without executing
ConnMaxLifetime time.Duration // Maximum amount of time a connection may be reused
ConnMaxIdleTime time.Duration // Maximum amount of time a connection may be idle before being closed
SlowThreshold time.Duration // Threshold for logging slow queries
// Connection retry
RetryAttempts int // Number of retry attempts for connection failures
RetryDelay time.Duration // Delay between retry attempts
// GORM Config override
GormConfig *gorm.Config
}
GormConfig holds database configuration
func DefaultGormConfig ¶
func DefaultGormConfig() *GormConfig
DefaultGormConfig returns a GormConfig with sensible defaults