db

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxWithHBase added in v0.0.6

func CtxWithHBase(ctx context.Context, hbase hbase.Client) context.Context

CtxWithHBase return new context.Context contain value with gohbase.Client

func CtxWithRedis added in v0.0.6

func CtxWithRedis(ctx context.Context, redis *redisv8.Client) context.Context

CtxWithRedis return new context.Context contain value with redisv8.Client

func GetDBFromCtx

func GetDBFromCtx(ctx context.Context) *gorm.DB

GetDBFromCtx try to get gorm.DB from context, if not found then return DB with context.Background

func GetHBaseFromCtx added in v0.0.6

func GetHBaseFromCtx(ctx context.Context) hbase.Client

GetHBaseFromCtx try to get gohbase.Client from context, if not found then return defaultHBaseClient

func GetRedisFromCtx added in v0.0.6

func GetRedisFromCtx(ctx context.Context) *redisv8.Client

GetRedisFromCtx try to get redisv8.Client from context, if not found then return defaultRedisClient

func IsInTransaction

func IsInTransaction(ctx context.Context) bool

IsInTransaction return true if current operation within a transaction

func Pipeline added in v0.0.6

func Pipeline(ctx context.Context, fn func(pipeline redisv8.Pipeliner) error) error

Pipeline get redisv8.Client from ctx and run Pipeline Operation with ctx

func Transaction

func Transaction(ctx context.Context, f func(context.Context) error) error

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
 }

func TxPipeline added in v0.0.6

func TxPipeline(ctx context.Context, fn func(pipeline redisv8.Pipeliner) error) error

TxPipeline get redisv8.Client from ctx and run Transaction Pipeline Operation with ctx

It's same as Pipeline, but wraps queued commands with MULTI/EXEC.

How to use: var (

	get *redisv8.StringCmd
)
err := TxPipeline(ctx, func(pipeline redisv8.Pipeliner) error {
	pipeline.Set(ctx, "key", "value", 0)
	get = pipeline.Get(ctx, "key")
	return nil
})
if err != nil {
	return err
}

fmt.Println(get.Val())
// Output: value

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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