Documentation
¶
Index ¶
- Constants
- func RemoveConfig(name ...string)
- func SetConfig(name string, cfg *Config)
- func SetConfigByMap(m map[string]any, name ...string) error
- type Config
- func (c *Config) AddPlugin(plugins ...gorm.Plugin)
- func (c *Config) AddReplica(replica Config)
- func (c *Config) SetConfigWithMap(config map[string]any) error
- func (c *Config) SetLogger(logger *mlog.Logger)
- func (c *Config) SetPlugins(plugins []gorm.Plugin)
- func (c *Config) SetReplicas(replicas []Config)
- type DB
- func (db *DB) GetLogger() *mlog.Logger
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) Transact(ctx context.Context, fn func(tx *DB) error) error
- func (db *DB) TransactWithOptions(ctx context.Context, opts *sql.TxOptions, fn func(tx *DB) error) error
- func (db *DB) WithContext(ctx context.Context) *DB
- type GormLogger
- func (l *GormLogger) Error(ctx context.Context, msg string, args ...any)
- func (l *GormLogger) Info(ctx context.Context, msg string, args ...any)
- func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface
- func (l *GormLogger) Trace(ctx context.Context, begin time.Time, ...)
- func (l *GormLogger) Warn(ctx context.Context, msg string, args ...any)
- type Option
Constants ¶
const (
// DefaultName is the default group name for db instance.
DefaultName = "default"
)
Variables ¶
This section is empty.
Functions ¶
func RemoveConfig ¶
func RemoveConfig(name ...string)
RemoveConfig removes the db configuration with the specified name.
Types ¶
type Config ¶
type Config struct {
// Type is the type of the database.
Type string `mconv:"type"`
// DSN is the data source name.
DSN string `mconv:"dsn"`
// Host is the host of the database.
Host string `mconv:"host"`
// Port is the port of the database.
Port string `mconv:"port"`
// User is the user of the database.
User string `mconv:"user"`
// Password is the password of the database.
Password string `mconv:"password"`
// DBName is the name of the database.
DBName string `mconv:"db_name"`
// MaxIdleTime is the maximum idle time for the database connection.
MaxIdleTime time.Duration `mconv:"max_idle_time"`
// MaxIdleConnection is the maximum idle connection for the database.
MaxIdleConnection int `mconv:"max_idle_connection"`
// MaxOpenConnection is the maximum open connection for the database.
MaxOpenConnection int `mconv:"max_open_connection"`
// MaxLifetime is the maximum lifetime for the database connection.
MaxLifetime time.Duration `mconv:"max_lifetime"`
// SlowThreshold is the slow query threshold.
SlowThreshold time.Duration `mconv:"slow_threshold"`
// Logger is the logger for the database.
Logger *mlog.Logger
// Replicas is the replicas list.
Replicas []Config
// Plugins is the plugins list.
Plugins []gorm.Plugin
}
func ConfigFromMap ¶
ConfigFromMap parses and returns config from given map.
func GetConfig ¶
GetConfig returns the db configuration with the specified name. If `name` is not passed, it returns configuration of the default name.
func (*Config) AddReplica ¶
func (*Config) SetPlugins ¶
func (*Config) SetReplicas ¶
type DB ¶
type GormLogger ¶
type GormLogger struct {
// contains filtered or unexported fields
}
GormLogger is a custom GORM logger that integrates with mlog.
func NewGormLogger ¶
func NewGormLogger(mlogger *mlog.Logger, opts ...Option) *GormLogger
NewGormLogger creates a new GormLogger.
func (*GormLogger) Error ¶
func (l *GormLogger) Error(ctx context.Context, msg string, args ...any)
Error logs an error message.
func (*GormLogger) Info ¶
func (l *GormLogger) Info(ctx context.Context, msg string, args ...any)
Info logs an info message.
func (*GormLogger) LogMode ¶
func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface
LogMode returns a new logger with a different log level.
type Option ¶
type Option func(*GormLogger)
Option is a functional option for configuring the GormLogger.
func WithLogLevel ¶
WithLogLevel sets the GORM log level.
func WithSkipErrRecordNotFound ¶
WithSkipErrRecordNotFound sets whether to skip ErrRecordNotFound errors.
func WithSlowThreshold ¶
WithSlowThreshold sets the slow query threshold.