Documentation
¶
Index ¶
- Constants
- func Create(v any) error
- func Delete(value any, where ...any) (err error)
- func Find(out any, where ...any) (err error)
- func First(out any, where ...any) (exist bool, err error)
- func FromJsonb(jsonb datatypes.JSON, v any) (err error)
- func GetDB() *gorm.DB
- func MustToJsonb(v any) (jsonb datatypes.JSON)
- func NewLogrAdapter(l logr.Logger) gormLogger.Writer
- func Save(v any) error
- func ToJsonb(v any) (jsonb datatypes.JSON, err error)
- func Tx(tx func(dbCtx DBContext) error) error
- type DBContext
- type DBContextImpl
- func (dbCtx *DBContextImpl) Begin() (err error)
- func (dbCtx *DBContextImpl) Commit() (err error)
- func (dbCtx *DBContextImpl) Create(v any) error
- func (dbCtx *DBContextImpl) Delete(value any, where ...any) (err error)
- func (dbCtx *DBContextImpl) Exec(sql string, values ...any) error
- func (dbCtx *DBContextImpl) Find(out any, where ...any) (err error)
- func (dbCtx *DBContextImpl) First(out any, where ...any) (exist bool, err error)
- func (dbCtx *DBContextImpl) NewQuery() Query
- func (dbCtx *DBContextImpl) Rollback() error
- func (dbCtx *DBContextImpl) Save(v any) error
- func (dbCtx *DBContextImpl) SetContext(ctx context.Context)
- func (dbCtx *DBContextImpl) Tx(tx func(dbCtx DBContext) error) (err error)
- type Model
- type ModelUnscoped
- type Query
- type QueryImpl
- func (q *QueryImpl) Count() (count int64, err error)
- func (q *QueryImpl) DBContext() DBContext
- func (q *QueryImpl) Find(out any) error
- func (q *QueryImpl) First(out any) (exist bool, err error)
- func (q *QueryImpl) Limit(limit int) Query
- func (q *QueryImpl) Model(model any) Query
- func (q *QueryImpl) Offset(offset int) Query
- func (q *QueryImpl) OrderBy(exp any) Query
- func (q *QueryImpl) Page(page, pageSize int) Query
- func (q *QueryImpl) Raw(sql string, values ...any) Query
- func (q *QueryImpl) Scan(out any) (err error)
- func (q *QueryImpl) Where(query any, args ...any) Query
Constants ¶
const ( EnvDBInit = "DB_INIT" EnvDBDriver = "DB_DRIVER" EnvDBConnection = "DB_CONNECTION" EnvDBConnMaxLifetime = "DB_CONNMAXLIFETIME" EnvDBMaxOpenConns = "DB_MAXOPENCONNS" EnvDBMaxIdleConns = "DB_MAXIDLECONNS" EnvDBDebug = "DB_DEBUG" )
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
func MustToJsonb ¶
MustToJsonb convert object to jsonb and ignore errors
func NewLogrAdapter ¶
func NewLogrAdapter(l logr.Logger) gormLogger.Writer
Types ¶
type DBContext ¶
type DBContext interface {
SetContext(ctx context.Context)
// db
Create(v any) error
Save(v any) error
First(out any, where ...any) (exist bool, err error)
Find(out any, where ...any) (err error)
Delete(value any, where ...any) (err error)
Exec(sql string, values ...any) error
Tx(tx func(dbCtx DBContext) error) (err error)
// transaction
Begin() (err error)
Commit() (err error)
Rollback() error
// query
NewQuery() Query
}
DBContext indicates the db context interface
func DefaultDBContext ¶
DefaultDBContext returns a new DBContextImpl if the input dbCtx is nil
type DBContextImpl ¶
type DBContextImpl struct {
// contains filtered or unexported fields
}
DBContextImpl is a DB operation package
func (*DBContextImpl) Begin ¶
func (dbCtx *DBContextImpl) Begin() (err error)
Begin a new transaction
func (*DBContextImpl) Commit ¶
func (dbCtx *DBContextImpl) Commit() (err error)
Commit a transaction
func (*DBContextImpl) Create ¶
func (dbCtx *DBContextImpl) Create(v any) error
Create insert the value into database
func (*DBContextImpl) Delete ¶
func (dbCtx *DBContextImpl) Delete(value any, where ...any) (err error)
Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
func (*DBContextImpl) Exec ¶
func (dbCtx *DBContextImpl) Exec(sql string, values ...any) error
Exec execute a raw SQL expression
func (*DBContextImpl) Find ¶
func (dbCtx *DBContextImpl) Find(out any, where ...any) (err error)
Find find records that match given conditions
func (*DBContextImpl) First ¶
func (dbCtx *DBContextImpl) First(out any, where ...any) (exist bool, err error)
First find first record that match given conditions, order by primary key
func (*DBContextImpl) NewQuery ¶
func (dbCtx *DBContextImpl) NewQuery() Query
NewQuery return a new QueryImpl
func (*DBContextImpl) Rollback ¶
func (dbCtx *DBContextImpl) Rollback() error
Rollback a transaction
func (*DBContextImpl) Save ¶
func (dbCtx *DBContextImpl) Save(v any) error
Save update value in database, if the value doesn't have primary key, will insert it
func (*DBContextImpl) SetContext ¶
func (dbCtx *DBContextImpl) SetContext(ctx context.Context)
SetContext the context of the DBContextImpl
type Model ¶
type Model struct {
ModelUnscoped
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
}
Model base DB model
type ModelUnscoped ¶
type Query ¶
type Query interface {
DBContext() DBContext
Model(model any) Query
Where(query any, args ...any) Query
OrderBy(exp any) Query
Offset(offset int) Query
Limit(limit int) Query
Page(page, pageSize int) Query
Raw(sql string, values ...any) Query
Find(out any) error
First(out any) (exist bool, err error)
Count() (count int64, err error)
Scan(out any) (err error)
}
Query indicates the db query interface
type QueryImpl ¶
type QueryImpl struct {
// contains filtered or unexported fields
}
QueryImpl DB query model