This package is a toolbox for postgres driven applications. It provides multiple tools to help to work with postgres efficiently. In comparison to other currently available libraries instead of pushing struct tags into anti pattern or parsing SQL, it allows to define schema programmatically.
- query builder:
Composer
- builder like object that keeps buffer and arguments but also tracks positional parameters.
- array support
JSONArrayInt64
- wrapper for []int64, it generates JSONB compatible array[]
instead of{}
JSONArrayFloat64
- wrapper for []float64, it generates JSONB compatible array[]
instead of{}
JSONArrayString
- wrapper for []string, it generates JSONB compatible array[]
instead of{}
- sql generation
- go generation - it includes:
<table-name>Entity
- struct that reflects single row within the database<table-name>Criteria
- object that can be passed to theFind
method, it allows to create complex queries<table-name>Patch
- structure used byUpdateOneBy<primary-key>
methods to modify existing cri<table-name>Iterator
- interface used byFindIter
methods as a result, implementation is<table-name>Rows
- it wrapssql.Rows
constants
:complete names
column names
constraints
- library generates exact names of each constraint and corresponding constant that allow to easily handle query errors usingErrorConstraint
helper function
<table-name>Repository
- data access layer that expose API to manipulate entities:Count
- returns number of entities for given criFind
- returns collection of entities that match given criFindIter
- works likeFind
but returnsiterator
Insert
- saves given cri into the databaseFindOneBy<primary-key>
- retrieves single cri, search by primary keyFindOneBy<unique-key>
- retrieves single cri, search by unique keyUpdateOneBy<primary-key>
- modifies single cri, search by primary keyUpdateOneBy<unique-key>
- modifies single cri, search by unique keyDeleteOneBy<primary-key>
- modifies single cri, search by primary key
func Scan<Entity>Rows(rows *sql.Rows) ([]*<cri>Entity, error) {
helper function
- schema definition - allow to programmatically define database schema, that includes:
schemas
tables
columns
constraints
relationships
- helper functions
ErrorConstraint
- if possible extracts constraint from pq.Error so it's easy to build switch statements using generated constraints.
pqtgo supports plugins over the interface.
Package itself do not provide any command line application that would generate output out of given input. Instead it encourage to write local generation application next to the proper package. Good example how such application could be structured can be found in examples.
By default example is trying to connect to local test
database on default port.
To run it simply call:
$ make gen // not necessary, since generated code is already part of the repo
$ make run
Very welcome in general. Especially in fields like:
- Change
<entity-name>FindExpr.OrderBy
to slice. - Postgres types better support.
- Support for functions.
- Selective go/sql generation.
- Refactor
WithXXX
functions to be prefixed by the type they return for exampleTableWithIfNotExists
orColumnWithNotNull
. - Constraint.
- Index
- Unique
- Primary Key
- Foreign Key
- Check
- Exclusion