Documentation ¶
Index ¶
- Variables
- func FromContext(ctx context.Context, defaultDB *gorm.DB) *gorm.DB
- func MigrateMysqlDB(dsn, dir string, isUp bool) error
- func Open(conf *Config) (*gorm.DB, error)
- func RunInTx(ctx context.Context, db *gorm.DB, opts *sql.TxOptions, ...) error
- func WithContext(ctx context.Context, db *gorm.DB) context.Context
- func WrapError(err error) error
- type CloseFunc
- type Config
- type Logger
- func (l *Logger) Error(ctx context.Context, s string, i ...interface{})
- func (l *Logger) Info(ctx context.Context, s string, i ...interface{})
- func (l *Logger) LogMode(level glogger.LogLevel) glogger.Interface
- func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *Logger) Warn(ctx context.Context, s string, i ...interface{})
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 ¶
FromContext returns the *gorm.DB stored in the context if exists, otherwise returns defaultDB.
func MigrateMysqlDB ¶
MigrateMysqlDB migrates database from the given dsn data source name and migration directories.
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 ¶
WithContext creates a new context with the provided db attached.
Types ¶
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.
Click to show internal directories.
Click to hide internal directories.