Documentation
¶
Overview ¶
Package pgxx contains helper functions for pgx
Index ¶
- Constants
- Variables
- func All[T any](db DB, v *[]T) error
- func ClearCache()
- func CollectRows[T any](rows pgx.Rows) (items []T, err error)
- func CollectStrings(rows pgx.Rows) ([]string, error)
- func Exec(db DB, sql string, args ...any) error
- func First(db DB, v any) error
- func Insert(db DB, v any) error
- func Many[T any](db DB, v *[]T, sql string, args ...any) error
- func One(db DB, v any, sql string, args ...any) error
- func RunScript(conn DB, script string, tdata any) error
- func SetScriptFS(dir fs.FS)
- func Update(db DB, v any) error
- type Columns
- type DB
- type FK
- type Field
- type Fields
- type ID
- type PaginationOptions
- type PaginationResults
- type Record
- type Schema
- type SortDirection
Constants ¶
const ( SortAscending = SortDirection("asc") SortDescending = SortDirection("desc") )
Variables ¶
var ( ErrNoForeignKeys = errors.New("no foreign keys were found") ErrNoForeignKeyMatch = errors.New("no foreign key matched") ErrNotFound = fmt.Errorf("not found: %w", pgx.ErrNoRows) )
var ( ErrInvalidType = errors.New("invalid type") ErrNoIdentity = errors.New("identity not found") )
Functions ¶
func All ¶
All is the same as Many with an empty sql string. It will return all rows from the table deduced by v and is equivalent to a select from table.
func CollectRows ¶
CollectRows scans a T from each row
func CollectStrings ¶
CollectStrings scans rows for string values
func First ¶
First returns the first row encountered for a given table. It is equivalent to One with an empty sql string
func Many ¶
Many returns all rows encountered that satisfy the sql condition. The sql string is placed immediately after the select statement
func One ¶
One returns the first row encountered that satisfies the sql condition. The sql string is placed immediately after the select statement
func RunScript ¶
RunScript executes a script from the underlying fs set using SetScriptFS. scripts are run as template so it is possible to pass data onto the scripts
func SetScriptFS ¶
SetScriptFS sets the underlying fs for reading sql scripts with RunScript
Types ¶
type Columns ¶
type Columns []string
Columns is an alias for []string that contains methods that faciliate formatting sql strings
func (Columns) AssignmentList ¶
AssignmentList returns an assignment list in the format of column1 = $1, column2 = $2, ... The start argument determines the initial number for the parameters.
func (Columns) PrefixedList ¶
PrefixedList returns a List where each column is prefixed by the given argument. A '.' is automatically added to the prefix
type DB ¶
type DB interface { Begin(ctx context.Context) (pgx.Tx, error) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) }
DB represents the underlying pgx connection. It can be a single conn, pool or transaction
type Field ¶
type Field struct { Name string // Name of the field in the struct Column string // Name of the database column Index int // Index of the field within a struct Identity bool // Is an ID field ReadOnly bool // Is only for select queries FK *FK // Foreign key meta data Schema *Schema // Embeded schema }
Field contains mapping information between struct field and database column
func (*Field) IsWriteable ¶
IsWriteable is true when the fields value can be included in an insert or update statement
type Fields ¶
type Fields []Field
Fields faciliatates collection methods over fields
func (Fields) Columns ¶
Columns returns all database columns for the given fields it goes recursively through fields
func (Fields) ForeignKeys ¶
ForeignKeys are fields representing foreign keys
type ID ¶
type ID int64
ID represents a serial id
func CollectIDs ¶
CollectIDs scans each row for id value
type PaginationOptions ¶
type PaginationOptions struct { Record Record Query string QueryColumns []string Page int PageSize int SortBy string SortDirection SortDirection }
PaginationOptions for configuring paginate query
type PaginationResults ¶
type PaginationResults[T any] struct { Total int64 Items []T Page int Start int End int HasNext bool }
PaginationResults contain results and stats from pagination query
func Paginate ¶
func Paginate[T any](db DB, opts *PaginationOptions) (*PaginationResults[T], error)
Paginate returns paginated data for T
type Record ¶
type Record interface{ TableName() string }
Record is the interface implemented by structs that want to explicitly specify their table name
type Schema ¶
type Schema struct { Parent *Schema // Not nil if schema represents an embeded type Table string // Database table name Type reflect.Type // Underlying reflect type Fields Fields // Field maps }
Schema contains the database mapping information for a given type
func Analyze ¶
Analyze returns a schema representing the mapping between the go type and database row. Schemas are cached by table name so as not to repeat analisis unnecesarily.
func MustAnalyze ¶
func MustAnalyze(v interface{}) *Schema
MustAnalyze panics if schema analisis fails. See Analyze for further information