database

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DBDriverNotSupport = errors.New("database driver not support")
)

Functions

func Pagination

func Pagination[T any](pager *Pager[T]) func(db *gorm.DB) *gorm.DB

Types

type BaseMapper

type BaseMapper[T any] struct {
	DB *Database
}

func NewMapper

func NewMapper[T any](db *Database, model T) *BaseMapper[T]

func (*BaseMapper[T]) Count

func (m *BaseMapper[T]) Count(entity T) (count int64, err error)

func (*BaseMapper[T]) Delete

func (m *BaseMapper[T]) Delete(entity T, db ...*gorm.DB) error

func (*BaseMapper[T]) Detail

func (m *BaseMapper[T]) Detail(entity T) (res T, err error)

func (*BaseMapper[T]) GetDB

func (m *BaseMapper[T]) GetDB() *gorm.DB

func (*BaseMapper[T]) Insert

func (m *BaseMapper[T]) Insert(entity T, db ...*gorm.DB) error

func (*BaseMapper[T]) List

func (m *BaseMapper[T]) List(entity T, order ...string) (res []T, err error)

func (*BaseMapper[T]) Page

func (m *BaseMapper[T]) Page(entity T, page, size int64) (res *Pager[T], err error)

func (*BaseMapper[T]) Update

func (m *BaseMapper[T]) Update(entity T, fields map[string]interface{}, db ...*gorm.DB) error

type BaseModel

type BaseModel struct {
	CreatedAt time.Time      `json:"createdAt" swaggerignore:"true"`
	UpdatedAt time.Time      `json:"updatedAt" swaggerignore:"true"`
	DeleteAt  gorm.DeletedAt `json:"-" swaggerignore:"true"`
}

type CachedMapper

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

func NewCachedMapper

func NewCachedMapper[T any](db *Database, model T, cache cache.Cache) *CachedMapper[T]

func (*CachedMapper[T]) Delete

func (m *CachedMapper[T]) Delete(entity T, db ...*gorm.DB) error

func (*CachedMapper[T]) Detail

func (m *CachedMapper[T]) Detail(entity T) (res T, err error)

func (*CachedMapper[T]) Insert

func (m *CachedMapper[T]) Insert(entity T, db ...*gorm.DB) error

func (*CachedMapper[T]) Update

func (m *CachedMapper[T]) Update(entity T, fields map[string]interface{}, db ...*gorm.DB) error

type Database

type Database struct {
	*gorm.DB
}

func GetDB

func GetDB() *Database

func NewDatabase

func NewDatabase(cfg *config.Config) *Database

type Mapper

type Mapper[T any] interface {
	Insert(T, ...*gorm.DB) error
	Delete(T, ...*gorm.DB) error
	Update(T, map[string]interface{}, ...*gorm.DB) error
	Detail(T) (T, error)
	List(T, ...string) ([]T, error)
	Count(T) (int64, error)
	Page(T, int64, int64) (*Pager[T], error)

	GetDB() *gorm.DB
}

type Pager

type Pager[T any] struct {
	CurrentPage int64 `json:"current"`
	PageSize    int64 `json:"size"`
	Total       int64 `json:"total"`
	Data        []T   `json:"data"`
}

Jump to

Keyboard shortcuts

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