Documentation
¶
Overview ¶
Package postgresql provides a github.com/sunkek/samsara-compatible PostgreSQL component backed by pgx/v5 connection pooling.
Usage ¶
comp := postgresql.New(postgresql.Config{
Host: "localhost",
Port: 5432,
Name: "mydb",
User: "myuser",
Pass: "secret",
})
sup.Add(comp, samsara.WithTier(samsara.TierCritical))
Domain adapters receive *Component (or the DB interface) and use its Select, Get, Exec, and transaction helpers — they never import pgx or pgxscan directly.
Index ¶
- Variables
- type Component
- func (c *Component) BeginTx(ctx context.Context, opts pgx.TxOptions) (pgx.Tx, error)
- func (c *Component) CommitTx(ctx context.Context, tx TxFinaliser, inErr error) error
- func (c *Component) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
- func (c *Component) Get(ctx context.Context, dst any, sql string, args ...any) error
- func (c *Component) Health(ctx context.Context) error
- func (c *Component) Name() string
- func (c *Component) Select(ctx context.Context, dst any, sql string, args ...any) error
- func (c *Component) Start(ctx context.Context, ready func()) error
- func (c *Component) Stop(ctx context.Context) error
- type Config
- type DB
- type Logger
- type Option
- type TxFinaliser
Constants ¶
This section is empty.
Variables ¶
var ErrNoRows = pgx.ErrNoRows
ErrNoRows is returned by [Select] and [Get] when no rows match the query. Use errors.Is(err, postgresql.ErrNoRows) to check.
Functions ¶
This section is empty.
Types ¶
type Component ¶
type Component struct {
// contains filtered or unexported fields
}
Component is a samsara-compatible PostgreSQL component. Obtain one with New; register it with a samsara supervisor.
Domain adapters should accept DB rather than *Component to keep their tests independent of a real database.
func New ¶
New creates a Component from the supplied config. The component is not connected until Component.Start is called.
func (*Component) CommitTx ¶
CommitTx commits tx when inErr is nil, and rolls back when inErr is non-nil. Both errors are preserved in the returned chain so callers can use errors.Is.
func (*Component) Exec ¶
Exec executes sql without scanning rows. Useful for INSERT, UPDATE, DELETE, DDL.
func (*Component) Get ¶
Get executes sql and scans the first result row into dst. dst must be a pointer to a struct or scalar type.
func (*Component) Health ¶
Health implements samsara.HealthChecker. Returns a non-nil error if the pool cannot reach the database.
func (*Component) Select ¶
Select executes sql and scans all result rows into dst. dst must be a pointer to a slice of structs or scalars.
func (*Component) Start ¶
Start creates the connection pool, pings the server to confirm reachability, calls ready() to unblock the supervisor, then blocks until Stop or ctx cancellation.
Start is safe to call multiple times across restarts; each call allocates a fresh stopCh so the previous Stop signal does not bleed into the new run.
type Config ¶
type Config struct {
// Host is the database server hostname or IP. Defaults to "localhost".
Host string
// Port is the database server port. Defaults to 5432.
Port int
// Name is the database name. Defaults to "postgres".
Name string
// User is the database user. Defaults to "postgres".
User string
// Pass is the database password. Special characters are safely encoded.
Pass string
// SSLMode controls the SSL negotiation mode (disable, require, verify-ca,
// verify-full). Defaults to "disable".
SSLMode string
// URI overrides all individual fields when non-empty.
// Must be a valid libpq connection string or pgx DSN.
URI string
// ConnectTimeout is the deadline for the initial connection + ping during
// Start. Defaults to 10 s.
ConnectTimeout time.Duration
// MaxConns caps the pool size. 0 means pgx default (min(4, GOMAXPROCS)).
MaxConns int32
// MinConns keeps this many connections alive even when idle. 0 means none.
MinConns int32
}
Config holds all connection parameters for the PostgreSQL component. Supply individual fields or a preformatted [Config.URI] to override them all.
type DB ¶
type DB interface {
// Select executes sql and scans all result rows into dst (a pointer to a
// slice). Returns [ErrNoRows] if the result set is empty.
Select(ctx context.Context, dst any, sql string, args ...any) error
// Get executes sql and scans the first result row into dst (a pointer to a
// struct or scalar). Returns [ErrNoRows] if no row was found.
Get(ctx context.Context, dst any, sql string, args ...any) error
// Exec executes sql and returns the command tag. Use for INSERT/UPDATE/DELETE
// where you don't need to scan rows.
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
// BeginTx starts a transaction with the given options.
BeginTx(ctx context.Context, opts pgx.TxOptions) (pgx.Tx, error)
// CommitTx commits tx if inErr is nil, and rolls back if inErr is non-nil.
// Use in a defer to guarantee finalisation:
//
// tx, err := db.BeginTx(ctx, pgx.TxOptions{})
// if err != nil { return err }
// defer func() { err = db.CommitTx(ctx, tx, err) }()
CommitTx(ctx context.Context, tx TxFinaliser, inErr error) error
}
DB is the interface that domain adapters should depend on. *Component satisfies it; depend on DB rather than *Component to keep adapters testable.
type UserRepo struct { db postgresql.DB }
type Logger ¶
Logger is satisfied by log/slog.Logger and most structured loggers.
type Option ¶
type Option func(*Component)
Option configures a Component.
func WithLogger ¶
WithLogger attaches a structured logger to the component. log/slog.Logger satisfies Logger directly.
func WithName ¶
WithName overrides the component name returned by Component.Name. Useful when registering multiple PostgreSQL components with the same supervisor (e.g. a primary and a read replica).