database

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxIdelConns = 10
	MaxOpenConns = 100
	MaxLifeTime  = time.Hour
	MaxIdleTime  = time.Hour
)
View Source
var (
	DefaultPageSize = 15
	PageKey         = "page"
	PageSizeKey     = "per_page"
	PageParamsType  = reflection.InterfaceType[PageParams]()
)
View Source
var DestSliceType = reflect.TypeOf([]interface{}{})
View Source
var Migrator func(tx *gorm.DB)
View Source
var ModelType = reflect.TypeOf(Model{})

Functions

func RegisterDriver added in v0.0.5

func RegisterDriver(name string, dr DriverRegister)

RegisterDriver register db driver

func RegisterFromConfig added in v0.1.19

func RegisterFromConfig(name string, config ConnectionConfig, defaultLogger *zap.Logger)

func WithDefaults added in v0.0.7

func WithDefaults()

Types

type Builder added in v0.1.0

type Builder[T interface{}] struct {
	*gorm.DB
	Paginator Paginator[T]
}

func NewBuilder added in v0.1.0

func NewBuilder[T interface{}](tx *gorm.DB, paginator Paginator[T]) *Builder[T]

func (*Builder[T]) First added in v0.1.0

func (b *Builder[T]) First(conds ...interface{}) (result T, err error)

func (*Builder[T]) Get added in v0.1.0

func (b *Builder[T]) Get() (result []T, err error)

func (*Builder[T]) Paginate added in v0.1.0

func (b *Builder[T]) Paginate(opts ...PaginateOptions) (*PageResult[T], error)

func (*Builder[T]) Query added in v0.1.0

func (b *Builder[T]) Query(fn func(*gorm.DB) *gorm.DB) *Builder[T]

type Config added in v0.0.5

type Config struct {
	Default     string                       `yaml:"default" env:"DB_CONNECTION"`
	Connections map[string]*ConnectionConfig `yaml:"connections"`
	//ImplicitInjection: whether injection model to request handler implicit
	//  usage:
	//      w.Get("/users/:user", func(user models.User) models.User {
	//			return user
	//      })
	//
	ImplicitInjection bool   `yaml:"implicit_injection"`
	AuthMigrate       bool   `yaml:"migrate" env:"DB_MIGRATE"`
	LogChannel        string `yaml:"log_channel"`
	WithForeignKey    bool   `yaml:"with_foreign_key" default:"false"`
	WithMigrationLog  bool   `yaml:"with_migration_log" default:"false"`
}

type ConnectionConfig added in v0.0.5

type ConnectionConfig struct {
	Driver     string `yaml:"driver" default:"mysql"`
	DSN        string `yaml:"dsn"`
	LogChannel string `yaml:"log_channel"`
	LogLevel   string `yaml:"log_level" default:"info"`
}

type DriverRegister added in v0.0.5

type DriverRegister func(dsn string) gorm.Dialector

func GetDriverRegister added in v0.0.5

func GetDriverRegister(name string) (DriverRegister, bool)

GetDriverRegister: get registerd driver

type Injector added in v0.0.2

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

func (Injector) Injection added in v0.0.2

func (ij Injector) Injection(abs interface{}, last reflect.Value) (reflect.Value, error)

func (Injector) When added in v0.0.2

func (ij Injector) When(abs interface{}) bool

type Logger added in v0.1.0

type Logger struct {
	SlowThreshold time.Duration
	// contains filtered or unexported fields
}

func NewLogger added in v0.1.0

func NewLogger(logger *zap.Logger, level logger.LogLevel) *Logger

func (*Logger) Error added in v0.1.0

func (l *Logger) Error(_ context.Context, msg string, data ...interface{})

func (*Logger) Info added in v0.1.0

func (l *Logger) Info(_ context.Context, msg string, data ...interface{})

func (*Logger) LogMode added in v0.1.0

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

func (*Logger) Trace added in v0.1.0

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

func (*Logger) Warn added in v0.1.0

func (l *Logger) Warn(_ context.Context, msg string, data ...interface{})

type Model added in v0.0.7

type Model struct {
	*gorm.DB  `gorm:"-" json:"-" msgpack:"-"`
	Paginator *gormdb.Paginator `gorm:"-" json:"-"  msgpack:"-"`
	Dest      interface{}       `gorm:"-" json:"-"  msgpack:"-"`
	DestSlice interface{}       `gorm:"-" json:"-"  msgpack:"-"`
}

func (Model) Paginate added in v0.0.7

func (m Model) Paginate() (map[string]interface{}, error)

type PageMeta added in v0.1.0

type PageMeta struct {
	Total    int64 `json:"total"`
	PerPage  int   `json:"per_page"`
	Page     int   `json:"page"`
	LastPage int   `json:"last_page"`
	From     int   `json:"from"`
	To       int   `json:"to"`
}

type PageParams added in v0.1.0

type PageParams interface {
	Page() int
	PerPage() int
}

type PageResult added in v0.1.0

type PageResult[T interface{}] struct {
	Meta  PageMeta               `json:"meta"`
	Data  []T                    `json:"data"`
	Extra map[string]interface{} `json:"extra,omitempty"`
}

func (*PageResult[T]) With added in v0.1.2

func (pr *PageResult[T]) With(key string, data interface{}) *PageResult[T]

func (*PageResult[T]) WithOut added in v0.1.2

func (pr *PageResult[T]) WithOut(key string) *PageResult[T]

type PaginateOptions added in v0.1.10

type PaginateOptions struct {
	AggTableScope func(tx *gorm.DB) *gorm.DB
}

type Paginator added in v0.1.0

type Paginator[T interface{}] struct {
	Params PageParams
}

func NewPaginator added in v0.1.0

func NewPaginator[T interface{}](params PageParams) Paginator[T]

func (Paginator[T]) Paginate added in v0.1.0

func (p Paginator[T]) Paginate(tx *gorm.DB, opts ...PaginateOptions) (*PageResult[T], error)

type RequestPageParams added in v0.1.0

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

func (RequestPageParams) Page added in v0.1.0

func (r RequestPageParams) Page() int

func (RequestPageParams) PerPage added in v0.1.0

func (r RequestPageParams) PerPage() int

type Service added in v0.0.5

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

Service of database

func NewService added in v0.0.7

func NewService() *Service

func (*Service) Lifetime added in v0.0.5

func (s *Service) Lifetime(ioc container.Interface, request contracts.RequestContract)

Lifetime container callback usually register request lifetime instance to IoC-Container (per-request unique) this function will run before every request

func (*Service) Register added in v0.0.5

func (s *Service) Register(app *framework.App) error

Register service when app starting, before http server start you can configure service, initialize global vars etc. running at main goroutine

Jump to

Keyboard shortcuts

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