package postgres

import "github.com/go-arrower/arrower/postgres"

Index

Constants

const CtxTX ctx2.CTXKey = "arrower.tx"

CtxTX contains a database transaction, only if set by e.g. a middleware.

Variables

var (
	ErrConnectionFailed = errors.New("connection failed")
	ErrMigrationFailed  = errors.New("migration failed")
)
var ArrowerDefaultMigrations embed.FS
var ErrQueryFailed = errors.New("query failed")

Types

type BaseRepository

type BaseRepository[T interface{ WithTx(tx pgx.Tx) T }] struct {
	// contains filtered or unexported fields
}

BaseRepository can be used in repository implementations. Because sqlc generates an own Queries struct for each corresponding models/db.go file, a generic approach is used, so each repo can create a BaseRepository with the fitting *models.Queries type.

func NewPostgresBaseRepository

func NewPostgresBaseRepository[T interface{ WithTx(tx pgx.Tx) T }](queries T) BaseRepository[T]

func (BaseRepository[T]) Conn

func (repo BaseRepository[T]) Conn() T

Conn returns the models.Queries using the underlying db connection.

func (BaseRepository[T]) TX

func (repo BaseRepository[T]) TX(ctx context.Context) T

TX wraps the models.Queries into the transaction in ctx. If no transaction is present in the given context, it returns nil.

func (BaseRepository[T]) TxOrConn

func (repo BaseRepository[T]) TxOrConn(ctx context.Context) T

TxOrConn wraps the models.Queries into the transaction in ctx. If no transaction is in the context, it falls back to the raw Queries struct.

type Config

type Config struct {
	Migrations fs.FS
	User       string
	Password   string
	Database   string
	SSLMode    string
	Host       string
	Port       int
	MaxConns   int
}

Config holds all values used to configure and connect to a postgres database.

type Handler

type Handler struct {
	PGx    *pgxpool.Pool
	DB     *sql.DB // keep a sql.DB connection around for migration & integration tests, e.g. setting up test fixtures.
	Config Config
}

func Connect

func Connect(ctx context.Context, pgConf Config, tracerProvider trace.TracerProvider) (*Handler, error)

Connect connects to a PostgreSQL database.

func ConnectAndMigrate

func ConnectAndMigrate(ctx context.Context, conf Config, tracerProvider trace.TracerProvider) (*Handler, error)

ConnectAndMigrate connects to a PostgreSQL database and runs all migrations to ensure that the schema is on the latest version.

func (Handler) Shutdown

func (h Handler) Shutdown(_ context.Context) error

Shutdown waits & closes all connections to PostgreSQL.

Source Files

base.repository.go pgx-adapter.infrastructure.go postgres.infrastructure.go

Directories

PathSynopsis
postgres/models
Version
v0.0.0-20250311203644-ab26c1152cb4 (latest)
Published
Mar 11, 2025
Platform
linux/amd64
Imports
18 packages
Last checked
1 week ago

Tools for package owners.