Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDBFromCtx ¶
GetDBFromCtx try to get gorm.DB from context, if not found then return DB with context.Background
func IsInTransaction ¶
IsInTransaction return true if current operation within a transaction
func Transaction ¶
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 }
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.