mdb

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
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.

func SetConfig

func SetConfig(name string, cfg *Config)

SetConfig sets the db configuration with the specified name.

func SetConfigByMap

func SetConfigByMap(m map[string]any, name ...string) error

SetConfigByMap sets 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

func ConfigFromMap(m map[string]any) (config *Config, err error)

ConfigFromMap parses and returns config from given map.

func GetConfig

func GetConfig(name ...string) (config *Config, ok bool)

GetConfig returns the db configuration with the specified name. If `name` is not passed, it returns configuration of the default name.

func (*Config) AddPlugin

func (c *Config) AddPlugin(plugins ...gorm.Plugin)

func (*Config) AddReplica

func (c *Config) AddReplica(replica Config)

func (*Config) SetConfigWithMap

func (c *Config) SetConfigWithMap(config map[string]any) error

func (*Config) SetLogger

func (c *Config) SetLogger(logger *mlog.Logger)

func (*Config) SetPlugins

func (c *Config) SetPlugins(plugins []gorm.Plugin)

func (*Config) SetReplicas

func (c *Config) SetReplicas(replicas []Config)

type DB

type DB struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func Instance

func Instance(name ...string) *DB

Instance returns a db instance.

func New

func New(config ...*Config) (*DB, error)

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping checks if the database is reachable.

func (*DB) Transact

func (db *DB) Transact(ctx context.Context, fn func(tx *DB) error) error

Transact starts a transaction with the given context.

func (*DB) TransactWithOptions

func (db *DB) TransactWithOptions(ctx context.Context, opts *sql.TxOptions, fn func(tx *DB) error) error

TransactWithOptions starts a transaction with the given context and options.

func (*DB) WithContext

func (db *DB) WithContext(ctx context.Context) *DB

WithContext returns a new DB with the given context.

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.

func (*GormLogger) Trace

func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Trace logs a SQL query.

func (*GormLogger) Warn

func (l *GormLogger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a warning message.

type Option

type Option func(*GormLogger)

Option is a functional option for configuring the GormLogger.

func WithLogLevel

func WithLogLevel(level logger.LogLevel) Option

WithLogLevel sets the GORM log level.

func WithSkipErrRecordNotFound

func WithSkipErrRecordNotFound(skip bool) Option

WithSkipErrRecordNotFound sets whether to skip ErrRecordNotFound errors.

func WithSlowThreshold

func WithSlowThreshold(threshold time.Duration) Option

WithSlowThreshold sets the slow query threshold.

Jump to

Keyboard shortcuts

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