supercontext

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PostgresDNSEnv is the env variable that should contain the Postgres connection string (Data Source Name or DSN).
	//
	// The DSN string should be in the format:
	//
	//	postgres://[user]:[password]@[host]:[port]/[database]?[...pgoptions]
	PostgresDNSEnv = "DSN"
)

Variables

View Source
var ErrNoDNS = errors.New("missing DSN environment variable")
View Source
var ErrUnsupportedContext = errors.New("unsupported context")

Functions

func Extract

func Extract[T any](ctx context.Context, key CtxKey) (T, error)

Extract a value from a context with the correct typing, It fails if the value is missing or has the wrong type.

func NewPGContext

func NewPGContext(ctx context.Context, migrations *embed.FS) (context.Context, error)

NewPGContext returns a new context with a shared PG connection.

func NewPGContextTX

func NewPGContextTX(ctx context.Context, opts *sql.TxOptions) (context.Context, func(commit bool) error, error)

NewPGContextTX creates a new Postgres context where the virtual database is replaced by a transaction.

The parent context MUST contain a virtual Postgres instance, either created with NewPGContext or NewPGContextTX.

The returned cancel function MUST be called with the argument set to true for the transaction to be committed. Omitting this call will automatically roll back the whole transaction.

func PGContext

func PGContext(ctx context.Context) (bun.IDB, error)

PGContext extracts the Postgres virtual database from the context.

Types

type CtxKey

type CtxKey string

CtxKey defines a key in the context. Used to avoid collisions with other string keys.

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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