pqt
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.
It relies to a large degree on packages:
Features:
- array support - golang postgres driver do not support arrays natively, pqt comes with help:
- pqt.ArrayInt64 - wrapper for []int64
- pqt.ArrayFloat64 - wrapper for []float64
- pqt.ArrayString - wrapper for []string
- sql generation
- go generation - it includes:
entity
- struct that reflects single row within the databasecriteria
- object that can be passed to theFind
method, it allows to create complex queriespatch
- structure used byUpdateBy<primary-key>
methods to modify existing entityiterator
- structure used byFindIter
methods as a result, it wrapssql.Rows
constants
:table names
column names
constraints
- library generates exact names of each constraint and corresponding constant that allow to easily handle query errors using ErrorConstraint helper function
repository
- data access layer that expose API to manipulate entities:Count
- returns number of entities for given criteriaFind
- returns collection of entities that match given criteriaFindIter
- works likeFind
but returnsiterator
Insert
- saves given entity into the databaseFindOneBy<primary-key>
- retrieves single entityUpdateBy<primary-key>
- modifies single entityDeleteBy<primary-key>
- modifies single entity
func Scan<Entity>Rows(rows *sql.Rows) ([]*<entity>Entity, error) {
helper function
- schema definition - allow to programmatically define database schema, that includes:
schemas
tables
columns
constraints
relationships
Documentation
Example
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
Contribution
Very welcome in general. Especially in fields like:
- postgres types better support
- support for functions
- better control over if generated code is private or public