uow

package
v0.0.0-...-91ee7f2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package uow defines the contract for Unit of Work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	// Begin begins the transaction.
	Begin(ctx context.Context) (Tx, error)
	// Exec executes the query without getting the data from the row.
	Exec(ctx context.Context, query string, args ...interface{}) (int64, error)
	// Query runs the query given.
	Query(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

DB defines an interface for database functionality. This interface exists for the sake of Unit of Work (UoW) pattern. The moment this project is being made, Go doesn't have proper UoW library that can abstract the process.

I will agree to the opinion that says this is not proper/correct way to achieve UoW or transaction as a business. I read many articles and this is the conclusion I made using all knowledges I have.

type Tr

type Tr trmpgx.Tr

Tr defines an interface for transaction.

type Tx

type Tx interface {
	DB
	// Commit commits the transaction.
	Commit(ctx context.Context) error
	// Rollback rolls back the transaction.
	Rollback(ctx context.Context) error
}

Tx defines an interface for business transaction functionality. This interface exists for the sake of Unit of Work (UoW) pattern. See more in the comment on DB interface.

type TxGetter

type TxGetter interface {
	DefaultTrOrDB(ctx context.Context, db trmpgx.Tr) trmpgx.Tr
}

TxGetter defines an interface to get transaction from context or use db from param. Copied from https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/pgxv5/v2#CtxGetter. It is used mostly for testing.

type TxManager

type TxManager interface {
	Do(ctx context.Context, fn func(ctx context.Context) error) (err error)
	DoWithSettings(ctx context.Context, s trm.Settings, fn func(ctx context.Context) error) (err error)
	Init(ctx context.Context, s trm.Settings) (context.Context, manager.Closer, error)
}

TxManager defines the interface to manage transaction. Copied from https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/trm/v2/manager#Manager. It is used mostly for testing.

type UnitOfWork

type UnitOfWork interface {
	// Begin begins the transaction.
	Begin(ctx context.Context) (Tx, error)
	// Finish finishes the transaction.
	Finish(ctx context.Context, tx Tx, err error) error
}

UnitOfWork defines the high-level use case of Unit of Work pattern.

type UnitWorker

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

UnitWorker represents a unit of work. It actually just to begin and finish the transaction.

func NewUnitWorker

func NewUnitWorker(db DB) *UnitWorker

NewUnitWorker creates an instance of UnitWorker.

func (*UnitWorker) Begin

func (u *UnitWorker) Begin(ctx context.Context) (Tx, error)

Begin begins the transaction.

func (*UnitWorker) Finish

func (u *UnitWorker) Finish(ctx context.Context, tx Tx, err error) error

Finish finishes the transaction.

Jump to

Keyboard shortcuts

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