db

package
v0.0.0-...-ffc2b38 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package db provides support to access a PostgreSQL database.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDBNotFound        = sql.ErrNoRows
	ErrDBDuplicatedEntry = errors.New("duplicated entry")
	ErrUndefinedTable    = errors.New("undefined table")
)

Set of error variables for CRUD operations.

Functions

func ConnString

func ConnString(cfg Config) string

ConnString creates a postgres connection string with config values.

func NamedExec

func NamedExec(ctx context.Context, log *slog.Logger, db DB, query string, data any) error

NamedExec is a helper function to execute a CUD operation with logging and tracing where field replacement is necessary.

func NamedQuerySlice

func NamedQuerySlice[T any](ctx context.Context, log *slog.Logger, db DB, query string, data any) ([]T, error)

NamedQuerySlice is a helper function for executing queries that return a collection of data to be unmarshalled into a slice where field replacement is necessary.

func NamedQueryStruct

func NamedQueryStruct[T any](ctx context.Context, log *slog.Logger, db DB, query string, data any) (T, error)

func Open

func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)

Open knows how to open a database connection based on the configuration.

func OpenConnString

func OpenConnString(ctx context.Context, connString string) (*pgxpool.Pool, error)

OpenConnString open a database connection using the connString.

func StatusCheck

func StatusCheck(ctx context.Context, db *pgxpool.Pool) error

StatusCheck returns nil if it can successfully talk to the database. It returns a non-nil error otherwise.

Types

type Config

type Config struct {
	User       string
	Password   string
	Host       string
	Name       string
	Schema     string
	DisableTLS bool
}

Config is the required properties to use the database.

type DB

type DB interface {
	Begin(ctx context.Context) (pgx.Tx, error)
	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

DB is an interface used to support both *pgxpool.Pool and pgx.Tx.

Jump to

Keyboard shortcuts

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