db

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const ComponentName = "database"

Variables

This section is empty.

Functions

func DialectorMap added in v0.1.3

func DialectorMap(drivers []DriverOpenFunc, cfgMap map[string]Config) map[string]DialectorConfig

func Logger

func Logger(conf Config) logger.Interface

func NewDB

func NewDB(dialector gorm.Dialector, conf Config) (*gorm.DB, error)

NewDB 新建数据库连接 内部已自动 ping

Types

type Config

type Config struct {
	Driver        string         `mapstructure:"driver" yaml:"driver" validate:"required"`    // 驱动名称
	Source        string         `mapstructure:"source" mask:":([^@]+)@" validate:"required"` // root:****@tcp(127.0.0.1:3306)/test
	DryRun        bool           `mapstructure:"dryRun" yaml:"dryRun"`                        // 是否为测试模式(空跑sql,不会实际操作数据库)
	SlowThreshold types.Duration `mapstructure:"slowThreshold" yaml:"slowThreshold"`          // 慢日志阈值
	MaxOpenConn   int            `mapstructure:"maxOpenConn" yaml:"maxOpenConn"`              // 最大连接数 (高并发 500,低并发 100)
	MaxIdleConn   int            `mapstructure:"maxIdleConn" yaml:"maxIdleConn"`              // 最大空闲连接数 (高并发 50,低并发 10)
	MaxLifeTime   types.Duration `mapstructure:"maxLifeTime" yaml:"maxLifeTime"`              // 最大连接时间 (高并发 1h,低并发 30m)
	MaxIdleTime   types.Duration `mapstructure:"maxIdleTime" yaml:"maxIdleTime"`              // 最大空闲时间 (高并发 15m,低并发 10m)
}

type DBManager

type DBManager map[string]*gorm.DB

多数据源管理

func NewDBManager

func NewDBManager(cfgKV map[string]DialectorConfig) (DBManager, error)

func (DBManager) Default

func (m DBManager) Default() *gorm.DB

func (DBManager) Get

func (m DBManager) Get(k string) *gorm.DB

type DialectorConfig added in v0.1.3

type DialectorConfig struct {
	Dialector gorm.Dialector
	Config    Config
}

type DriverOpenFunc added in v0.1.3

type DriverOpenFunc func(dsn string) gorm.Dialector

DriverOpenFunc 驱动打开函数 如: mysql.Open

Jump to

Keyboard shortcuts

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