dbx

package
v0.0.0-...-6ce63f4 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultLimit uint32 = 2000
	MaxLimit     uint32 = 100000
)
View Source
const (
	ProtoMessageFieldCreatedAt = "created_at"
	ProtoMessageFieldUpdatedAt = "updated_at"
	ProtoMessageFieldDeletedAt = "deleted_at"
)

Variables

This section is empty.

Functions

func GetNotFoundErr

func GetNotFoundErr(err error) *errorx.Error

func IsNotFoundErr

func IsNotFoundErr(err error, errCode int32) bool

IsNotFoundErr returns true if err is an *Error and its ErrorCode matches ErrCodeRecordNotFoundSys or errCode.

func ScopeTxGo

func ScopeTxGo(db *gorm.DB, callbacks ...TxCallback) error

func TxGo

func TxGo(db *gorm.DB, executes ...TxHandler) error

Types

type GenerateIDFunc

type GenerateIDFunc func(ctx context.Context, cnt int) []uint64

type ICypher

type ICypher interface {
	Encrypt(plaintext any) (any, error)
	Decrypt(ciphertext any) (any, error)
}

type IModel

type IModel interface {
	ToORM() ITModel
}

type ITModel

type ITModel interface {
	ToSource() IModel
}

type ModelBase

type ModelBase interface {
	ProtoReflect() protoreflect.Message
	GetId() uint64
}

type OrderColumn

type OrderColumn struct {
	Field   string
	Desc    bool
	Reorder bool
}

type ProtoDeletedAt

type ProtoDeletedAt interface {
	GetDeletedAt() uint32
}

type ProtoUpsertedAt

type ProtoUpsertedAt interface {
	ProtoReflect() protoreflect.Message
	GetCreatedAt() uint32
	GetUpdatedAt() uint32
	ProtoDeletedAt
}

type QueryOpts

type QueryOpts struct {
	Selects   []string
	Where     map[string]interface{}
	Between   map[string][2]interface{}
	Like      map[string]string
	Or        map[string]interface{}
	OrLike    map[string]string
	OrBetween map[string][2]interface{}

	GroupBys []string
	OrderBys []OrderColumn
	// contains filtered or unexported fields
}

type Scope

type Scope struct {
	*gorm.DB

	NotFoundErr error
	// contains filtered or unexported fields
}

func NewScope

func NewScope(db *gorm.DB, model interface{}) *Scope

func NewScopeOfPure

func NewScopeOfPure(db *gorm.DB, model interface{}) *Scope

func (*Scope) Association

func (s *Scope) Association(value string) *Scope

func (*Scope) Between

func (s *Scope) Between(field string, arg1, arg2 interface{}) *Scope

func (*Scope) Clause

func (s *Scope) Clause(value ...clause.Expression) *Scope

func (*Scope) Count

func (s *Scope) Count() (total int64, err error)

func (*Scope) Create

func (s *Scope) Create(v interface{}) error

func (*Scope) CreateInBatches

func (s *Scope) CreateInBatches(values interface{}, batchSize int) error

CreateInBatches Insert data in batches after splitting data according to batchSize

func (*Scope) Delete

func (s *Scope) Delete(conds ...interface{}) error

func (*Scope) Eq

func (s *Scope) Eq(field string, arg interface{}) *Scope

func (*Scope) Exist

func (s *Scope) Exist(conds ...interface{}) (bool, error)

func (*Scope) Find

func (s *Scope) Find(dest interface{}, conds ...interface{}) error

func (*Scope) First

func (s *Scope) First(dest interface{}, conds ...interface{}) error

func (*Scope) Group

func (s *Scope) Group(name string) *Scope

func (*Scope) Groups

func (s *Scope) Groups(names ...string) *Scope

func (*Scope) Gt

func (s *Scope) Gt(field string, arg interface{}) *Scope

func (*Scope) Gte

func (s *Scope) Gte(field string, arg interface{}) *Scope

func (*Scope) HandlePagination

func (s *Scope) HandlePagination(opt *pagination.Pagination) *pagination.Pagination

func (*Scope) Having

func (s *Scope) Having(query interface{}, args ...interface{}) *Scope

func (*Scope) IgnoreNotFoundErr

func (s *Scope) IgnoreNotFoundErr() *Scope

func (*Scope) In

func (s *Scope) In(field string, values interface{}) *Scope

func (*Scope) Joins

func (s *Scope) Joins(query string, args ...interface{}) *Scope

func (*Scope) Like

func (s *Scope) Like(field string, value string) *Scope

func (*Scope) LikePrefix

func (s *Scope) LikePrefix(field string, value string) *Scope

func (*Scope) Limit

func (s *Scope) Limit(limit int) *Scope

func (*Scope) Lt

func (s *Scope) Lt(field string, arg interface{}) *Scope

func (*Scope) Lte

func (s *Scope) Lte(field string, arg interface{}) *Scope

func (*Scope) MultiOr

func (s *Scope) MultiOr(opts map[string]interface{}) *Scope

func (*Scope) MultiOrLike

func (s *Scope) MultiOrLike(opts map[string]string, isPrefix bool) *Scope

func (*Scope) Ne

func (s *Scope) Ne(field string, arg interface{}) *Scope

Ne :Where("field != ?", arg)

func (*Scope) NotBetween

func (s *Scope) NotBetween(field string, arg1, arg2 interface{}) *Scope

func (*Scope) NotIn

func (s *Scope) NotIn(field string, values interface{}) *Scope

