tx

package
v0.0.0-...-22d23f5 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTxNotFound = errors.New("can not find transaction inside context")

Functions

func MockManager

func MockManager()

func SetupManager

func SetupManager(db *sqlx.DB, extensions ...Extension)

Types

type Extension

type Extension func(conn sqlx.ExtContext) sqlx.ExtContext

type Func

type Func func(ctx context.Context) error

type ManagerTx

type ManagerTx interface {
	// Do provide possibility to run transaction among all db functions inside txFunc.
	// Transaction level configurable by sql.Options. By default, will be applied
	// default level of used database, for example, 'read committed' in PostgreSQL.
	//
	// Example:
	//	func main() {
	//		tx.Manager().Do(ctx, func(ctx context.Context) error {
	//			res, err := repo.GetItem(ctx, 1)
	//			...
	//			err = nextRepo.SetItem(ctx, res)
	//		})
	//	}
	Do(ctx context.Context, txFunc Func, opts ...sql.TxOptions) error
	// Conn extracts transaction from provided context.Context. If transaction is not
	// contains inside context.Context, then function return default database connection.
	//
	// Example:
	//	func (r *repo) GetItem(ctx context.Context, id int64) (Item, error) {
	//		conn := tx.Manager().Conn(ctx)
	//
	//		res, err := conn.QueryContext(ctx, query)
	//		...
	//	}
	Conn(ctx context.Context) sqlx.ExtContext
}

ManagerTx is helper that provide possibility to execute group of database queries in single transaction.

func Manager

func Manager() ManagerTx

Jump to

Keyboard shortcuts

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