txpool

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 6 Imported by: 0

README

txpool

txpool is a simple wrapper for frequently used pgxpool methods (from pgx library) to allow adding an optional transaction.

Before executing queries, it checks context.Context for a pgx.Tx, and runs the query on it, if its present.

Motivation

A typical repository would look like this, wrapping an instance of pgxpool.Pool:

type Repository struct {
    pool *pgxpool.Pool
}

func (r *Repository) CountRows(ctx context.Context) (int, error) {
    row := r.pgx.QueryRow(ctx, "select count(*) from some_table")
    // ...
}

func (r *Repository) DoStuff(ctx context.Context) error {
    row := r.pgx.QueryRow(ctx, "insert (id) values (1) into some_table")
    // ...
}

To execute these two queries in a single transaction, you would need to add an optional pgx.Tx argument, add nil checks, etc; alternatively you could create copies of those methods, thus unnecessarily duplicating code.

Using txpool, you can simply do this:

type Repository struct {
    pool *txpool.Pool // pgxpool -> txpool
}

func (r *Repository) CountRows(ctx context.Context) (int, error) {
    row := r.pool.QueryRow(ctx, "select count(*) from some_table")
    // ...
}

func (r *Repository) DoStuff(ctx context.Context) error {
    row := r.pool.QueryRow(ctx, "insert (id) values (1) into some_table")
    // ...
}

And use them like this:

func (r *PgxRepository) CallBoth(ctx context.Context) error {
	return r.pool.Transaction(ctx, func(ctx context.Context) error {
		count, err := r.pool.CountRows(ctx)
		// ...
		err = r.pool.DoStuff(ctx)
		// ...
		return nil
	})
}

Accessing other pgxpool.Pool methods

Use this method to access the underlying pgxpool.Pool:

func (t *TxPool) Pool() *pgxpool.Pool {
	return t.pool
}

Note

txpool currently only supports a few general methods of pgxpool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxWithTransaction

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

CtxWithTransaction puts a transaction into context. Can be used with individual methods of TxPool instead of TxPool.Transaction to avoid nesting.

Types

type Querier

type Querier interface {
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

type TxPool

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

TxPool is a wrapper for pgxpool.Pool which can accept optional transactions through context.Context.

func New

func New(pool *pgxpool.Pool) *TxPool

func (*TxPool) Exec

func (t *TxPool) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

func (*TxPool) Pool

func (t *TxPool) Pool() *pgxpool.Pool

Pool returns the underlying pgxpool.Pool.

func (*TxPool) Querier

func (t *TxPool) Querier(ctx context.Context) Querier

Querier returns a transaction if it is present in context. Otherwise, returns the underlying pgxpool.Pool.

func (*TxPool) Query

func (t *TxPool) Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)

func (*TxPool) QueryRow

func (t *TxPool) QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row

func (*TxPool) Transaction

func (t *TxPool) Transaction(ctx context.Context, f func(ctx context.Context) error) (err error)

Transaction wraps the function in a transaction, handles rollback and commit.

Jump to

Keyboard shortcuts

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