pgxatomic

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 5 Imported by: 0

README

pgxatomic

pgxatomic is a library of tools that allow you to implement transfer of clean control to transactions to a higher level by adding transaction in a context.Context using pgx driver.

schema

Example Usage

  1. You can use pgxatomic.Pool within repository implementation. It's simple wrapper around pgxpool.Pool which is wrapping Query, QueryRow and Exec methods with pgxatomic query functions.
type orderRepo struct {
    pool *pgxatomic.Pool // pgxpool.Pool wrapper
}

type order struct {
    ID uuid.UUID
    Cost int
}

func (r *orderRepo) Insert(ctx context.Context, cost int) order {
    rows, _ := r.pool.Query(ctx, "insert into order(cost) values ($1) RETURNING id, cost", cost)
    o, _ := pgx.CollectOneRow(rows, pgx.RowToStructByPos[order])
    return o
}

Or you can use Query, QueryRow, Exec functions directly from the library.

  1. Run wrapped usecase method calls within txFunc using pgxatomic.runner.Run function
conf, _ := pgxpool.ParseConfig("postgres://user:pass@localhost:5432/postgres")
pool, _ := pgxpool.NewWithConfig(context.Background(), conf)

r, _ := pgxatomic.NewRunner(pool, pgx.TxOptions{})

_ = r.Run(context.Background(), func(txCtx context.Context) error {
    _ = orderService.Create(txCtx)
    _ = balanceService.Withdraw(txCtx)
    return nil
})

Error handling is omitted on purpose, handle all errors!

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(ctx context.Context, db executor, sql string, args ...any) (pgconn.CommandTag, error)

Exec is a wrapper around pgx Exec method.

func Query

func Query(ctx context.Context, db querier, sql string, args ...any) (pgx.Rows, error)

Query is a wrapper around pgx Query method.

func QueryRow

func QueryRow(ctx context.Context, db queryRower, sql string, args ...any) pgx.Row

QueryRow is a wrapper around pgx QueryRow method.

func TxFromContext added in v1.0.7

func TxFromContext(ctx context.Context) pgx.Tx

TxFromContext return pgx.Tx from context or nil if not found.

func WithTx added in v1.0.7

func WithTx(ctx context.Context, tx pgx.Tx) context.Context

WithTx sets pgx.Tx into context.

Types

type Pool added in v1.0.1

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

Pool wraps pgxpool.Pool query methods with pgxatomic corresponding functions which injects pgx.Tx into context.

func NewPool added in v1.0.1

func NewPool(p *pgxpool.Pool) (Pool, error)

func (Pool) Exec added in v1.0.1

func (p Pool) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

func (Pool) Query added in v1.0.1

func (p Pool) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

func (Pool) QueryRow added in v1.0.1

func (p Pool) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

type Runner added in v1.0.7

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

Runner starts transaction in Run method by wrapping txFunc using db, pgx.Conn and pgxpool.Pool implements db.

func NewRunner

func NewRunner(db txStarter, opts pgx.TxOptions) (Runner, error)

func (Runner) Run added in v1.0.7

func (r Runner) Run(ctx context.Context, txFunc func(ctx context.Context) error) error

Run wraps txFunc in pgx.BeginTxFunc with injected pgx.Tx into context and runs it.

Jump to

Keyboard shortcuts

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