dgw 根据 PostgreSQL 表元数据生成 Golang 结构体和简单的表/行数据网关函数
dgw generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata. Heavily inspired by xo.
Personally, I prefer Table/Row Data Gateway over ORM/Query Builder approach when using Go with RDBMS. However, it is very time consuming, tedious, and error-prone to write a lot of columns, query place holders, and struct fields that all have to be exactly in order even for a simple select/insert statement. dgw generate Go struct, and simple functions to get/create row from PostgreSQL table definitions to avoid manually writing simple but tedious SQL.
dgw can properly detect autogenerated column (e.g. serial, bigserial), and composit primary key to create appropriate SQL.dgw has ability to easily customize PostgreSQL column type Go type mapping using toml config file.brew install kanmu/tools/dgw…dgw postgres://dbuser@localhost/dbname?sslmode=disable--exclude-column (-X) drops a column from the generated struct field, and from the
generated INSERT/SELECT statements. It must be given in table.column format, so a bare
column name is rejected. Pass the flag multiple times to exclude more than one column.
dgw postgres://dbuser@localhost/dbname?sslmode=disable -X t1.nullable_str -X t1.xml_datadgw stops with an error, instead of generating broken code, when the exclusion is:
GetXByPk would no longer match the table)--exclude to skip the whole table)…Generate Go code by the following command.
$ dgw postgres://dgw_test@localhost/dgw_test?sslmode=disable --typemap=./typemap.toml --schema=public --package=dgwexample --output=example.go…# Start PostgreSQL 16 with pre-configured test database and user
$ docker compose up -d
# Wait for PostgreSQL to be ready
$ docker compose exec postgres pg_isready -U dgw_test
# Run tests
$ go test -v
# Stop PostgreSQL when done
$ docker compose down$ psql -d template1
> CREATE USER dgw_test;
> CREATE DATABASE dgw_test OWNER dgw_test;
> \q
$ go test -v暂无开放 Issues,或尚未同步最近议题。