Documentation
¶
Index ¶
- Variables
- func NewGormDB(p Params) (*gorm.DB, error)
- type DatabaseConfig
- type DatabaseManager
- func (dm *DatabaseManager) Close() error
- func (dm *DatabaseManager) Connect() error
- func (dm *DatabaseManager) GetDB() *gorm.DB
- func (dm *DatabaseManager) GetPoolConfig() PoolConfig
- func (dm *DatabaseManager) GetPoolStats() (*sql.DBStats, error)
- func (dm *DatabaseManager) SetPoolConfig(config PoolConfig) error
- type DatabaseType
- type GormConfig
- type LogConfig
- type Params
- type PoolConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var FxGorm = fx.Module( "fxgorm", fx.Provide(NewGormConfig), fx.Provide(NewGormDB), fx.Provide(NewDatabaseManagerWithConfig), )
FxGorm provides the GORM module for dependency injection
Functions ¶
Types ¶
type DatabaseConfig ¶
type DatabaseConfig struct {
Type DatabaseType `mapstructure:"type"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
SSLMode string `mapstructure:"sslmode"`
Charset string `mapstructure:"charset"`
ParseTime bool `mapstructure:"parse_time"`
Loc string `mapstructure:"loc"`
File string `mapstructure:"file"` // For SQLite
}
DatabaseConfig holds database-specific configuration
type DatabaseManager ¶
type DatabaseManager struct {
// contains filtered or unexported fields
}
DatabaseManager handles database operations
func NewDatabaseManager ¶
func NewDatabaseManager(config *GormConfig) *DatabaseManager
NewDatabaseManager creates a new database manager
func NewDatabaseManagerWithConfig ¶
func NewDatabaseManagerWithConfig(config *GormConfig) *DatabaseManager
NewDatabaseManagerWithConfig creates a new database manager with the given configuration
func (*DatabaseManager) Close ¶
func (dm *DatabaseManager) Close() error
Close closes the database connection
func (*DatabaseManager) Connect ¶
func (dm *DatabaseManager) Connect() error
Connect establishes a database connection with validation
func (*DatabaseManager) GetDB ¶
func (dm *DatabaseManager) GetDB() *gorm.DB
GetDB returns the database instance
func (*DatabaseManager) GetPoolConfig ¶
func (dm *DatabaseManager) GetPoolConfig() PoolConfig
GetPoolConfig returns the current pool configuration
func (*DatabaseManager) GetPoolStats ¶
func (dm *DatabaseManager) GetPoolStats() (*sql.DBStats, error)
GetPoolStats returns current connection pool statistics
func (*DatabaseManager) SetPoolConfig ¶
func (dm *DatabaseManager) SetPoolConfig(config PoolConfig) error
SetPoolConfig updates the connection pool configuration
type DatabaseType ¶
type DatabaseType string
DatabaseType represents supported database types
const ( PostgreSQL DatabaseType = "postgres" MySQL DatabaseType = "mysql" SQLite DatabaseType = "sqlite" SQLServer DatabaseType = "sqlserver" )
type GormConfig ¶
type GormConfig struct {
Database DatabaseConfig `mapstructure:"database"`
Pool PoolConfig `mapstructure:"pool"`
Log LogConfig `mapstructure:"log"`
Debug bool `mapstructure:"debug"`
}
GormConfig holds the complete GORM configuration
func NewGormConfig ¶
func NewGormConfig(config *fxconfig.Config) *GormConfig
NewGormConfig creates a new GORM configuration from the main config
Example ¶
ExampleGormConfig demonstrates how to create a GORM configuration
// This would typically come from your main config
config := &fxconfig.Config{}
// Create GORM configuration
gormConfig := NewGormConfig(config)
_ = gormConfig
func (*GormConfig) SetDefaults ¶
func (gc *GormConfig) SetDefaults()
SetDefaults sets default values for unconfigured settings
func (*GormConfig) Validate ¶
func (gc *GormConfig) Validate() error
Validate validates the configuration
type LogConfig ¶
type LogConfig struct {
Level logger.LogLevel `mapstructure:"level"`
SlowThreshold time.Duration `mapstructure:"slow_threshold"`
Colorful bool `mapstructure:"colorful"`
IgnoreRecordNotFoundError bool `mapstructure:"ignore_record_not_found_error"`
}
LogConfig holds logging configuration
type PoolConfig ¶
type PoolConfig struct {
MaxIdleConns int `mapstructure:"max_idle_conns"`
MaxOpenConns int `mapstructure:"max_open_conns"`
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
ConnMaxIdleTime time.Duration `mapstructure:"conn_max_idle_time"`
}
PoolConfig holds connection pool configuration