uow

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 8 Imported by: 13

README

uow (Unit of Work) pattern library in golang

install

go get -u github.com/go-saas/uow

usage See https://github.com/go-saas/uow/tree/main/gorm/db_test.go

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

func NewCurrentUow added in v0.0.5

func NewCurrentUow(ctx context.Context, u *UnitOfWork) context.Context

func WithCurrentUnitOfWork added in v0.0.5

func WithCurrentUnitOfWork(ctx context.Context, fn func(ctx context.Context) error) (err error)

WithCurrentUnitOfWork wrap a function into current unit of work. Automatically Rollback if function returns error

func WithUnitOfWork added in v0.0.5

func WithUnitOfWork(ctx context.Context, u *UnitOfWork, fn func(ctx context.Context) error) (err error)

Types

type Config

type Config struct {
	DisableNestedTransaction bool
	// contains filtered or unexported fields
}

type DbFactory

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

DbFactory resolve transactional db by database keys

type IdGenerator added in v0.0.4

type IdGenerator func(ctx context.Context) string
var (
	DefaultIdGenerator IdGenerator = func(ctx context.Context) string {
		return uuid.New().String()
	}
)

type KeyFormatter

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

type Manager

type Manager interface {
	CreateNew(ctx context.Context, opt ...*sql.TxOptions) (*UnitOfWork, error)
	// 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 WithDisableNestedNestedTransaction added in v0.0.4

func WithDisableNestedNestedTransaction() Option

func WithIdGenerator added in v0.0.4

func WithIdGenerator(idGen IdGenerator) Option

func WithKeyFormatter

func WithKeyFormatter(f KeyFormatter) Option

type TransactionalDb

type TransactionalDb interface {
	// Begin a transaction
	Begin(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 (*UnitOfWork) Commit

func (u *UnitOfWork) Commit() error

func (*UnitOfWork) GetId added in v0.0.5

func (u *UnitOfWork) GetId() string

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