# Functions
CtxWithHBase return new context.Context contain value with gohbase.Client.
CtxWithRedis return new context.Context contain value with redisv8.Client.
GetDBFromCtx try to get gorm.DB from context, if not found then return DB with context.Background.
GetHBaseFromCtx try to get gohbase.Client from context, if not found then return defaultHBaseClient.
GetRedisFromCtx try to get redisv8.Client from context, if not found then return defaultRedisClient.
IsInTransaction return true if current operation within a transaction.
Pipeline get redisv8.Client from ctx and run Pipeline Operation with ctx.
Transaction get gorm.DB from ctx and run Transaction Operation with ctx
How to use:
// 所有 db 操作的第一参数为 context.Context, 然后通过 ctx 读取 DB 对象
if err := db.Transaction(context.Background(), func(ctx context.Context) error {
if err := d.Create(ctx); err != nil {
return err
}
d.Name = "123"
return d.Update(ctx)
}); err != nil {
return
}
func (d *Domain) Create(ctx context.Context) error {
return GetDBFromCtx(ctx).Create(d).Error
}
func (d *Domain) Update(ctx context.Context) error {
return GetDBFromCtx(ctx).Updates(d).Error
}
*/.
TxPipeline get redisv8.Client from ctx and run Transaction Pipeline Operation with ctx.