postgres

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 14 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
	Begin(ctx context.Context) (pgx.Tx, error)
	Ping(ctx context.Context) error
}

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

type PgxSyncRow added in v0.1.1

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

PgxSyncRow is a concurrent wrapper for pgx.Row.

func (*PgxSyncRow) Scan added in v0.1.1

func (row *PgxSyncRow) Scan(dest ...any) error

Scan scans the data of PgxSyncRow into dest.

type PgxSyncRows added in v0.1.1

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

PgxSyncRows is a concurrent wrapper for pgx.Rows.

func (*PgxSyncRows) Close added in v0.1.1

func (rows *PgxSyncRows) Close()

Close doesn't do anything for PgxSyncRows as these are closed in [Query].

func (*PgxSyncRows) CommandTag added in v0.1.1

func (rows *PgxSyncRows) CommandTag() pgconn.CommandTag

CommandTag fetches the pgconn.CommandTag.

func (*PgxSyncRows) Conn added in v0.1.1

func (rows *PgxSyncRows) Conn() *pgx.Conn

Conn fetches the pgx.Conn.

func (*PgxSyncRows) Err added in v0.1.1

func (rows *PgxSyncRows) Err() error

Err fetches any errors.

func (*PgxSyncRows) FieldDescriptions added in v0.1.1

func (rows *PgxSyncRows) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions fetches [pgconn.FieldDescription]s.

func (*PgxSyncRows) Next added in v0.1.1

func (rows *PgxSyncRows) Next() bool

Next continues to the next row of PgxSyncRows if there is one.

func (*PgxSyncRows) RawValues added in v0.1.1

func (rows *PgxSyncRows) RawValues() [][]byte

RawValues fetches the raw values of the current row.

func (*PgxSyncRows) Scan added in v0.1.1

func (rows *PgxSyncRows) Scan(dest ...any) error

Scan scans the data of the current row into dest.

func (*PgxSyncRows) Values added in v0.1.1

func (rows *PgxSyncRows) Values() ([]any, error)

Values fetches the values of the current row.

type PgxSyncTx added in v0.1.1

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

PgxSyncTx uses database.SyncTx to make sure pgx.Tx can be used concurrently.

func CreatePgxSyncTx added in v0.1.1

func CreatePgxSyncTx(ctx context.Context, db DB) *PgxSyncTx

CreatePgxSyncTx returns a pgx.Tx which works concurrently.

func (*PgxSyncTx) Begin added in v0.1.1

func (tx *PgxSyncTx) Begin(ctx context.Context) (pgx.Tx, error)

Begin is used to wrap pgx.Tx.Begin in a database.SyncTx.

func (*PgxSyncTx) Exec added in v0.1.1

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

Exec is used to wrap pgx.Tx.Exec in a database.SyncTx.

func (*PgxSyncTx) Ping added in v0.1.1

func (tx *PgxSyncTx) Ping(ctx context.Context) error

Ping is used to wrap [pgx.Tx.Conn.Ping] in a database.SyncTx.

func (*PgxSyncTx) Query added in v0.1.1

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

Query is used to wrap pgx.Tx.Query in a database.SyncTx.

func (*PgxSyncTx) QueryRow added in v0.1.1

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

QueryRow is used to wrap pgx.Tx.QueryRow in a database.SyncTx.

func (*PgxSyncTx) Rollback added in v0.1.1

func (tx *PgxSyncTx) Rollback(ctx context.Context) error

Rollback is used to rollback a PgxSyncTx.

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) 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].

Jump to

Keyboard shortcuts

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