db

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

framework-db

Go framework package for db.

Installation

go get github.com/go-anyway/framework-db@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts *Options) (*gorm.DB, error)

New 根据给定的选项创建一个新的 GORM 数据库实例.

func NewPostgreSQL

func NewPostgreSQL(opts *PostgreSQLOptions) (*gorm.DB, error)

NewPostgreSQL 根据给定的选项创建一个新的 GORM PostgreSQL 数据库实例

func NewRedis

func NewRedis(opts *RedisOptions) (*redis.Client, error)

NewRedis 根据给定的选项创建一个新的 Redis 客户端实例

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 回调

func (*GormTracePlugin) Name

func (op *GormTracePlugin) Name() string

Name 返回追踪插件的名称

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) DSN

func (c *MySQLConfig) DSN() string

DSN 返回 MySQL 数据源名称

func (*MySQLConfig) ToOptions

func (c *MySQLConfig) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*MySQLConfig) Validate

func (c *MySQLConfig) Validate() error

Validate 验证 MySQL 配置

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) Addr

func (c *RedisConfig) Addr() string

Addr 返回 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) Validate

func (c *RedisConfig) Validate() error

Validate 验证 Redis 配置

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 连接器的配置选项(内部使用)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址