postgres

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package postgres provides several github.com/jackc/pgx related helpers for interacting with postgres.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(
	logger *slog.Logger,
	dsn string,
	maxConns int,
	maxIdleTime string,
	connectTimeout int,
	sleepBeforeRetry time.Duration,
	maxRetryDuration time.Duration,
) (*pgxpool.Pool, error)

Connect connects to postgres and returns a *pgxpool.Pool.

The provided arguments are:

  • dsn: the url to reach the database
  • maxConns: the maximum amount of open connections
  • maxIdleTime: the maximum idle time of an open connection
  • connectTimeout: the timeout on connecting to the database in seconds
  • sleepBeforeRetry: duration to sleep before trying to connect again
  • maxRetryDuration: total amount of time to try and achieve a database connection

func PgxErrorToHTTPError added in v0.1.1

func PgxErrorToHTTPError(err error) error

PgxErrorToHTTPError converts a database error from github.com/jackc/pgx to an appropriate HTTP error.

Types

type DB

type DB interface {
	Exec(
		ctx context.Context,
		sql string,
		arguments ...any,
	) (pgconn.CommandTag, error)
	Query(
		ctx context.Context,
		sql string,
		optionsAndArgs ...any,
	) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Begin(ctx context.Context) (pgx.Tx, error)
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	Ping(ctx context.Context) error
}

DB provides a uniform interface for the postgres database connection, pools and transactions.

type SpanDB

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

SpanDB is used to wrap database actions in [sentry.Span]s.

func NewSpanDB added in v0.1.1

func NewSpanDB(db DB) *SpanDB

NewSpanDB creates a new *SpanDB.

func (*SpanDB) Begin

func (db *SpanDB) Begin(ctx context.Context) (pgx.Tx, error)

Begin doesn't wrap Begin in a [sentry.Span] as this makes little sense for starting a transaction.

func (*SpanDB) BeginTx added in v0.1.9

func (db *SpanDB) BeginTx(
	ctx context.Context,
	txOptions pgx.TxOptions,
) (pgx.Tx, error)

BeginTx doesn't wrap BeginTx in a [sentry.Span] as this makes little sense for starting a transaction.

func (*SpanDB) Exec

func (db *SpanDB) Exec(
	ctx context.Context,
	sql string,
	arguments ...any,
) (pgconn.CommandTag, error)

Exec is used to wrap Exec in a [sentry.Span].

func (*SpanDB) Ping added in v0.1.1

func (db *SpanDB) Ping(ctx context.Context) error

Ping doesn't wrap Ping in a [sentry.Span] as this makes little sense for pinging the db.

func (*SpanDB) Query

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

Query is used to wrap Query in a [sentry.Span].

func (*SpanDB) QueryRow

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

QueryRow is used to wrap QueryRow in a [sentry.Span].

func (*SpanDB) SendBatch added in v0.1.9

func (db *SpanDB) SendBatch(
	ctx context.Context,
	b *pgx.Batch,
) pgx.BatchResults

SendBatch is used to wrap SendBatch in a [sentry.Span].

Jump to

Keyboard shortcuts

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