txpostgres

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package txpostgres provides transaction helpers over a DatabasePool.

WithinTx uses the caller context for application work and commit, but deferred rollback cleanup switches to a short-lived context without caller cancellation so timed-out or canceled requests still attempt to release the transaction cleanly. Missing pool wiring fails closed with ErrPoolNotConfigured instead of panicking.

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolNotConfigured = errors.New("database pool not configured")

ErrPoolNotConfigured reports that the transaction adapter was constructed without a database pool.

Functions

func AsPgError

func AsPgError(err error) (*pgconn.PgError, bool)

AsPgError converts err into a pgconn.PgError when possible.

func IsNoRows

func IsNoRows(err error) bool

IsNoRows reports whether err is pgx.ErrNoRows.

func New

New constructs a transaction manager over a DatabasePool.

Types

type DBer

type DBer interface {
	Exec(ctx context.Context, sql string, args ...any) (ports.DatabaseResult, error)
	Query(ctx context.Context, sql string, args ...any) (ports.DatabaseRows, error)
	QueryRow(ctx context.Context, sql string, args ...any) ports.DatabaseRow
}

DBer is satisfied by transactions and the pool facade below.

func FromCtx

func FromCtx(ctx context.Context, pool ports.DatabasePool) DBer

FromCtx returns the active transaction if present; otherwise a facade that acquires/releases a connection per call (no leaks).

type Manager

type Manager struct {
	Pool ports.DatabasePool
}

Manager implements ports.TxManager using a pgx-like pool.

func (*Manager) WithinTx

func (m *Manager) WithinTx(
	ctx context.Context, fn func(ctx context.Context) error,
) error

WithinTx runs fn inside a database transaction.

Jump to

Keyboard shortcuts

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