pgx v5-native PostgreSQL client with launcher lifecycle, health check, unit-of-work via context injection, and structured error mapping. What's included: - Executor / Tx / Client / Component interfaces using pgx native types (pgconn.CommandTag, pgx.Rows, pgx.Row) - New(logger, cfg) constructor; pgxpool initialised in OnInit - Config struct with env-tag support for all pool tuning parameters - UnitOfWork via context injection; GetExecutor(ctx) returns active Tx or pool - HandleError mapping pgerrcode constants to xerrors codes (AlreadyExists, InvalidInput, NotFound, Internal) - health.Checkable at LevelCritical; HealthCheck delegates to pgxpool.Ping Tested-via: todo-api POC integration Reviewed-against: docs/adr/
3.0 KiB
3.0 KiB
Changelog
All notable changes to this module will be documented in this file.
The format is based on Keep a Changelog, and this module adheres to Semantic Versioning.
0.9.0 - 2026-03-18
Added
Executorinterface:Exec,Query,QueryRowusing native pgx types (pgconn.CommandTag,pgx.Rows,pgx.Row).Txinterface: embedsExecutorand addsCommit(ctx context.Context) errorandRollback(ctx context.Context) error.Clientinterface:GetExecutor(ctx context.Context) Executor,Begin(ctx context.Context) (Tx, error),Ping(ctx context.Context) error,HandleError(err error) error.Componentinterface: composeslauncher.Component,health.Checkable, andClientinto a single injectable dependency.UnitOfWorkinterface:Do(ctx context.Context, fn func(ctx context.Context) error) error.Configstruct: fieldsHost,Port,User,Password,Name,SSLMode,Timezone,MaxConns,MinConns,MaxConnLifetime,MaxConnIdleTime,HealthCheckPeriod; all settable via environment variables withenvstruct tags and sensible defaults.Config.DSN() string: constructs apostgres://URL including SSL mode and timezone query parameters.New(logger logz.Logger, cfg Config) Component: returns apgxpool-backed component; pool is created lazily inOnInit.- Lifecycle hooks:
OnInitparses config and creates the connection pool with a 30-second timeout;OnStartpings the database with a 5-second timeout;OnStopcloses the pool gracefully. health.Checkableimplementation:HealthCheckdelegates toPing;Name()returns"postgres";Priority()returnshealth.LevelCritical.NewUnitOfWork(logger logz.Logger, client Client) UnitOfWork: wraps aClientto provide transactionalDosemantics; rolls back and logs on error, commits on success.HandleError(err error) error(package-level function): maps*pgconn.PgErrorcodes to xerrors —UniqueViolation→ErrAlreadyExists,ForeignKeyViolation→ErrInvalidInput,CheckViolation→ErrInvalidInput;pgx.ErrNoRows→ErrNotFound; all other errors →ErrInternal.- Transaction context injection: the active
pgx.Txis stored under an unexportedctxTxKey{}context key;GetExecutorreturns the transaction when found, otherwise returns the pool. - All pool reads guarded by
sync.RWMutexfor safe concurrent access.
Design Notes
- All interfaces use pgx-native types (
pgconn.CommandTag,pgx.Rows,pgx.Row) directly; there is nodatabase/sqladapter. This is intentional and incompatible with themysqlmodule by design. UnitOfWork.Doinjects the transaction into the context so repositories can callGetExecutor(ctx)transparently without knowing whether a transaction is active.- PostgreSQL error codes are matched via
pgerrcodeconstants anderrors.As, never by parsing error message strings.