Documentation
¶
Overview ¶
Package pgxext provides a thin PostgreSQL connection-pool wrapper built on pgx/v5, with helpers for configuration, migrations, and a generic repository query builder.
Index ¶
- func CollectOneRow[T any](rows pgx.Rows) (*T, error)
- func CollectRows[T any](rows pgx.Rows) ([]*T, error)
- func IsPostgresError(err error) (*pgconn.PgError, bool)
- type Config
- func (config *Config) WithAfterConnect(fn func(context.Context, *pgx.Conn) error) *Config
- func (config *Config) WithAfterRelease(fn func(*pgx.Conn) bool) *Config
- func (config *Config) WithApplicationName(name string) *Config
- func (config *Config) WithBeforeAcquire(fn func(context.Context, *pgx.Conn) bool) *Config
- func (config *Config) WithBeforeClose(fn func(*pgx.Conn)) *Config
- func (config *Config) WithBeforeConnect(fn func(context.Context, *pgx.ConnConfig) error) *Config
- func (config *Config) WithConnectTimeout(timeout time.Duration) *Config
- func (config *Config) WithDatabase(database string) *Config
- func (config *Config) WithDialFunc(fn pgconn.DialFunc) *Config
- func (config *Config) WithHealthCheckPeriod(d time.Duration) *Config
- func (config *Config) WithHost(host string) *Config
- func (config *Config) WithLookupFunc(fn pgconn.LookupFunc) *Config
- func (config *Config) WithMaxConnIdleTime(d time.Duration) *Config
- func (config *Config) WithMaxConnLifetime(d time.Duration) *Config
- func (config *Config) WithMaxConns(n int32) *Config
- func (config *Config) WithMinConns(n int32) *Config
- func (config *Config) WithOnNotice(fn pgconn.NoticeHandler) *Config
- func (config *Config) WithOnNotification(fn pgconn.NotificationHandler) *Config
- func (config *Config) WithOnPgError(fn pgconn.PgErrorHandler) *Config
- func (config *Config) WithPassword(password string) *Config
- func (config *Config) WithPort(port uint16) *Config
- func (config *Config) WithPrepareConn(fn func(context.Context, *pgx.Conn) (bool, error)) *Config
- func (config *Config) WithSSLClientCert(certFile, keyFile, password string) (*Config, error)
- func (config *Config) WithSSLMode(mode string) *Config
- func (config *Config) WithSSLRootCert(rootCertFile string) (*Config, error)
- func (config *Config) WithSearchPath(path string) *Config
- func (config *Config) WithShouldPing(fn func(context.Context, pgxpool.ShouldPingParams) bool) *Config
- func (config *Config) WithTimezone(tz string) *Config
- func (config *Config) WithURL(rawURL string) (*Config, error)
- func (config *Config) WithUser(user string) *Config
- func (config *Config) WithValidateConnect(fn pgconn.ValidateConnectFunc) *Config
- type DataSource
- func (ds *DataSource) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)
- func (ds *DataSource) NewBatch() *pgx.Batch
- func (ds *DataSource) NewCustomTransaction(ctx context.Context, options pgx.TxOptions) (pgx.Tx, error)
- func (ds *DataSource) NewTransaction(ctx context.Context) (pgx.Tx, error)
- func (ds *DataSource) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
- func (ds *DataSource) QueryRow(ctx context.Context, sql string, args ...any) (pgx.Row, error)
- func (ds *DataSource) SendBatch(ctx context.Context, batch *pgx.Batch) (pgx.BatchResults, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectOneRow ¶
CollectOneRow scans the first row of rows into *T using db-tag field mapping. Returns nil without an error if no rows are found.
func CollectRows ¶
CollectRows scans all rows into []*T using db-tag field mapping. Returns nil without an error if no rows are found.
Types ¶
type Config ¶
Config extends pgxpool.Config with session parameters not directly representable as fields in the underlying pgxpool/pgconn structs.
func NewConfig ¶
func NewConfig() *Config
NewConfig returns a Config initialised from an empty DSN via pgxpool.ParseConfig.
func (*Config) WithAfterConnect ¶
WithAfterConnect sets a hook called after a connection is established, before it is added to the pool.
func (*Config) WithAfterRelease ¶
WithAfterRelease sets a hook called after a connection is released but before it is returned to the pool. Return false to destroy the connection.
func (*Config) WithApplicationName ¶
WithApplicationName sets the application_name runtime parameter.
func (*Config) WithBeforeAcquire ¶
WithBeforeAcquire sets a hook called before a connection is acquired from the pool. Deprecated: prefer WithPrepareConn.
func (*Config) WithBeforeClose ¶
WithBeforeClose sets a hook called just before a connection is closed and removed from the pool.
func (*Config) WithBeforeConnect ¶
WithBeforeConnect sets a hook called before each new connection is dialed. The pgx.ConnConfig copy passed to fn is not shared with any open connection.
func (*Config) WithConnectTimeout ¶
WithConnectTimeout sets the maximum wait for a connection to be established.
func (*Config) WithDatabase ¶
WithDatabase sets the database name.
func (*Config) WithDialFunc ¶
WithDialFunc overrides the function used to establish the underlying network connection (e.g. to use a custom dialer or proxy).
func (*Config) WithHealthCheckPeriod ¶
WithHealthCheckPeriod sets the interval between health checks on idle connections. Maps to pgxpool.Config.HealthCheckPeriod.
func (*Config) WithLookupFunc ¶
func (config *Config) WithLookupFunc(fn pgconn.LookupFunc) *Config
WithLookupFunc overrides the DNS resolver used during connection.
func (*Config) WithMaxConnIdleTime ¶
WithMaxConnIdleTime sets the maximum idle time before a connection is closed. Maps to pgxpool.Config.MaxConnIdleTime.
func (*Config) WithMaxConnLifetime ¶
WithMaxConnLifetime sets the maximum lifetime of a pooled connection. Maps to pgxpool.Config.MaxConnLifetime.
func (*Config) WithMaxConns ¶
WithMaxConns sets the maximum number of connections in the pool. Maps to pgxpool.Config.MaxConns.
func (*Config) WithMinConns ¶
WithMinConns sets the minimum number of connections in the pool. Maps to pgxpool.Config.MinConns.
func (*Config) WithOnNotice ¶
func (config *Config) WithOnNotice(fn pgconn.NoticeHandler) *Config
WithOnNotice sets a callback invoked whenever the server sends a notice.
func (*Config) WithOnNotification ¶
func (config *Config) WithOnNotification(fn pgconn.NotificationHandler) *Config
WithOnNotification sets a callback invoked on LISTEN/NOTIFY notifications.
func (*Config) WithOnPgError ¶
func (config *Config) WithOnPgError(fn pgconn.PgErrorHandler) *Config
WithOnPgError sets a callback invoked when the server returns a Postgres error. Return false to close the connection; return true to keep it open.
func (*Config) WithPassword ¶
WithPassword sets the database password.
func (*Config) WithPrepareConn ¶
WithPrepareConn sets a hook called before a connection is acquired from the pool. Return (true, nil) to allow, (false, nil) to destroy and retry on a new connection, or (_, err) to surface an error to the caller.
func (*Config) WithSSLClientCert ¶
WithSSLClientCert loads the client certificate and private key from certFile and keyFile, building a ready-to-use tls.Certificate inside ConnConfig.TLSConfig. Pass a non-empty password if the private key is PEM-encrypted; otherwise pass "".
func (*Config) WithSSLMode ¶
WithSSLMode configures ConnConfig.TLSConfig according to the PostgreSQL sslmode:
- "disable" — TLS is disabled entirely.
- "require" — TLS is required; server certificate is not verified.
- "verify-ca" — TLS is required; certificate chain is verified against RootCAs but the server hostname is not checked.
- "verify-full" — TLS is required; both certificate chain and hostname are verified.
func (*Config) WithSSLRootCert ¶
WithSSLRootCert loads the PEM-encoded CA certificate from rootCertFile and registers it as the trusted root for TLS verification.
func (*Config) WithSearchPath ¶
WithSearchPath sets the search_path runtime parameter.
func (*Config) WithShouldPing ¶
func (config *Config) WithShouldPing(fn func(context.Context, pgxpool.ShouldPingParams) bool) *Config
WithShouldPing sets a hook that decides whether an acquired connection should be pinged to check liveness before use.
func (*Config) WithTimezone ¶
WithTimezone sets the TimeZone runtime parameter.
func (*Config) WithURL ¶
WithURL parses a PostgreSQL connection URL and fills all Config fields. Delegates entirely to pgxpool.ParseConfig, which handles all standard params, SSL (builds ConnConfig.TLSConfig directly), pool_* params, and runtime params.
Example:
postgres://alice:secret@localhost:5432/mydb?sslmode=verify-full&pool_max_conns=10
func (*Config) WithValidateConnect ¶
func (config *Config) WithValidateConnect(fn pgconn.ValidateConnectFunc) *Config
WithValidateConnect sets a hook called after authentication succeeds. Return an error to reject the connection and try the next fallback.
type DataSource ¶
DataSource wraps a pgxpool.Pool and exposes the query surface used by the rest of the library. A nil context is treated as context.Background().
func NewDataSource ¶
func NewDataSource(ctx context.Context, config *Config) (ds *DataSource, err error)
NewDataSource opens a connection pool using the provided Config.
func (*DataSource) Exec ¶
func (ds *DataSource) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)
Exec runs a SQL statement that does not return rows.
func (*DataSource) NewBatch ¶
func (ds *DataSource) NewBatch() *pgx.Batch
NewBatch returns an empty pgx.Batch ready to queue statements.
func (*DataSource) NewCustomTransaction ¶
func (ds *DataSource) NewCustomTransaction(ctx context.Context, options pgx.TxOptions) (pgx.Tx, error)
NewCustomTransaction begins a transaction with the specified isolation level and access mode.
func (*DataSource) NewTransaction ¶
NewTransaction begins a new database transaction with default options.
func (*DataSource) SendBatch ¶
func (ds *DataSource) SendBatch(ctx context.Context, batch *pgx.Batch) (pgx.BatchResults, error)
SendBatch dispatches all queued statements in batch as a single round-trip.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package migration provides a simple transactional SQL migration runner backed by a pgxext.DataSource.
|
Package migration provides a simple transactional SQL migration runner backed by a pgxext.DataSource. |
|
Package repository provides a generic, type-safe query builder for PostgreSQL backed by pgxext.DataSource.
|
Package repository provides a generic, type-safe query builder for PostgreSQL backed by pgxext.DataSource. |