A golang code generator to introspect and generate database/sql code from Postgres databases
Example usage of the generated code can be found here.
- Create a configuration file in your project root, YAML and JSON formats are supported. Example:
# The PostgreSQL database connection string
dsn: "postgresql://myuser:mypassword@localhost:5432/mydb?sslmode=disable"
# The database schemas that will be introspected for code generation
schemas:
public:
# If views should be included in code generation (Optional, default=false)
include_views: true
# Tables or views that should be ignored in code generation (Optional, default=null)
ignore:
- "locked_table"
- "super_secret_view"
# Golang code generation specific stuff
go:
# The destination folder
dest: "./gen"
# The generated code package name
package: "gen"
# If the generated entities must include JSON tags (Optional, default=false)
emit_json_tags: true- Execute the generator
go run github.com/gustapinto/pg_gen@latest -config=./example_config.yaml- All done! Your generated code is ready to be used
You can also use the generator as a go:generate clause. Example:
//go:generate go run github.com/gustapinto/pg_gen@latest -config=./example_config.yaml
package main
func main() {
// Your code
}The generated code API uses a DAO/Active Record like struct and method organization, example usage of this can be found here.
Currently the generated code API is not set on stone yet, so we highly recommend you to use explicit versions of pg_gen when generating code (ex: go run github.com/gustapinto/[email protected] instead of go run github.com/gustapinto/pg_gen@latest).