Documentation
¶
Index ¶
- func New(opts *Options) (*gorm.DB, error)
- func NewPostgreSQL(opts *PostgreSQLOptions) (*gorm.DB, error)
- func NewRedis(opts *RedisOptions) (*redis.Client, error)
- type GormTracePlugin
- type MySQLConfig
- type Options
- type PostgreSQLConfig
- type PostgreSQLOptions
- type RedisConfig
- func (c *RedisConfig) Addr() string
- func (c *RedisConfig) DialTimeoutDuration() time.Duration
- func (c *RedisConfig) IdleTimeoutDuration() time.Duration
- func (c *RedisConfig) ReadTimeoutDuration() time.Duration
- func (c *RedisConfig) ToOptions() (*RedisOptions, error)
- func (c *RedisConfig) Validate() error
- func (c *RedisConfig) WriteTimeoutDuration() time.Duration
- type RedisOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewPostgreSQL ¶
func NewPostgreSQL(opts *PostgreSQLOptions) (*gorm.DB, error)
NewPostgreSQL 根据给定的选项创建一个新的 GORM PostgreSQL 数据库实例
Types ¶
type GormTracePlugin ¶
type GormTracePlugin struct {
// contains filtered or unexported fields
}
GormTracePlugin 定义了一个 GORM 插件,用于追踪 SQL 查询的执行时间(支持 OpenTelemetry)
func NewGormTracePlugin ¶
func NewGormTracePlugin(enableTrace bool) *GormTracePlugin
NewGormTracePlugin 创建新的 GORM 追踪插件
func (*GormTracePlugin) Initialize ¶
func (op *GormTracePlugin) Initialize(db *gorm.DB) (err error)
Initialize 初始化追踪插件,注册 GORM 回调
type MySQLConfig ¶
type MySQLConfig struct {
Enabled bool `yaml:"enabled" env:"MYSQL_ENABLED" default:"true"`
Host string `yaml:"host" env:"MYSQL_HOST" default:"localhost"`
Port int `yaml:"port" env:"MYSQL_PORT" default:"3306"`
Database string `yaml:"database" env:"MYSQL_DATABASE" required:"true"`
Username string `yaml:"username" env:"MYSQL_USERNAME" required:"true"`
Password string `yaml:"password" env:"MYSQL_PASSWORD" required:"true"`
MaxConnections int `yaml:"max_connections" env:"MYSQL_MAX_CONNECTIONS" default:"100"`
Timeout pkgConfig.Duration `yaml:"timeout" env:"MYSQL_TIMEOUT" default:"30s"`
Charset string `yaml:"charset" env:"MYSQL_CHARSET" default:"utf8mb4"`
ParseTime bool `yaml:"parse_time" env:"MYSQL_PARSE_TIME" default:"true"`
Loc string `yaml:"loc" env:"MYSQL_LOC" default:"Local"`
EnableTrace bool `yaml:"enable_trace" env:"MYSQL_ENABLE_TRACE" default:"true"`
}
MySQLConfig MySQL 数据库配置结构体(用于从配置文件创建)
func (*MySQLConfig) ToOptions ¶
func (c *MySQLConfig) ToOptions() (*Options, error)
ToOptions 转换为 Options
type Options ¶
type Options struct {
Host string
Username string
Password string
Database string
MaxIdleConnections int
MaxOpenConnections int
MaxConnectionLifeTime time.Duration
LogLevel logger.LogLevel // 使用 GORM 自带的 LogLevel 类型
Logger logger.Interface
EnableTrace bool // 是否启用 SQL 追踪插件,用于记录 SQL 执行时间
}
Options 结构体定义了 GORM MySQL 连接器的配置选项(内部使用)
type PostgreSQLConfig ¶
type PostgreSQLConfig struct {
Enabled bool `yaml:"enabled" env:"POSTGRESQL_ENABLED" default:"true"`
Host string `yaml:"host" env:"POSTGRESQL_HOST" default:"localhost"`
Port int `yaml:"port" env:"POSTGRESQL_PORT" default:"5432"`
Database string `yaml:"database" env:"POSTGRESQL_DATABASE" required:"true"`
Username string `yaml:"username" env:"POSTGRESQL_USERNAME" required:"true"`
Password string `yaml:"password" env:"POSTGRESQL_PASSWORD" required:"true"`
SSLMode string `yaml:"ssl_mode" env:"POSTGRESQL_SSL_MODE" default:"disable"`
MaxConnections int `yaml:"max_connections" env:"POSTGRESQL_MAX_CONNECTIONS" default:"100"`
Timeout pkgConfig.Duration `yaml:"timeout" env:"POSTGRESQL_TIMEOUT" default:"30s"`
EnableTrace bool `yaml:"enable_trace" env:"POSTGRESQL_ENABLE_TRACE" default:"true"`
}
PostgreSQLConfig PostgreSQL 配置结构体(用于从配置文件创建)
func (*PostgreSQLConfig) TimeoutDuration ¶
func (c *PostgreSQLConfig) TimeoutDuration() time.Duration
TimeoutDuration 返回 time.Duration 类型的 Timeout
func (*PostgreSQLConfig) ToOptions ¶
func (c *PostgreSQLConfig) ToOptions() (*PostgreSQLOptions, error)
ToOptions 转换为 PostgreSQLOptions
func (*PostgreSQLConfig) Validate ¶
func (c *PostgreSQLConfig) Validate() error
Validate 验证 PostgreSQL 配置
type PostgreSQLOptions ¶
type PostgreSQLOptions struct {
Host string
Port int
Username string
Password string
Database string
SSLMode string
MaxIdleConnections int
MaxOpenConnections int
MaxConnectionLifeTime time.Duration
LogLevel logger.LogLevel
Logger logger.Interface
EnableTrace bool // 是否启用 SQL 追踪插件,用于记录 SQL 执行时间
}
PostgreSQLOptions 结构体定义了 GORM PostgreSQL 连接器的配置选项(内部使用)
type RedisConfig ¶
type RedisConfig struct {
Enabled bool `yaml:"enabled" env:"REDIS_ENABLED" default:"true"`
Host string `yaml:"host" env:"REDIS_HOST" default:"localhost"`
Port int `yaml:"port" env:"REDIS_PORT" default:"6379"`
Password string `yaml:"password" env:"REDIS_PASSWORD"`
DB int `yaml:"db" env:"REDIS_DB" default:"0"`
PoolSize int `yaml:"pool_size" env:"REDIS_POOL_SIZE" default:"20"`
MinIdleConns int `yaml:"min_idle_conns" env:"REDIS_MIN_IDLE_CONNS" default:"5"`
DialTimeout pkgConfig.Duration `yaml:"dial_timeout" env:"REDIS_DIAL_TIMEOUT" default:"5s"`
ReadTimeout pkgConfig.Duration `yaml:"read_timeout" env:"REDIS_READ_TIMEOUT" default:"3s"`
WriteTimeout pkgConfig.Duration `yaml:"write_timeout" env:"REDIS_WRITE_TIMEOUT" default:"3s"`
IdleTimeout pkgConfig.Duration `yaml:"idle_timeout" env:"REDIS_IDLE_TIMEOUT" default:"5m"`
EnableTrace bool `yaml:"enable_trace" env:"REDIS_ENABLE_TRACE" default:"true"`
}
RedisConfig Redis 配置结构体(用于从配置文件创建)
func (*RedisConfig) DialTimeoutDuration ¶
func (c *RedisConfig) DialTimeoutDuration() time.Duration
DialTimeoutDuration 返回 time.Duration 类型的 DialTimeout
func (*RedisConfig) IdleTimeoutDuration ¶
func (c *RedisConfig) IdleTimeoutDuration() time.Duration
IdleTimeoutDuration 返回 time.Duration 类型的 IdleTimeout
func (*RedisConfig) ReadTimeoutDuration ¶
func (c *RedisConfig) ReadTimeoutDuration() time.Duration
ReadTimeoutDuration 返回 time.Duration 类型的 ReadTimeout
func (*RedisConfig) ToOptions ¶
func (c *RedisConfig) ToOptions() (*RedisOptions, error)
ToOptions 转换为 RedisOptions
func (*RedisConfig) WriteTimeoutDuration ¶
func (c *RedisConfig) WriteTimeoutDuration() time.Duration
WriteTimeoutDuration 返回 time.Duration 类型的 WriteTimeout
type RedisOptions ¶
type RedisOptions struct {
Addr string
Password string
DB int
PoolSize int
MinIdleConns int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
EnableTrace bool // 是否启用命令追踪,用于记录 Redis 命令执行时间
}
RedisOptions 结构体定义了 Redis 连接器的配置选项(内部使用)