postgres

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 12 Imported by: 0

README

internal/infra/db/postgres

Pattern used

  • Owns connection pool, transaction boundary (TxManager contract), and store implementations.
  • Stores use generated static SQL and shared builder for dynamic SQL.
  • No PostgreSQL/driver types exposed outside this package.
  • pgtype.go converts PostgreSQL driver values at the infra boundary.
  • DDL: sqlc/schema.sql mirrors tables + indexes (sqlc + tuning reference); migrations apply in envs—keep them in sync when you change schema or indexes.

How to extend

  • Add store implementations under store/; static queries in the SQL layer, or builder for conditional shapes.
  • Use the pgtype helpers when mapping nullable timestamps or date-only values to domain-friendly Go types.
  • New migrations under migrations/; mirror structural/index changes in sqlc/schema.sql, then make sqlc-generate.

Documentation

Index

Constants

View Source
const (
	CodeForeignKeyViolation    = "23503"
	CodeUniqueViolation        = "23505"
	CodeCheckViolation         = "23514"
	CodeStringDataTruncation   = "22001"
	CodeNumericValueOutOfRange = "22003"
)

PostgreSQL error codes used to map driver errors to repository sentinels. See: https://www.postgresql.org/docs/current/errcodes-appendix.html

Variables

This section is empty.

Functions

func ClosePool

func ClosePool(ctx context.Context, pool *pgxpool.Pool, log logger.Logger)

func DateToTime added in v0.5.0

func DateToTime(d pgtype.Date) time.Time

DateToTime converts a non-null Postgres date to a UTC-midnight time.

func IsCheckViolation added in v0.4.0

func IsCheckViolation(err error) bool

IsCheckViolation reports whether err is a CHECK constraint failure.

func IsForeignKeyViolation added in v0.4.0

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a foreign_key_violation.

func IsNoRows added in v0.5.0

func IsNoRows(err error) bool

IsNoRows reports whether err is a pgx.ErrNoRows.

func IsUniqueViolation added in v0.4.0

func IsUniqueViolation(err error) bool

IsUniqueViolation reports whether err is a Postgres unique_violation (e.g. unique index).

func IsValueTooLong added in v0.4.0

func IsValueTooLong(err error) bool

IsValueTooLong reports whether err is a string_data_right_truncation (VARCHAR overflow).

func NewPool

func NewPool(ctx context.Context, cfg config.DBConfig, log logger.Logger) (*pgxpool.Pool, error)

func TimeToDate added in v0.5.0

func TimeToDate(t time.Time) pgtype.Date

TimeToDate wraps the calendar date portion of t into a valid pgtype.Date.

func TimeToTimestampz added in v0.5.0

func TimeToTimestampz(t time.Time) pgtype.Timestamptz

TimeToTimestampz wraps a time.Time into a valid pgtype.Timestamptz for a query parameter.

func TimestampzToTimePtr added in v0.5.0

func TimestampzToTimePtr(ts pgtype.Timestamptz) *time.Time

TimestampzToTimePtr converts a nullable pgtype.Timestamptz to *time.Time.

Types

type TxManager

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

func NewTxManager

func NewTxManager(pool *pgxpool.Pool, tracer observability.Tracer) *TxManager

func (*TxManager) WithTx

func (m *TxManager) WithTx(ctx context.Context, fn repodb.TxFunc) error

Directories

Path Synopsis
sqlc
gen

Jump to

Keyboard shortcuts

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