postgres

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyAll

func CopyAll[T any](
	connection Connection,
	ctx context.Context,
	tableName string,
	columnNames []string,
	input []T,
	outputMapper SliceMapper[T],
) (errorResult error)

func CopyAny

func CopyAny[T any](
	connection Connection,
	ctx context.Context,
	tableName string,
	columnNames []string,
	input []T,
	outputMapper SliceMapper[T],
) (int64, error)

Types

type Batch

type Batch interface {
	Exec(handler CommandTagHandler, sql string, args ...any)
	Query(collector RowCollector, handler CommandTagHandler, sql string, args ...any)
	QueryRow(collector RowCollector, sql string, args ...any)
	Send() error
	// contains filtered or unexported methods
}

type CommandTag

type CommandTag interface {
	String() string
	RowsAffected() int64
	Insert() bool
	Update() bool
	Delete() bool
	Select() bool
}

type CommandTagHandler

type CommandTagHandler func(ctx context.Context, tag CommandTag) error

type Config

type Config struct {
	Address          string `env:"POSTGRES_ADDRESS" validate:"required,hostname_port"`
	Username         string `env:"POSTGRES_USER" validate:"required"`
	Password         string `env:"POSTGRES_PASSWORD" validate:"required"`
	DatabaseName     string `env:"POSTGRES_DB_NAME" validate:"required"`
	LogLevel         string `env:"POSTGRES_LOG_LEVEL" validate:"oneof=trace debug info warn error none" default:"info"`
	MigrationTimeout uint   `env:"POSTGRES_MIGRATION_TIMEOUT" default:"30"`
}

Config defines the options that are used when connecting to a PostgreSQL instance

type Connection

type Connection interface {
	// Begin starts a transaction.
	Begin(ctx context.Context) (Transaction, error)

	// Batch creates a batch of commands.
	Batch(ctx context.Context) Batch

	// Exec execute the command.
	Exec(ctx context.Context, sql string, args ...any) (CommandTag, error)

	// Query scan the result rows by calling the collector repeatedly.
	Query(ctx context.Context, collector RowCollector, sql string, args ...any) (CommandTag, error)

	// QueryRow expects the result is at most one row. Returns nil [RowScanner] if
	// the result is empty.
	QueryRow(ctx context.Context, sql string, args ...any) (RowScanner, error)
	// contains filtered or unexported methods
}

type Database

type Database interface {
	Connection
	// contains filtered or unexported methods
}

func New

func New(
	lifecycle fx.Lifecycle,
	config *Config,
	plan MigrationPlan,
) (Database, error)

New connects to the PostgreSQL database that are specified in the configuration, migrates the database if required.

type MigrationPlan

type MigrationPlan []MigrationRecord

type MigrationRecord

type MigrationRecord struct {
	Id  string
	Sql []string
}

type RowCollector

type RowCollector func(ctx context.Context, row RowScanner) error

type RowScanner

type RowScanner func(destination ...any) error

type SliceMapper

type SliceMapper[T any] func(output []any, input T)

type Transaction

type Transaction interface {
	Connection

	// Finalize safely concludes a database transaction by either committing or
	// rolling back. It rolls back the transaction if any of the following conditions
	// are met: the input error is not null, a panic occurred earlier in execution,
	// the provided context has an error (canceled or expired), or the commit
	// operation itself fails. This ensures reliable and consistent transaction
	// handling.
	Finalize(ctx context.Context, errorResult *error)
}

Jump to

Keyboard shortcuts

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