pgxutil

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2022 License: MIT Imports: 8 Imported by: 0

README

pgxutil

build-img pkg-img reportcard-img coverage-img

Go jackc/pgx helper to write proper transactions.

Features

  • Simple API.

Install

Go version 1.17+

go get github.com/cristalhq/pgxutil

Example

// create jackc/pgx pool
var pool *pgxpool.Pool

db, err := pgxutil.New(pool)
if err != nil {
	panic(err)
}

ctx := context.Background()

// to make transaction with a given isolation level
level := pgx.Serializable
errTx := db.InWriteTx(ctx, level, func(tx pgx.Tx) error {
	// TODO: good query with tx
	return nil
})
if errTx != nil {
	panic(errTx)
}

// to make read-only transaction with a read committed isolation level
errRead := db.InReadTx(ctx, func(tx pgx.Tx) error {
	// TODO: good read-only query with tx
	return nil
})
if errRead != nil {
	panic(errRead)
}	

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.

Documentation

Overview

Example
var pool *pgxpool.Pool

db, err := pgxutil.New(pool)
if err != nil {
	panic(err)
}

ctx := context.Background()

// to make transaction with a given isolation level
level := pgx.Serializable
errTx := db.InWriteTx(ctx, level, func(tx pgx.Tx) error {
	// TODO: good query with tx
	return nil
})
if errTx != nil {
	panic(errTx)
}

// to make read-only transaction with a read committed isolation level
errRead := db.InReadTx(ctx, func(tx pgx.Tx) error {
	// TODO: good read-only query with tx
	return nil
})
if errRead != nil {
	panic(errRead)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec added in v0.5.0

func Exec(ctx context.Context, db *DB, b builq.Builder) error

func ExecRead added in v0.5.0

func ExecRead[T Scannable](ctx context.Context, db *DB, b builq.Builder, dst T) error

func Read added in v0.5.0

func Read[T Scannable](ctx context.Context, db *DB, b builq.Builder, dst T) error

func ReadMany added in v0.5.0

func ReadMany[T Scannable](ctx context.Context, db *DB, b builq.Builder, dst *[]T) error

Types

type DB

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

func New

func New(pool *pgxpool.Pool) (*DB, error)

New creates a new wrapper for pgx.

func (*DB) InReadTx added in v0.3.0

func (db *DB) InReadTx(ctx context.Context, fn func(tx pgx.Tx) error) error

InReadTx runs the given function within a read-only transaction with read commited isolation level.

func (*DB) InWriteTx added in v0.3.0

func (db *DB) InWriteTx(ctx context.Context, level pgx.TxIsoLevel, fn func(tx pgx.Tx) error) error

InWriteTx runs the given function within a transaction with a given isolation level.

type Int64 added in v0.5.0

type Int64 int64

func (*Int64) Scan added in v0.5.0

func (s *Int64) Scan(row pgx.Row) error

type Scannable added in v0.5.0

type Scannable interface {
	Scan(pgx.Row) error
}

type String added in v0.5.0

type String string

func (*String) Scan added in v0.5.0

func (s *String) Scan(row pgx.Row) error

Jump to

Keyboard shortcuts

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