stdpgxfx

package
v0.0.249 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package stdpgxfx provides sql.DB connection pools usng pgx/v5.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyPoolHostConventions added in v0.0.223

func ApplyPoolHostConventions(name string, pcfg *pgxpool.Config, logs *zap.Logger)

ApplyPoolHostConventions applies name-based host conventions to a pool's *pgxpool.Config. Currently, the "ro" pool auto-rewrites an Aurora cluster writer endpoint to the corresponding reader endpoint (e.g., "*.cluster-xxx.<region>.rds.amazonaws.com" → "*.cluster-ro-xxx.<region>.rds.amazonaws.com"). This runs AFTER any user/framework-supplied Deriver so that the pool's host reflects the routing intent encoded by the pool name.

It is exported so that tests (and other layers that build *pgxpool.Config outside of newDB) can apply the same conventions explicitly.

func ApplyPoolHostOverride added in v0.0.223

func ApplyPoolHostOverride(name string, pcfg *pgxpool.Config, hosts map[string]string, logs *zap.Logger) bool

ApplyPoolHostOverride applies an explicit per-pool host override from the given map (typically Config.Hosts) to the given *pgxpool.Config. It returns true if an override was applied (so callers can skip name-based conventions).

It is exported so that tests (and other layers that build *pgxpool.Config outside of newDB) can apply the same override logic explicitly.

func Provide

func Provide[DBT any](drv Driver[DBT], mainPoolName string, derivedPoolNames ...string) fx.Option

Provide components as fx dependencies.

func ProvideDeriver added in v0.0.13

func ProvideDeriver(name string, deriver Deriver) fx.Option

ProvideDeriver is a short-hande function for providing a named deriver function that.

func TestProvide

func TestProvide[DBT any](
	tb testing.TB, mig pgtestdb.Migrator, drv Driver[DBT], mainPoolName string, derivedPoolNames ...string,
) fx.Option

TestProvide provides the package's components as an fx module with a setup more useful for testing.

Types

type Config

type Config struct {
	// MainDatabaseURL configures the database connection string for the main connection.
	MainDatabaseURL string `env:"MAIN_DATABASE_URL,required"`
	// IamAuth enables IAM authentication. When enabled, the password of every new
	// connection is replaced by the token returned by the provided IamTokenFunc.
	// The stdpgxiamfx sub-package provides an RDS implementation; see its docs for
	// how the signing region is derived from the host.
	IamAuth bool `env:"IAM_AUTH"`
	// Hosts overrides the host for named pools. Useful for Aurora Global Database
	// deployments where the secondary region's reader pool should connect to a
	// local regional reader endpoint while the writer pool stays on the primary
	// region's cluster endpoint.
	//
	// Format: comma-separated <pool>:<host> pairs.
	//
	//	STDPGX_HOSTS=ro:mycluster.cluster-ro-def.ap-southeast-1.rds.amazonaws.com
	//	STDPGX_HOSTS=ro:ro-host.example.com,rw:rw-host.example.com
	//
	// An override wins over name-based conventions (i.e., when a pool's host is
	// overridden, the cluster-ro auto-rewrite is skipped).
	Hosts map[string]string `env:"HOSTS"`
}

Config configures the module.

type Deriver added in v0.0.13

type Deriver func(logs *zap.Logger, base *pgxpool.Config) *pgxpool.Config

Deriver needs to be provided by the user of this module if derived pools are created.

type Driver added in v0.0.117

type Driver[DBT any] interface {
	NewPool(pcfg *pgxpool.Config) (DBT, error)
	Close(pool DBT) error
}

Driver abstracts turning a pgxpool config into connection pool and closing it.

func NewPgxV5Driver added in v0.0.117

func NewPgxV5Driver() Driver[*pgxpool.Pool]

NewPgxV5Driver inits a driver that uses the pgx v5 pooling.

func NewStandardDriver added in v0.0.117

func NewStandardDriver() Driver[*sql.DB]

NewStandardDriver inits a driver that uses the standard lib sql.DB as a pool.

type EndRole added in v0.0.120

type EndRole struct {
	Username string
	Password string
}

EndRole defines a type that can be provided if the role that actually connects to the database in tests is different from the migration role.

type FinalPgxPoolConfig added in v0.0.241

type FinalPgxPoolConfig struct {
	*pgxpool.Config
}

FinalPgxPoolConfig contains a pool's resolved configuration after its named deriver and host overrides or conventions have been applied. Provide exposes it under the same Fx name as the pool. Consumers must copy Config before use.

type IamTokenFunc added in v0.0.243

type IamTokenFunc func(ctx context.Context, cc *pgx.ConnConfig) (string, error)

IamTokenFunc returns the password to use for a new connection when IamAuth is enabled. It receives the live connection config so the token can be built for the connection's actual host, which may differ per (derived) pool.

type Params

type Params struct {
	fx.In

	Config   Config
	Logs     *zap.Logger
	IamToken IamTokenFunc `optional:"true"`
}

Params describe the main parameters for providing components.

type PgtestdbTestMigratorParams added in v0.0.58

type PgtestdbTestMigratorParams struct {
	fx.In
	Migrator pgtestdb.Migrator
	Role     *pgtestdb.Role `optional:"true"`
	EndRole  *EndRole       `optional:"true"`
}

PgtestdbTestMigratorParams holds the dependencies for creating a pgtestdb test migrator.

type Result

type Result struct {
	fx.Out

	PoolConfig *pgxpool.Config
}

Result describe the main components provided for this module.

func New

func New(params Params) (r Result, err error)

New is the main constructor. In this package it only provides the pool configuration used through out the package.

type TestMigrator added in v0.0.58

type TestMigrator interface {
	Migrate(
		tb testing.TB,
		cfg Config,
		pcfg *pgxpool.Config,
	) (*pgxpool.Config, error)
}

TestMigrator can be implemented and provided to migrate the database for tests.

func NewPgtestdbTestMigrator added in v0.0.58

func NewPgtestdbTestMigrator(params PgtestdbTestMigratorParams) TestMigrator

NewPgtestdbTestMigrator implements the TestMigrator using the pgtestdb library.

type TestMigratorFunc added in v0.0.58

type TestMigratorFunc func(
	tb testing.TB,
	cfg Config,
	pcfg *pgxpool.Config,
) (*pgxpool.Config, error)

TestMigratorFunc makes it easy to implement the migrator.

func (TestMigratorFunc) Migrate added in v0.0.58

func (f TestMigratorFunc) Migrate(
	tb testing.TB, cfg Config, pcfg *pgxpool.Config,
) (*pgxpool.Config, error)

Migrate implements the TestMigrator interface.

Directories

Path Synopsis
Package stdpgxiamfx provides RDS IAM authentication for stdpgxfx connection pools.
Package stdpgxiamfx provides RDS IAM authentication for stdpgxfx connection pools.
Package stdpgxtestfx provides stdpgxfx connection pools for tests, on a PostgreSQL server that is started for the tests, so they need no Docker Compose or other prior setup.
Package stdpgxtestfx provides stdpgxfx connection pools for tests, on a PostgreSQL server that is started for the tests, so they need no Docker Compose or other prior setup.

Jump to

Keyboard shortcuts

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