Documentation
¶
Index ¶
- Variables
- func Close()
- func CreateConnection(config DatabaseConfig) (*gorm.DB, error)
- func GetDatabase(name string) *gorm.DB
- func InitDb(connections []map[string]any)
- func Mysql(dsn string) gorm.Dialector
- func Postgres(dsn string) gorm.Dialector
- func Sqlserver(dsn string) gorm.Dialector
- type DatabaseConfig
- type Logger
- func (l Logger) Error(ctx context.Context, str string, args ...interface{})
- func (l Logger) Info(ctx context.Context, str string, args ...interface{})
- func (l Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
- func (l Logger) SetAsDefault()
- func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l Logger) Warn(ctx context.Context, str string, args ...interface{})
Constants ¶
This section is empty.
Variables ¶
var (
Master = make(map[string]*gorm.DB)
)
Global Master map 存储所有数据库连接对象(全局唯一)
Functions ¶
func Close ¶
func Close()
Close 关闭所有数据库连接(除了名称为 "default" 的连接) Close closes all database connections except for the one named "default".
func CreateConnection ¶
func CreateConnection(config DatabaseConfig) (*gorm.DB, error)
CreateConnection 根据配置创建单个数据库连接 CreateConnection creates a single database connection based on the provided configuration.
func GetDatabase ¶
GetDatabase 根据名称获取数据库连接对象 GetDatabase retrieves a database connection by its name.
func InitDb ¶
InitDb 初始化数据库连接 InitDb initializes database connections based on the provided configuration. connections is a slice of maps representing the configuration for each database.
Types ¶
type DatabaseConfig ¶
type DatabaseConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Hostname string `json:"hostname"`
Port string `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
DatabaseName string `json:"database"`
Params string `json:"params"`
LogEnabled bool `json:"log"`
DSN string `json:"dsn"`
MaxIdleConns int `json:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns"`
ConnMaxLifetime int `json:"conn_max_lifetime"`
ConnMaxIdleTime int `json:"conn_max_idle_time"` // 注意:修改为统一格式
LogLevel gormlogger.LogLevel `json:"level"`
}
DatabaseConfig 数据库配置结构体 DatabaseConfig represents the configuration for a database connection.
func (*DatabaseConfig) Distributed ¶
func (d *DatabaseConfig) Distributed(config dbresolver.Config, datas ...interface{}) *dbresolver.DBResolver
Distributed 设置分布式数据库,返回 dbresolver 插件实例 Distributed configures a distributed database using GORM's dbresolver plugin. 'config' specifies the resolver configuration, and 'datas' represents the data sources.
type Logger ¶
type Logger struct {
ZapLogger *zap.Logger
LogLevel gormlogger.LogLevel
SlowThreshold time.Duration
SkipCallerLookup bool
IgnoreRecordNotFoundError bool
}
func (Logger) LogMode ¶
func (l Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
func (Logger) SetAsDefault ¶
func (l Logger) SetAsDefault()