postgres

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package postgres is a platform module: a PostgreSQL connection pool with a health check, pool metrics and a clean shutdown.

The module writes no queries: sqlc generates the static ones, jet builds the dynamic ones.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InTx

func InTx(ctx context.Context, app *platform.App, fn func(tx pgx.Tx) error) error

InTx runs fn in a transaction on the pool from the container.

func Pool

func Pool(app *platform.App) *pgxpool.Pool

Pool returns the pool from the container.

func SQL added in v0.2.0

func SQL(app *platform.App) *sql.DB

SQL returns database/sql over the pool from the container. Dynamic queries built with jet run through it: stmt.QueryContext(ctx, postgres.SQL(app), &dest).

Types

type Config

type Config struct {
	URL             string
	MaxConns        int32
	MinConns        int32
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
	ConnectTimeout  time.Duration

	// Migrate applies pending migrations during startup. Turn it off when migrations
	// run as a separate step of the deploy.
	Migrate bool
}

Config holds module settings. Load fills it from the environment; the platform generator writes the Load call into the project's config.gen.go.

func Load

func Load(l *confx.Loader) Config

Load reads the module settings from environment variables.

type Module

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

Module implements platform.Module.

func New

func New(cfg Config, opts ...Option) *Module

New creates the module from ready settings.

func (*Module) Health

func (m *Module) Health(ctx context.Context) error

Health reports whether the database answers.

func (*Module) Init

func (m *Module) Init(ctx context.Context, app *platform.App) error

Init opens the pool, verifies the connection and puts the pool into the container, where other modules and project code pick it up through Pool(app).

func (*Module) Name

func (m *Module) Name() string

func (*Module) Stop

func (m *Module) Stop(context.Context) error

Stop closes the pool, waiting for busy connections to come back.

type Option added in v0.2.0

type Option func(*Module)

Option configures the module.

func WithMigrations added in v0.2.0

func WithMigrations(fsys fs.FS) Option

WithMigrations gives the module the migrations of the project. The generated wiring passes the embedded db/migrations directory.

Jump to

Keyboard shortcuts

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