postgres

package
v0.0.0-...-3b74e67 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TransactionKey = txKey{}

TransactionKey is the key used to store a pgx.Tx in a context.Context.

Functions

func AppendRowsNonNil

func AppendRowsNonNil[T comparable, S ~[]T](slice S, rows pgx.Rows, fn pgx.RowToFunc[T]) (S, error)

AppendRowsNonNil iterates through rows, calling fn for each row, and appending the results into a slice of T if the result is not nil.

This function closes the rows automatically on return.

func CollectRowsNonNil

func CollectRowsNonNil[T comparable](rows pgx.Rows, fn pgx.RowToFunc[T]) ([]T, error)

CollectRowsNonNil iterates through rows, calling fn for each row, and collecting the results into a slice of T if the result is not nil.

This function closes the rows automatically on return.

func GetTestLogger

func GetTestLogger() *slog.Logger

func WithTx

func WithTx(ctx context.Context, tx pgx.Tx) context.Context

WithTx returns a new context with the provided transaction. Use this function when you start a transaction.

Types

type DB

type DB interface {
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
	Begin(ctx context.Context) (pgx.Tx, error)
}

DB is an interface that both pgx.Tx and *pgxpool.Pool satisfy. This allows our methods to work transparently with either a transaction or the pool.

func GetDBFromContext

func GetDBFromContext(ctx context.Context, pool *pgxpool.Pool) DB

GetDBFromContext returns a DB instance that is either the transaction stored in the context or the provided pool.

type DbCleanup

type DbCleanup func()

func GetTestPool

func GetTestPool() (*pgxpool.Pool, DbCleanup)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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