Documentation
¶
Overview ¶
Package unitofwork provides a lightweight, clean, and idiomatic Go implementation of the Unit of Work pattern using context-based transaction propagation.
It enables decoupled domain/service layers and repositories to seamlessly share the same database transaction without polluting function signatures with framework handles.
Index ¶
- Variables
- func ExecTx(ctx context.Context, db *gorm.DB, fn func(txCtx context.Context) error, ...) (err error)
- func GetDB(ctx context.Context, defaultDB *gorm.DB) *gorm.DB
- func HasTx(ctx context.Context) bool
- func RegisterOnCommit(ctx context.Context, fn func() error)
- func RegisterOnRollback(ctx context.Context, fn func())
- func WithTx(ctx context.Context, tx *gorm.DB) context.Context
- type GormManager
- type Manager
- type Option
- type Options
- type Propagation
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoTransaction indicates that no active transaction was found in the context. ErrNoTransaction = errors.New("unitofwork: no active transaction in context") )
Functions ¶
func ExecTx ¶
func ExecTx(ctx context.Context, db *gorm.DB, fn func(txCtx context.Context) error, opts ...Option) (err error)
ExecTx executes fn inside a GORM transaction with support for: 1. Re-entrancy / nested transactions (PropagationRequired vs PropagationRequiresNew) 2. Options (Isolation level, ReadOnly) 3. Post-commit and post-rollback hooks 4. Panic recovery with automatic rollback
func GetDB ¶
GetDB extracts the active transaction from context, or falls back to defaultDB with context. If defaultDB is nil (e.g. during unit tests without a real database), it returns the transaction if present or nil.
func RegisterOnCommit ¶
RegisterOnCommit registers a callback to be executed immediately after the outer transaction successfully commits. Useful for publishing domain events, triggering async jobs, or invalidating caches.
func RegisterOnRollback ¶
RegisterOnRollback registers a callback to be executed immediately after the transaction is rolled back.
Types ¶
type GormManager ¶
type GormManager struct {
// contains filtered or unexported fields
}
GormManager implements Manager using GORM as the underlying ORM engine.
func NewGormManager ¶
func NewGormManager(db *gorm.DB) *GormManager
NewGormManager creates a new GormManager instance.
type Manager ¶
type Manager interface {
// Execute runs the provided callback inside a managed transaction context.
// If the callback returns an error or panics, the transaction is rolled back.
// If the callback returns nil, the transaction is committed and OnCommit hooks are executed.
Execute(ctx context.Context, fn func(txCtx context.Context) error, opts ...Option) error
}
Manager is the high-level Unit of Work interface that manages transactional boundaries.
type Option ¶
type Option func(*Options)
Option configures an Options instance.
func WithIsolationLevel ¶
func WithIsolationLevel(level sql.IsolationLevel) Option
WithIsolationLevel sets the transaction isolation level.
func WithPropagation ¶
func WithPropagation(p Propagation) Option
WithPropagation sets the transaction propagation behavior.
func WithReadOnly ¶
WithReadOnly marks the transaction as read-only.
type Options ¶
type Options struct {
TxOptions *sql.TxOptions
Propagation Propagation
}
Options holds configuration for transaction execution.
type Propagation ¶
type Propagation int
Propagation defines how transactions relate to each other.
const ( // PropagationRequired (default): Support a current transaction, create a new one if none exists. PropagationRequired Propagation = iota // PropagationRequiresNew: Create a new transaction, suspending the current transaction if one exists. PropagationRequiresNew )