database

package
v0.0.0-...-fc07508 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedDriver = errors.New("unsupported database driver")
	ErrEmptyDir          = errors.New("empty directory")
)
View Source
var (
	// ErrRecordNotFound is an error if not exists record.
	ErrRecordNotFound = errors.New("record not found")
	// ErrKeyConflict is an error if duplicate field.
	ErrKeyConflict = errors.New("conflict key")
	// ErrFKConstraint is an error if foreign key constraint failed.
	ErrFKConstraint = errors.New("a foreign key constraint fails")
)
View Source
var DefaulTxOptions = &sql.TxOptions{
	Isolation: sql.LevelDefault,
	ReadOnly:  false,
}

Functions

func FromContext

func FromContext(ctx context.Context, defaultDB *gorm.DB) *gorm.DB

FromContext returns the *gorm.DB stored in the context if exists, otherwise returns defaultDB.

func MigrateMysqlDB

func MigrateMysqlDB(dsn, dir string, isUp bool) error

MigrateMysqlDB migrates database from the given dsn data source name and migration directories.

func Open

func Open(conf *Config) (*gorm.DB, error)

Open returns a new gorm.DB for given conf Config.

func RunInTx

func RunInTx(ctx context.Context, db *gorm.DB, opts *sql.TxOptions, f func(txdb *gorm.DB) error) error

RunInTx begin transaction from given database and execute f.

func WithContext

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

WithContext creates a new context with the provided db attached.

func WrapError

func WrapError(err error) error

WrapError wraps given error to database error. ErrRecordNotFound is returned if err is a gorm.ErrRecordNotFound If conflict key error, then ErrKeyConflict will be returned.

Types

type CloseFunc

type CloseFunc func() error

func NewTestMysqlDB

func NewTestMysqlDB(tb testing.TB, tag string) (string, *gorm.DB, CloseFunc)

NewTestMysqlDB starts mysql docker container with given version tag and returns dsn, gorm.DB, CloseFunc to clean up. If provide empty tag, will use "5.7".

type Config

type Config struct {
	Driver         string `json:"driver" yaml:"driver"`
	DataSourceName string `json:"data-source-name" yaml:"data-source-name"`
	LoggingLevel   int    `json:"logging-level" yaml:"logging-level"`
	LoggingPrefix  string `json:"logging-prefix" yaml:"logging-prefix"`
	BatchSize      int    `json:"batch-size" yaml:"batch-size"`
	Migrate        struct {
		Enabled bool   `json:"enabled" yaml:"enabled"`
		Dir     string `json:"dir" yaml:"dir"`
	} `json:"migrate" yaml:"migrate"`
	Pool struct {
		MaxOpen     int           `json:"max-open" yaml:"max-open"`
		MaxIdle     int           `json:"max-idle" yaml:"max-idle"`
		MaxLifeTime time.Duration `json:"max-lifetime" yaml:"max-lifetime"`
	} `json:"pool" yaml:"pool"`
	Replica struct {
		DataSourceNames []string `json:"data-source-names" yaml:"data-source-names"`
		Pool            struct {
			MaxOpen     int           `json:"max-open" yaml:"max-open"`
			MaxIdle     int           `json:"max-idle" yaml:"max-idle"`
			MaxLifeTime time.Duration `json:"max-lifetime" yaml:"max-lifetime"`
		} `json:"pool" yaml:"pool"`
	} `json:"replica" yaml:"replica"`
}

Config represents database configs.

type Logger

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

func NewLogger

func NewLogger(slowThreshold time.Duration,
	ignoreRecordNotFoundError bool,
	level zapcore.Level,
	prefix string,
) *Logger

NewLogger returns a new logger for gorm. *zap.SugaredLogger will use from context.Context.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, s string, i ...interface{})

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, s string, i ...interface{})

func (*Logger) LogMode

func (l *Logger) LogMode(level glogger.LogLevel) glogger.Interface

func (*Logger) Trace

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

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, s string, i ...interface{})

Jump to

Keyboard shortcuts

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