datastore

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Overview

Package datastore owns the Postgres pool, transactions, and health checks.

Index

Constants

View Source
const (
	PgSearchPath = "public,internal,reference"
	PgTimezone   = "UTC"
)

Variables

This section is empty.

Functions

func Deref

func Deref[T any](p *T) T

Deref returns the pointed value or its zero value when p is nil.

func MapErr

func MapErr(err error, store string, notFound, duplicate error) error

MapErr maps common PostgreSQL errors to module sentinels.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to v.

func TextPtr

func TextPtr(t pgtype.Text) *string

TextPtr converts invalid text to nil.

func TimePtr

func TimePtr(t pgtype.Timestamptz) *time.Time

TimePtr converts an invalid timestamptz to nil.

func UserUUID

func UserUUID(raw string) string

UserUUID converts a typed ID string (or bare UUID) to the UUID column form. Stores decoupled from the identity packages use this for user_id columns.

func Wrap

func Wrap(store, op string, err error) error

Wrap adds store and operation context without changing the error type.

Types

type Backend

type Backend interface {
	HealthCheck(ctx context.Context) error
	io.Closer
}

Backend provides health checks and shutdown.

type ConnectionInfo

type ConnectionInfo struct {
	Host     string
	Port     uint16
	Database string
	User     string

	ServerVersion    string
	ServerVersionNum int64

	Latency time.Duration

	Stats *pgxpool.Stat
}

ConnectionInfo contains connection details and pool stats.

type Executor

type Executor interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

Executor is the query surface for a pool or transaction.

type Options

type Options struct {
	// DSN is the connection string (postgresql:// or postgres://).
	DSN string

	// MaxConnections caps pool size; 0 keeps pgx/DSN default.
	MaxConnections int32

	// MinConnections keeps warm idle; 0 keeps pgx/DSN default.
	MinConnections int32

	// MaxConnLifetime bounds reuse; 0 keeps pgx/DSN default.
	MaxConnLifetime time.Duration

	// MaxConnIdleTime closes idle conns; 0 keeps pgx/DSN default.
	MaxConnIdleTime time.Duration
}

Options configures the Postgres backend. Explicit pool values override DSN values.

type Postgres

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

Postgres is a pgx/v5 pool-backed Store.

func New

func New(ctx context.Context, opts Options) (*Postgres, error)

New builds the pool and verifies the connection.

func (*Postgres) Close

func (p *Postgres) Close() error

Close closes the pool.

func (*Postgres) Exec

func (p *Postgres) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

Exec runs a statement outside a transaction.

func (*Postgres) HealthCheck

func (p *Postgres) HealthCheck(ctx context.Context) error

HealthCheck reports whether the pool can reach the server.

func (*Postgres) Pool

func (p *Postgres) Pool() *pgxpool.Pool

Pool exposes the pool to infrastructure that needs direct access.

func (*Postgres) Query

func (p *Postgres) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query runs a query outside a transaction.

func (*Postgres) QueryRow

func (p *Postgres) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow runs a single-row query outside a transaction.

func (*Postgres) TestConnection

func (p *Postgres) TestConnection(ctx context.Context) (*ConnectionInfo, error)

TestConnection queries the server and returns connection details.

func (*Postgres) WithTx

func (p *Postgres) WithTx(ctx context.Context, fn func(Executor) error) error

WithTx runs fn in a transaction and rolls back on error or panic.

type Store

type Store interface {
	Backend
	Executor

	WithTx(ctx context.Context, fn func(Executor) error) error
}

Store combines Backend, Executor, and transaction support.

Jump to

Keyboard shortcuts

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