postgres schema definition, sql/go code generation and helper package
Go Other
Permalink
Failed to load latest commit information.
example test and formatting fixes Jan 12, 2017
pqtgo set composer as dirty when column default value is set on update Jan 19, 2017
pqtsql separate WithSchemaIfNotExists and WithTableIfNotExists to be compati… Jul 13, 2016
.gitignore more tests, more comments Jan 15, 2016
.travis.yml plugin support - .travis.yml update postgres to version 9.4 Jan 7, 2017
LICENSE.txt LICENSE.txt Jun 5, 2016
README.md README.md update #2 Jan 12, 2017
constraint.go WithColumnShortName, WithTableShortName Jul 8, 2016
constraint_test.go TestForeignKey May 30, 2016
error.go ErrorConstraint function inspired by grpc.ErrorDesc Mar 1, 2016
error_test.go ExampleErrorConstraint May 30, 2016
function.go relationshup functionality refactor, table api changed, generators ap… Jan 17, 2016
json_array.go new interface CompositionWriter that replace Criterion, Composer that… Jun 25, 2016
json_array_test.go UpdateOneBy<unique-key>, controll over visibility Aug 18, 2016
relationship.go new interface CompositionWriter that replace Criterion, Composer that… Jun 25, 2016
relationship_test.go lint fixes Sep 24, 2016
row.go plugin support - example app, complete table Jan 4, 2017
row_test.go UpdateOneBy<unique-key>, controll over visibility Aug 18, 2016
schema.go separate WithSchemaIfNotExists and WithTableIfNotExists to be compati… Jul 13, 2016
table.go bidirectional option works now for relationships created by column re… Sep 14, 2016
table_test.go adding reference to the column creates now many to one relationship Sep 14, 2016
test.sh plugin support - upgraded test.sh script Jan 2, 2017
type.go plugin support - example app, complete table Jan 4, 2017
type_test.go Correct Double to match Postgres type May 27, 2016

README.md

pqt GoDoc Build Status codecov.io

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.

Features:

  • 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 the Find method, it allows to create complex queries
    • <table-name>Patch - structure used by UpdateOneBy<primary-key> methods to modify existing cri
    • <table-name>Iterator - structure used by FindIter methods as a result, it wraps sql.Rows
    • constants:
      • complete 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
    • <table-name>Repository - data access layer that expose API to manipulate entities:
      • Count - returns number of entities for given cri
      • Find - returns collection of entities that match given cri
      • FindIter - works like Find but returns iterator
      • Insert - saves given cri into the database
      • FindOneBy<primary-key> - retrieves single cri, search by primary key
      • FindOneBy<unique-key> - retrieves single cri, search by unique key
      • UpdateOneBy<primary-key> - modifies single cri, search by primary key
      • UpdateOneBy<unique-key> - modifies single cri, search by unique key
      • DeleteOneBy<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.

Documentation

Plugins

pqtgo supports plugins over the interface.

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 w is already part of the repo
$ make run

Contribution

Very welcome in general. Especially in fields like:

  • postgres types better support
  • support for functions