pgctx

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

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginTxOption

func BeginTxOption[R any](ctx context.Context, opt *pgsql.TxOptions, f func(ctx context.Context) (*R, error)) (*R, error)

BeginTxOption is a shortcut function that runs f in a transaction. and returns its result.

func Collect

func Collect[T any](ctx context.Context, sql string, args ...any) ([]*T, error)

func CollectOne

func CollectOne[T any](ctx context.Context, sql string, args ...any) (*T, error)

func Committed

func Committed(ctx context.Context, f func(ctx context.Context))

Committed calls f after committed or immediate if not in tx

func Exec

func Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)

Exec calls db.ExecContext

func GetTx

func GetTx(ctx context.Context) pgx.Tx

func IsInTx

func IsInTx(ctx context.Context) bool

IsInTx checks is context inside RunInTx

func Iter

func Iter(ctx context.Context, iter pgsql.Iterator, query string, args ...any) error

Iter calls pgsql.IterContext

func KeyMiddleware

func KeyMiddleware(key any, db DB) func(h http.Handler) http.Handler

func Middleware

func Middleware(db DB) func(h http.Handler) http.Handler

Middleware injects db into request's context

func NewContext

func NewContext(ctx context.Context, db DB) context.Context

NewContext creates new context

func NewKeyContext

func NewKeyContext(ctx context.Context, key any, db DB) context.Context

func Query

func Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query calls db.QueryContext

func QueryRow

func QueryRow(ctx context.Context, query string, args ...any) pgx.Row

QueryRow calls db.QueryRowContext

func RunInReadOnlyTx

func RunInReadOnlyTx(ctx context.Context, f func(ctx context.Context) error) error

RunInReadOnlyTx calls RunInTxOptions with read only options

func RunInTx

func RunInTx(ctx context.Context, f func(ctx context.Context) error) error

RunInTx calls RunInTxOptions with default options

func RunInTxOptions

func RunInTxOptions(ctx context.Context, opt *pgsql.TxOptions, f func(ctx context.Context) error) error

RunInTxOptions starts sql tx if not started

func RunTx

func RunTx[R any](ctx context.Context, f func(ctx context.Context) (*R, error)) (*R, error)

func With

func With(ctx context.Context, key any) context.Context

With creates new empty key context with db from keyed context

Types

type DB

type DB interface {
	Queryer
	pgsql.BeginTxer
}

func GetDB

func GetDB(ctx context.Context) DB

func GetDBKey

func GetDBKey(ctx context.Context, key any) DB

type Queryer

type Queryer interface {
	QueryRow(context.Context, string, ...any) pgx.Row
	Query(context.Context, string, ...any) (pgx.Rows, error)
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
}

Queryer interface

Jump to

Keyboard shortcuts

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