Skip to content
forked from wantedly/pq2gorm

Generate gorm model structs from PostgreSQL database schema

License

Notifications You must be signed in to change notification settings

fahmifan/pq2gorm

 
 

Repository files navigation

pq2gorm - Generate gorm model structs from PostgreSQL database schema

Build Status

pq2gorm is a generator of gorm model structs from a PostgresSQL database.

  • Input: Connection URI of a PostgresSQL database.
  • Output: Model definitions based on gorm annotated struct.

How to build and install

Prepare Go 1.6 or higher. Go 1.5 is acceptable, but GO15VENDOREXPERIMENT=1 must be set.

After installing required version of Go, you can build and install pq2gorm by

$ go get -d -u github.com/fahmifan/pq2gorm
$ cd $GOPATH/src/github.com/fahmifan/pq2gorm
$ make
$ make install

make generates a binary into bin/pq2gorm. make install put it to $GOPATH/bin.

How to use

Run pq2gorm with Connection URI of a PostgresSQL database. Connection URI is necessary for running.

Usage

$ pq2gorm
Usage: Generate gorm model structs from PostgreSQL database schema.
  -d string
        Set output path (default "./")
  -dir string
        Set output path (default "./")
  -t string
        Target tables (table1,table2,...) (default: all tables)
  -tables string
        Target tables (table1,table2,...) (default: all tables)

Example 1: Generate gorm model files of all tables in current directory.

$ pq2gorm "postgresql://user:password@host:port/dbname?sslmode=disable"

For example, user model user.go as shown below will be generated:

type User struct {
    ID uint `json:"id"`
    ...
}

Example 2: Generate gorm model files of all tables in ./out directory.

$ pq2gorm "postgresql://user:password@host:port/dbname?sslmode=disable"  -dbtype postgres -d ./out

Example 3: Generate gorm model files of profiles and users tables.

$ pq2gorm "postgresql://user:password@host:port/dbname?sslmode=disable" -d ./out -t profiles,users

Example 4 Generate gorm model files of all tables in ./out directory for cockroachdb.

$ pq2gorm "postgresql://user:password@host:port/dbname?sslmode=disable"  -dbtype cockroachdb -dbname $dbname -d ./out

If the directory ./out does not exist, pq2gorm creates ./out directory with output files.

License

MIT License

About

Generate gorm model structs from PostgreSQL database schema

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 89.8%
  • Makefile 5.6%
  • Shell 4.6%