package dbx import ( "context" "github.com/jackc/pgx/v5" ) // Collect executes a read query and collects all rows into a slice of T // using pgx.RowToStructByName. T must be a struct with db tags matching column names. func Collect[T any](ctx context.Context, c *Cluster, sql string, args ...any) ([]T, error) { rows, err := c.ReadQuery(ctx, sql, args...) if err != nil { return nil, err } return pgx.CollectRows(rows, pgx.RowToStructByName[T]) } // CollectOne executes a read query and collects exactly one row into T. // Returns pgx.ErrNoRows if no rows are returned. func CollectOne[T any](ctx context.Context, c *Cluster, sql string, args ...any) (T, error) { rows, err := c.ReadQuery(ctx, sql, args...) if err != nil { var zero T return zero, err } return pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[T]) }