db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(ctx context.Context, db *gorm.DB, sql string, args ...any) error

func NewTxContext

func NewTxContext(ctx context.Context, tx *gorm.DB) context.Context

func Transaction

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

func TxFromContext

func TxFromContext(ctx context.Context) (*gorm.DB, bool)

Types

type MySQL

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

func NewMySQL

func NewMySQL(ctx context.Context, cfg MySQLConfig, opts ...Option) (*MySQL, error)

func (*MySQL) Close

func (m *MySQL) Close() error

func (*MySQL) Ping

func (m *MySQL) Ping(ctx context.Context) error

type MySQLConfig

type MySQLConfig struct {
	Driver          string `json:"driver" yaml:"driver"`
	DSN             string `json:"dsn" yaml:"dsn"`
	MaxOpenConns    int    `json:"max_open_conns" yaml:"max_open_conns"`
	MaxIdleConns    int    `json:"max_idle_conns" yaml:"max_idle_conns"`
	ConnMaxLifetime string `json:"conn_max_lifetime" yaml:"conn_max_lifetime"`
	SlowThreshold   string `json:"slow_threshold" yaml:"slow_threshold"`
	LogLevel        string `json:"log_level" yaml:"log_level"`
}

type Option

type Option func(*options)

func WithLogger

func WithLogger(l *slog.Logger) Option

func WithMetrics

func WithMetrics(m *metrics.Metrics) Option

type PageRequest

type PageRequest struct {
	Page     int
	PageSize int
	Sort     string
}

func NormalizePage

func NormalizePage(req PageRequest) PageRequest

type PageResult

type PageResult[T any] struct {
	Items    []T   `json:"items"`
	Total    int64 `json:"total"`
	Page     int   `json:"page"`
	PageSize int   `json:"page_size"`
}

func Paginate

func Paginate[T any](query *gorm.DB, req PageRequest) (PageResult[T], error)

type TxManager

type TxManager struct {
	// contains filtered or unexported fields
}

func NewTxManager

func NewTxManager(db *gorm.DB, logger *slog.Logger) *TxManager

func (*TxManager) DB

func (m *TxManager) DB(ctx context.Context) *gorm.DB

func (*TxManager) WithTx

func (m *TxManager) WithTx(ctx context.Context, fn func(ctx context.Context, tx *gorm.DB) error) error

Jump to

Keyboard shortcuts

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