Documentation
¶
Overview ¶
Package xgorm manages named GORM databases and their connection pools. Initialize databases at startup and stop their consumers before calling CloseAll.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
Close removes and closes a named database, returning nil if the alias is absent. Stop its consumers first; pool closure cannot stop arbitrary application goroutines.
func CloseAll ¶
func CloseAll() error
CloseAll removes every registered database and attempts to close each pool. Call it after all database consumers have stopped. Errors retain their database aliases.
func Get ¶
Get returns the shared database for alias, or nil if no database is registered. Use WithContext or Session for request-specific settings; do not mutate or close the shared root.
func Init ¶
Init initializes and health-checks a batch of databases before registering it. Failure leaves the registry unchanged and closes pools created by this call. ctx controls context-aware initialization work and is not retained by registered databases. Initialize during startup and do not overlap initialization with registry shutdown.
Example ¶
config := Config{Alias: "example", Driver: DriverSQLite, DSN: ":memory:", PingTimeout: 1, MaxOpenConns: 1, MaxIdleConns: 1}
if err := Init(context.Background(), []Config{config}); err != nil {
panic(err)
}
defer Close("example")
db := Get("example")
if err := db.AutoMigrate(&exampleRecord{}); err != nil {
panic(err)
}
if err := db.Transaction(func(tx *gorm.DB) error { return tx.Create(&exampleRecord{Name: "Ada"}).Error }); err != nil {
panic(err)
}
var record exampleRecord
if err := db.First(&record).Error; err != nil {
panic(err)
}
fmt.Println(record.Name)
Output: Ada
Types ¶
type Config ¶
type Config struct {
// Alias identifies the database in the package registry and must be unique.
Alias string `json:"alias"`
// Driver selects the SQL dialect and driver.
Driver Driver `json:"driver"`
// DSN is the driver-specific data source name and may contain credentials.
DSN string `json:"dsn"`
// Debug enables GORM's info-level SQL logging.
Debug bool `json:"debug"`
// GORM supplies fresh ORM settings such as logging, naming, migrations, and plugins.
// ConnPool and Dialector must be unset; xgorm owns connection creation and shutdown.
// ClauseBuilders and Plugins maps are copied, but their values and other referenced objects are shared.
// Shared plugins and callbacks must be safe for their intended concurrent use.
// Pool wrappers must expose the original SQL pool through GORM's GetDBConnector interface.
// Automatic GORM Ping is always disabled in favor of the bounded startup check.
GORM *gorm.Config `json:"-"`
// PingTimeout bounds context-aware startup checks, including MySQL version detection.
// Driver operations or plugins that ignore context may not honor this timeout.
PingTimeout int `json:"ping_timeout"`
// MaxOpenConns limits the number of open connections and must be positive.
MaxOpenConns int `json:"max_open_conns"`
// MaxIdleConns limits retained idle connections; zero retains none.
MaxIdleConns int `json:"max_idle_conns"`
// ConnMaxLifetime limits connection reuse by age; zero disables this limit.
ConnMaxLifetime int `json:"conn_max_lifetime"`
// ConnMaxIdleTime limits how long a connection may remain idle; zero disables this limit.
ConnMaxIdleTime int `json:"conn_max_idle_time"`
}
Config defines a named database and its connection pool. Durations are expressed in seconds. Connection limits and PingTimeout are explicit; lifetime and idle-time limits of zero are disabled. Configuration and referenced objects must not be mutated during initialization.