dao

package
v0.0.0-...-4b55cb2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PrefixTable       = ""
	IsSelectForUpdate = true
)

Functions

func Check

func Check(ctx context.Context, db *gorm.DB) (bool, error)

Check 检查数据是否存在

func ExecTrans

func ExecTrans(ctx context.Context, db *gorm.DB, fn TransFunc) error

ExecTrans 执行事务

func ExecTransWithLock

func ExecTransWithLock(ctx context.Context, db *gorm.DB, fn TransFunc) error

ExecTransWithLock 执行事务(加锁)

func FindOne

func FindOne(ctx context.Context, db *gorm.DB, out interface{}) (bool, error)

FindOne 查询单条数据

func FindPage

func FindPage(ctx context.Context, db *gorm.DB, pp PaginationParam, out interface{}) (int, error)

FindPage 查询分页数据

func GetDB

func GetDB(ctx context.Context, defDB *gorm.DB) *gorm.DB

func GetDBWithModel

func GetDBWithModel(ctx context.Context, defDB *gorm.DB, m interface{}) *gorm.DB

func NewDB

func NewDB(c *Config) (*gorm.DB, error)

NewDB 创建DB实例

func NewMongoClient

func NewMongoClient(cfg *MongoConfig) (*mongo.Client, error)

NewClient 创建mongo客户端实例

func ParseOrder

func ParseOrder(items []*OrderField, handle ...OrderFieldFunc) string

ParseOrder 解析排序字段

func ToString

func ToString(v interface{}) string

Types

type Config

type Config struct {
	Debug        bool   `json:"debug"`
	DBType       string `json:"db_type"`
	DSN          string `json:"dsn"`
	MaxLifetime  int    `json:"max_lifetime"`
	MaxOpenConns int    `json:"max_open_conns"`
	MaxIdleConns int    `json:"max_idle_conns"`
}

Config 配置参数

type ListResult

type ListResult struct {
	List       interface{}       `json:"list"`
	Pagination *PaginationResult `json:"pagination,omitempty"`
}

ListResult 响应列表数据

type Model

type Model struct {
	ID        uint       `gorm:"column:id;primary_key;auto_increment;"`
	RecordID  string     `gorm:"column:record_id;size:36;index;default:'';not null;"`
	CreatedAt time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt *time.Time `gorm:"column:deleted_at;index;"`
}

Model base model

func (*Model) TableName

func (m *Model) TableName(name string) string

TableName table name

type MongoConfig

type MongoConfig struct {
	URI      string
	Database string
	Timeout  time.Duration
}

Config 配置参数

type OrderDirection

type OrderDirection int

OrderDirection 排序方向

const (
	// OrderByASC 升序排序
	OrderByASC OrderDirection = 1
	// OrderByDESC 降序排序
	OrderByDESC OrderDirection = 2
)

type OrderField

type OrderField struct {
	Key       string         // 字段名(字段名约束为小写蛇形)
	Direction OrderDirection // 排序方向
}

OrderField 排序字段

func NewOrderField

func NewOrderField(key string, d OrderDirection) *OrderField

NewOrderField 创建排序字段

func NewOrderFields

func NewOrderFields(keys []string, directions ...map[int]OrderDirection) []*OrderField

NewOrderFields 创建排序字段(默认升序排序),可指定不同key的排序规则 keys 需要排序的key directions 排序规则,按照key的索引指定,索引默认从0开始

type OrderFieldFunc

type OrderFieldFunc func(string) string

OrderFieldFunc 排序字段转换函数

type PaginationParam

type PaginationParam struct {
	Pagination bool `form:"-"`        // 是否使用分页查询
	OnlyCount  bool `form:"-"`        // 是否仅查询count
	Current    uint `form:"current"`  // 当前页
	PageSize   uint `form:"pageSize"` // 页大小
}

PaginationParam 分页查询条件

func (PaginationParam) GetCurrent

func (a PaginationParam) GetCurrent() uint

GetCurrent 获取当前页

func (PaginationParam) GetPageSize

func (a PaginationParam) GetPageSize() uint

GetPageSize 获取页大小

type PaginationResult

type PaginationResult struct {
	Total    int  `json:"total"`
	Current  uint `json:"current"`
	PageSize uint `json:"pageSize"`
}

PaginationResult 分页查询结果

func WrapPageQuery

func WrapPageQuery(ctx context.Context, db *gorm.DB, pp PaginationParam, out interface{}) (*PaginationResult, error)

WrapPageQuery 包装带有分页的查询

type RecordIDResult

type RecordIDResult struct {
	RecordID string `json:"record_id"`
}

RecordIDResult 响应记录ID

func NewRecordIDResult

func NewRecordIDResult(recordID string) *RecordIDResult

NewRecordIDResult 创建响应记录ID实例

type Trans

type Trans struct {
	DB *gorm.DB
}

Trans 事务管理

func (*Trans) Exec

func (t *Trans) Exec(ctx context.Context, fn func(context.Context) error) error

Exec 执行事务

type TransFunc

type TransFunc func(context.Context) error

TransFunc 定义事务执行函数

Jump to

Keyboard shortcuts

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