pgxatomic

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 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 interface {
		Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	},
	sql string,
	args ...any,
) (pgconn.CommandTag, error)

Exec is a wrapper around pgx Exec method.

func NewRunner

func NewRunner(db txStarter, o pgx.TxOptions) (runner, error)

func Query

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

Query is a wrapper around pgx Query method.

func QueryRow

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

QueryRow is a wrapper around pgx QueryRow method.

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

Jump to

Keyboard shortcuts

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