func (*Scope) Offset

func (s *Scope) Offset(offset int) *Scope

func (*Scope) Omit

func (s *Scope) Omit(value ...string) *Scope

func (*Scope) Or

func (s *Scope) Or(query interface{}, args ...interface{}) *Scope

func (*Scope) Order

func (s *Scope) Order(value string) *Scope

Order db.Order("name DESC")

func (*Scope) Orders

func (s *Scope) Orders(values ...OrderColumn) *Scope

func (*Scope) PaginationQuery

func (s *Scope) PaginationQuery(opt *pagination.Pagination, list interface{}) (*pagination.Pagination, error)

func (*Scope) Preload

func (s *Scope) Preload(query string, args ...interface{}) *Scope

func (*Scope) Query

func (s *Scope) Query(opts *QueryOpts) *Scope

func (*Scope) ResetSysDateTimeField

func (s *Scope) ResetSysDateTimeField(v interface{})

ResetSysDateTimeField To prevent someone from passing in these three fields by mistake, this method is provided to reset

func (*Scope) ReturnColumns

func (s *Scope) ReturnColumns(columns ...string) *Scope

ReturnColumns Return data, return all field data without passing columns

func (*Scope) Select

func (s *Scope) Select(fields ...string) *Scope

func (*Scope) SetGenerateIDFunc

func (s *Scope) SetGenerateIDFunc(idFunc GenerateIDFunc) *Scope

func (*Scope) SetNotFoundErr

func (s *Scope) SetNotFoundErr(notFoundErrCode int32) *Scope

func (*Scope) Unscoped

func (s *Scope) Unscoped() *Scope

func (*Scope) Update

func (s *Scope) Update(v interface{}, conds ...interface{}) error

Update updates attributes using callbacks. values must be a struct or map.

func (*Scope) Where

func (s *Scope) Where(query interface{}, args ...interface{}) *Scope

type T

type T struct {
	NotFoundErrCode int32
	BatchCreateSize int
	PrimaryKey      string

	// encrypt config
	EncryptStructFieldMap map[string]bool // {structFieldName:true}
	EncryptDBFieldMap     map[string]bool // {dbFieldName:true}
	Encryptor             ICypher
	NeedEncrypt           bool
	NeedDecrypt           bool
	// contains filtered or unexported fields
}

func NewT

func NewT(fn func() *gorm.DB, m interface{}, errCode int32, opts ...TOption) *T

func (*T) BatchCreate

func (x *T) BatchCreate(list interface{}) (err error)

func (*T) CheckAndCrypto

func (x *T) CheckAndCrypto(m interface{}, cryptoFunc func(s any) (any, error)) error

func (*T) CloneTModel

func (x *T) CloneTModel() interface{}

func (*T) Count

func (x *T) Count(whereMap map[string]interface{}) (int64, error)

func (*T) Create

func (x *T) Create(m interface{}) (err error)

func (*T) Decrypt

func (x *T) Decrypt(ciphertext any) (any, error)

func (*T) DeleteByPk

func (x *T) DeleteByPk(pk interface{}) error

func (*T) DeleteByWhere

func (x *T) DeleteByWhere(query interface{}, args ...interface{}) error

func (*T) Encrypt

func (x *T) Encrypt(plaintext any) (any, error)

func (*T) ExistByWhere

func (x *T) ExistByWhere(query interface{}, args ...interface{}) (bool, error)

func (*T) GetOne

func (x *T) GetOne(pk uint64, m interface{}) error

func (*T) GetOneByWhere

func (x *T) GetOneByWhere(whereMap map[string]interface{}, m interface{}) (err error)

func (*T) ListAll

func (x *T) ListAll(opts interface{}, listPtr interface{}) error

func (*T) ListWithPagination

func (x *T) ListWithPagination(paginate *pagination.Pagination, opts interface{}, listPtr interface{}) (*pagination.Pagination, error)

func (*T) Scope

func (x *T) Scope() *Scope

func (*T) SetCreateBatchSize

func (x *T) SetCreateBatchSize(size int)

func (*T) Update

func (x *T) Update(m interface{}, whereMap map[string]interface{}) (rows int64, err error)

type TOption

type TOption func(*T)

TOption is a function that takes a pointer to a T and modifies it.

func WithEncryptFieldMap

func WithEncryptFieldMap(encryptStructFieldMap map[string]string) TOption

WithEncryptFieldMap is a function that takes a map of struct field names to database field names and sets the encryptStructFieldMap field of the T struct. {structFieldName: dbFieldName}

func WithEncryptor

func WithEncryptor(encryptor ICypher) TOption

WithEncryptor is a function that takes an ICypher and sets the Encryptor field of the T struct

func WithNeedDecrypt

func WithNeedDecrypt(needDecrypt bool) TOption

WithNeedDecrypt enable decryption of encrypted fields when get data.

func WithNeedEncrypt

func WithNeedEncrypt(needEncrypt bool) TOption

WithNeedEncrypt enable some specified fields to be encrypted

func WithNotFoundErrCode

func WithNotFoundErrCode(notFoundErrCode int32) TOption

WithNotFoundErrCode set the NotFoundErrCode when record not found

func WithPrimaryKey

func WithPrimaryKey(pk string) TOption

WithPrimaryKey is a function that takes a string and sets the PrimaryKey field of the T struct

type TxCallback

type TxCallback func() (model interface{}, execute func(scope *Scope) error)

type TxHandler

type TxHandler func(tx *gorm.DB) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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