Lightweight unit-of-work helper for Go. It abstracts transaction lifecycle (begin/commit/rollback), supports nesting with reference counting, integrates with context.Context, and provides a convenient AutoCommit pattern.
- Nested transaction
Start/Commitwith reference counting - Context helpers:
AddToContext,GetFromContext,RollBack AutoCommit(ctx, err)to commit on success or rollback on error/panic- Force rollback via context critical status
- Pluggable
Databaseinterface (adapter over your DB/ORM)
Replace github.com/bi0dread/unitow with your repo path if needed.
go get github.com/bi0dread/unitowImplement the Database interface for your DB/ORM. Example for GORM:
package yourpkg
import (
"gorm.io/gorm"
unitow "github.com/bi0dread/unitow"
)
type GormDB struct{ DB *gorm.DB }
func (g GormDB) Begin() unitow.Database { return GormDB{DB: g.DB.Begin()} }
func (g GormDB) Commit() error { return g.DB.Commit().Error }
func (g GormDB) Rollback() error { return g.DB.Rollback().Error }Use AddToContext to attach a transaction and AutoCommit to handle commit/rollback automatically. Use a named return err.
func CreateOrder(ctx context.Context, db *gorm.DB) (err error) {
u := unitow.New(GormDB{DB: db})
ctx, err = unitow.AddToContext(ctx, u.NewTransaction("CreateOrder"))
if err != nil { return err }
defer func() { _ = unitow.AutoCommit(ctx, err) }()
// Use transactional DB
tx := unitow.GetFromContext(ctx).Transaction().(GormDB).DB
// ... perform writes with tx ...
return nil
}Start/Commit can be nested; commit only happens when the outermost scope commits.
trx := u.NewTransaction("ComplexOperation")
trx.Start()
defer trx.Commit()
// inner
trx.Start()
defer trx.Commit()
// work ...You can force a rollback regardless of success by setting the critical status in context.
ctx = unitow.SetContextCriticalStatusToForceRollback(ctx)If your function returns an error that implements TransactionError, AutoCommit will commit or rollback based on DoCommit().
type txErr struct{ commit bool }
func (e txErr) Error() string { return "reason" }
func (e txErr) DoCommit() bool { return e.commit }
func Handler(ctx context.Context) (err error) {
u := unitow.New(GormDB{DB: db})
ctx, err = unitow.AddToContext(ctx, u.NewTransaction("Handler"))
if err != nil { return err }
defer func() { _ = unitow.AutoCommit(ctx, err) }()
// ...
return txErr{commit: true}
}New(db Database) UnitowUnitow.NewTransaction(name string) TransactionTransaction.Start()/Transaction.Commit()/Transaction.CommitAll()/Transaction.Rollback()Transaction.Transaction() Database(returns transactional DB if active)AddToContext(ctx, uow)/GetFromContext(ctx)/RollBack(ctx)AutoCommit(ctx, err)SetContextCriticalStatusToForceRollback(ctx)GenerateName(skip int)type TransactionError interface { DoCommit() bool }
go test ./...