Skip to content
forked from piotrkowalczuk/pqt

postgres schema definition, sql/go code generation and helper package

License

Notifications You must be signed in to change notification settings

yurykozyrev/pqt

 
 

Repository files navigation

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

Contribution

Very welcome in general. Especially in fields like:

TODO

  • Postgres types better support.
  • Support for functions.
  • Refactor WithXXX functions to be prefixed by the type they return for example TableWithIfNotExists or ColumnWithNotNull.
  • Constraint.
    • Index
    • Unique
    • Primary Key
    • Foreign Key
    • Check
    • Exclusion

About

postgres schema definition, sql/go code generation and helper package

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.8%
  • Other 0.2%