Documentation
¶
Overview ¶
Package db provides support to access a PostgreSQL database.
Index ¶
- Variables
- func ConnString(cfg Config) string
- func NamedExec(ctx context.Context, log *slog.Logger, db DB, query string, data any) error
- func NamedQuerySlice[T any](ctx context.Context, log *slog.Logger, db DB, query string, data any) ([]T, error)
- func NamedQueryStruct[T any](ctx context.Context, log *slog.Logger, db DB, query string, data any) (T, error)
- func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)
- func OpenConnString(ctx context.Context, connString string) (*pgxpool.Pool, error)
- func StatusCheck(ctx context.Context, db *pgxpool.Pool) error
- type Config
- type DB
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 ¶
ConnString creates a postgres connection string with config values.
func NamedExec ¶
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 OpenConnString ¶
OpenConnString open a database connection using the connString.
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.
Click to show internal directories.
Click to hide internal directories.