gormutil

package
v0.0.0-...-098be99 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	LoggerStd    = "std"
	LoggerLogrus = "logrus"
)
View Source
const (
	MYSQL  = "mysql"
	SQLITE = "sqlite"
)

Variables

View Source
var ErrIterationEoF = errors.New("iteration eof")

Functions

func FromContext

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

func LogrusLogger

func LogrusLogger(config logger.Config) logger.Interface

func MustOpen

func MustOpen(c Config, options ...GORMOption) *gorm.DB

func Open

func Open(c Config, options ...GORMOption) (*gorm.DB, error)

func OpenMemory

func OpenMemory(options ...GORMOption) (*gorm.DB, error)

func RegisterDriver

func RegisterDriver(name string, driver Driver)

func StdLogger

func StdLogger(level ...logger.LogLevel) logger.Interface

func Tx

func Tx(ctx context.Context, f func(ctx context.Context) error) error

func WithContext

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

Types

type Config

type Config struct {
	Driver          string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxIdleTime Duration
	ConnMaxLifetime Duration
	Logger          LoggerConfig
	Option          Option
}

func MemoryOption

func MemoryOption() Config

type Driver

type Driver func(c Option) (gorm.Dialector, error)

type Duration

type Duration string

type GORMOption

type GORMOption func(o *gorm.Config)

func WithAllowGlobalUpdate

func WithAllowGlobalUpdate(AllowGlobalUpdate bool) GORMOption

func WithClauseBuilders

func WithClauseBuilders(ClauseBuilders map[string]clause.ClauseBuilder) GORMOption

func WithConnPool

func WithConnPool(ConnPool gorm.ConnPool) GORMOption

func WithCreateBatchSize

func WithCreateBatchSize(CreateBatchSize int) GORMOption

func WithDisableAutomaticPing

func WithDisableAutomaticPing(DisableAutomaticPing bool) GORMOption

func WithDisableForeignKeyConstraintWhenMigrating

func WithDisableForeignKeyConstraintWhenMigrating(DisableForeignKeyConstraintWhenMigrating bool) GORMOption

func WithDisableNestedTransaction

func WithDisableNestedTransaction(DisableNestedTransaction bool) GORMOption

func WithDryRun

func WithDryRun(DryRun bool) GORMOption

func WithFullSaveAssociations

func WithFullSaveAssociations(FullSaveAssociations bool) GORMOption

func WithIgnoreRelationshipsWhenMigrating

func WithIgnoreRelationshipsWhenMigrating(IgnoreRelationshipsWhenMigrating bool) GORMOption

func WithLogger

func WithLogger(Logger logger.Interface) GORMOption

func WithNamingStrategy

func WithNamingStrategy(NamingStrategy schema.Namer) GORMOption

func WithNowFunc

func WithNowFunc(NowFunc func() time.Time) GORMOption

func WithPlugins

func WithPlugins(Plugins map[string]gorm.Plugin) GORMOption

func WithPrepareStmt

func WithPrepareStmt(PrepareStmt bool) GORMOption

func WithQueryFields

func WithQueryFields(QueryFields bool) GORMOption

func WithSkipDefaultTransaction

func WithSkipDefaultTransaction(SkipDefaultTransaction bool) GORMOption

func WithStdLogger

func WithStdLogger(level ...logger.LogLevel) GORMOption

func WithTranslateError

func WithTranslateError(TranslateError bool) GORMOption

type LogLevel

type LogLevel string

func (LogLevel) Level

func (l LogLevel) Level() logger.LogLevel

type LoggerConfig

type LoggerConfig struct {
	Logger                    string   // std logrus
	SlowThreshold             string   //慢查询定义,格式:2s 1s 200ms
	Colorful                  bool     //
	IgnoreRecordNotFoundError bool     //忽略 NotFoundError
	ParameterizedQueries      bool     //隐藏查询参数
	LogLevel                  LogLevel //日志打印级别 1 Silent, 2 Error, 3 Warn, 4 Info
}

type Option

type Option = common.Config

type Repository

type Repository[T any] struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository[T any](db *gorm.DB) *Repository[T]

func (*Repository[T]) Count

func (r *Repository[T]) Count(ctx context.Context, where any) (int64, error)

func (*Repository[T]) Delete

func (r *Repository[T]) Delete(ctx context.Context, where any) (int64, error)

func (*Repository[T]) FindOne

func (r *Repository[T]) FindOne(ctx context.Context, where any) (dst *T, err error)

func (*Repository[T]) GetDB

func (r *Repository[T]) GetDB(ctx context.Context) *gorm.DB

func (*Repository[T]) Insert

func (r *Repository[T]) Insert(ctx context.Context, data any) (int64, error)

func (*Repository[T]) Iterate

func (r *Repository[T]) Iterate(ctx context.Context, column string, where any) (repository.Iterator[T], error)

func (*Repository[T]) List

func (r *Repository[T]) List(ctx context.Context, where any, offset, limit int, order ...any) ([]*T, int64, error)

func (*Repository[T]) SetDB

func (r *Repository[T]) SetDB(db *gorm.DB)

func (*Repository[T]) Update

func (r *Repository[T]) Update(ctx context.Context, where any, update any) (int64, error)

Jump to

Keyboard shortcuts

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