sqlx

package
v0.0.0-...-d12aba9 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package sqlx maps database errors from database/sql and pgx into HTTP-friendly herodot errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapDBErrorHTTP

func MapDBErrorHTTP(err error, resourceName string) *herodot.DefaultError

MapDBErrorHTTP maps a database/sql error into a herodot error suitable for an HTTP response, translating sql.ErrNoRows into a not-found error and returning nil for a nil err. resourceName is used to build a human-readable reason (e.g. "user not found").

func MapPgxErrorHTTP

func MapPgxErrorHTTP(err error, resourceName string) *herodot.DefaultError

MapPgxErrorHTTP maps a pgx error into a herodot error suitable for an HTTP response, translating pgx.ErrNoRows into a not-found error and returning nil for a nil err. resourceName is used to build a human-readable reason (e.g. "user not found").

func NewMigratePgx

func NewMigratePgx(ctx context.Context, databaseURL string, migrationsFS fs.FS, logger *slog.Logger) (*migrate.Migrate, func(), error)

NewMigratePgx opens a PostgreSQL-backed *migrate.Migrate instance, connecting via a pgxpool.Pool built from databaseURL and reading migration files from the "migrations" directory of migrationsFS.

Callers own their migration files: embed them in the calling package, e.g.

//go:embed migrations/*.sql
var migrationsFS embed.FS

m, cleanup, err := sqlx.NewMigratePgx(ctx, dbURL, migrationsFS, logger)
if err != nil {
	return err
}
defer cleanup()

On success the returned cleanup func closes the migrate instance and the underlying connection pool; it must be called exactly once, typically via defer, regardless of whether migrations are actually run. On error, all resources opened so far are closed internally and cleanup is nil.

func NewMigrateSqlite

func NewMigrateSqlite(ctx context.Context, databaseURL string, migrationsFS fs.FS, dir string, logger *slog.Logger) (*migrate.Migrate, func(), error)

NewMigrateSqlite opens a SQLite-backed *migrate.Migrate instance, connecting via database/sql (using the pure-Go modernc.org/sqlite driver) to databaseURL and reading migration files from dir in migrationsFS.

Callers own their migration files: embed them in the calling package, e.g.

//go:embed migrations/*.sql
var migrationsFS embed.FS

m, cleanup, err := sqlx.NewMigrateSqlite(ctx, dbURL, migrationsFS, "migrations", logger)
if err != nil {
	return err
}
defer cleanup()

On success the returned cleanup func closes the migrate instance, which in turn closes the underlying *sql.DB; it must be called exactly once, typically via defer, regardless of whether migrations are actually run. On error, all resources opened so far are closed internally and cleanup is nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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