postgres

package
v0.0.0-...-ca86632 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectAll

func CollectAll[T any](rows pgx.Rows, err error) ([]T, error)

CollectAll scans all rows into a slice of T using struct field name matching. Returns an empty slice if no rows are found.

func CollectOne

func CollectOne[T any](rows pgx.Rows, err error) (*T, error)

CollectOne scans a single row into T using struct field name matching. Returns nil, nil if no rows are found.

func CollectOneScalar

func CollectOneScalar[T any](rows pgx.Rows, err error) (T, error)

CollectOneScalar scans a single scalar value from one row. Returns the zero value of T if no rows are found.

func InTransaction

func InTransaction(ctx context.Context) bool

InTransaction reports whether ctx contains an active transaction.

Types

type Option

type Option func(*pgxpool.Config)

Option configures a pgxpool.Config.

func WithMaxConns

func WithMaxConns(n int32) Option

WithMaxConns sets the maximum number of connections in the pool.

func WithMinConns

func WithMinConns(n int32) Option

WithMinConns sets the minimum number of connections in the pool.

type Postgres

type Postgres struct {
	Pool *pgxpool.Pool
}

Postgres wraps a pgxpool.Pool and implements DB.

func New

func New(ctx context.Context, dsn string, opts ...Option) (*Postgres, error)

New creates a new Postgres connection pool, applies options, and pings the server.

func (*Postgres) Close

func (p *Postgres) Close()

Close closes all connections in the pool.

func (*Postgres) Exec

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

Exec implements Querier.

func (*Postgres) Ping

func (p *Postgres) Ping(ctx context.Context) error

Ping verifies the connection is alive.

func (*Postgres) Query

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

Query implements Querier.

func (*Postgres) QueryRow

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

QueryRow implements Querier.

type Querier

type Querier interface {
	Exec(ctx context.Context, sql string, args ...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
}

Querier is the common interface for *pgxpool.Pool and pgx.Tx. It allows repository methods to work transparently within or outside transactions.

func GetQuerier

func GetQuerier(ctx context.Context, pool *pgxpool.Pool) Querier

GetQuerier returns the pgx.Tx stored in ctx, or falls back to pool. This allows repository methods to be transaction-aware without knowing whether they are called inside a transaction.

type TransactionManager

type TransactionManager interface {
	WithinTransaction(ctx context.Context, fn func(ctx context.Context) error) error
}

TransactionManager defines the interface for running code within a transaction.

type TxManager

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

TxManager implements TransactionManager using pgxpool.

func NewTxManager

func NewTxManager(pool *pgxpool.Pool) *TxManager

NewTxManager returns a new TxManager backed by pool.

func (*TxManager) WithinTransaction

func (m *TxManager) WithinTransaction(ctx context.Context, fn func(ctx context.Context) error) error

WithinTransaction begins a transaction, calls fn with a context containing the transaction, and commits on success or rolls back on error.

Jump to

Keyboard shortcuts

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