uow

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2022 License: MIT Imports: 6 Imported by: 0

README

uow (Unit of Work) pattern library in golang

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnitOfWorkNotFound = errors.New("unit of work not found, please wrap with manager.WithNew")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	NestedTransaction bool
	Formatter         KeyFormatter
}

type DbFactory

type DbFactory func(ctx context.Context, keys ...string) TransactionalDb

DbFactory resolve transactional db by database keys

type KeyFormatter

type KeyFormatter func(keys ...string) string
var (
	DefaultKeyFormatter KeyFormatter = func(keys ...string) string {
		return strings.Join(keys, "/")
	}
)

type Manager

type Manager interface {
	// WithNew create a new unit of work and execute [fn] with this unit of work
	WithNew(ctx context.Context, fn func(ctx context.Context) error, opt ...*sql.TxOptions) error
}

func NewManager

func NewManager(factory DbFactory, opts ...Option) Manager

type Option

type Option func(*Config)

func WithKeyFormatter

func WithKeyFormatter(f KeyFormatter) Option

func WithNestedNestedTransaction

func WithNestedNestedTransaction() Option

type TransactionalDb

type TransactionalDb interface {
	// Begin a transaction
	Begin(ctx context.Context, opt ...*sql.TxOptions) (db Txn, err error)
}

type Txn

type Txn interface {
	Commit() error
	Rollback() error
}

type UnitOfWork

type UnitOfWork struct {
	// contains filtered or unexported fields
}

func FromCurrentUow

func FromCurrentUow(ctx context.Context) (u *UnitOfWork, ok bool)

func NewUnitOfWork

func NewUnitOfWork(factory DbFactory, formatter KeyFormatter, opt ...*sql.TxOptions) *UnitOfWork

func (*UnitOfWork) Commit

func (u *UnitOfWork) Commit() error

func (*UnitOfWork) GetTxDb

func (u *UnitOfWork) GetTxDb(ctx context.Context, keys ...string) (tx Txn, err error)

func (*UnitOfWork) Rollback

func (u *UnitOfWork) Rollback() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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