database

package
v0.0.0-...-bb59e5b Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Closes the database connection.

func Do

func Do(ctx context.Context, sql string, args ...any) error

Runs a query which does not return a result.

func Init

func Init(config *koanf.Koanf) error

Initalizes a connection to a PostgreSQL database.

func Multiple

func Multiple[T any](ctx context.Context, sql string, args ...any) ([]T, error)

Runs a query which returns multiple rows and collects them in a slice of type []T using pgx.RowToStructByName. If no records were returned by the query, an empty slice is returned, along with the pgx error.

func RowToAddrOfStructByName

func RowToAddrOfStructByName[T any](row pgx.CollectableRow) (*T, error)

RowToAddrOfStructByPos returns the address of a T scanned from row. T must be a struct. T must have the same number of named public fields as row has fields. The row and T fields will by matched by name. The match is case-insensitive. The database column name can be overridden with a "db" struct tag. If the "db" struct tag is "-" then the field will be ignored. Handles embedded structs, if they specify "embedded" in the "db" tag.

func RowToStructByName

func RowToStructByName[T any](row pgx.CollectableRow) (T, error)

RowToStructByName returns a T scanned from row. T must be a struct. T must have the same number of named public fields as row has fields. The row and T fields will by matched by name. The match is case-insensitive. The database column name can be overridden with a "db" struct tag. If the "db" struct tag is "-" then the field will be ignored. Handles embedded structs, if they specify "embedded" in the "db" tag.

func Single

func Single[T any](ctx context.Context, sql string, args ...any) (*T, error)

Runs a query which returns a single row and converts it to T using pgx.RowToAddrOfStructByName. If no records were returned by the query, a nil pointer to T is returned, along with the pgx error.

Types

type Postgres

type Postgres struct {
	squirrel.StatementBuilderType
	*pgxpool.Pool
	// contains filtered or unexported fields
}

Type Postgres contains a database connection pool and a query builder.

func Instance

func Instance() *Postgres

Returns a reference to the internal database connection and query builder.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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