百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

dgw

> 数据库
开源

dgw 根据 PostgreSQL 表元数据生成 Golang 结构体和简单的表/行数据网关函数

190 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

dgw 根据 PostgreSQL 表元数据生成 Golang 结构体和简单的表/行数据网关函数

dgw

Description

dgw generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata. Heavily inspired by xo.

Why created

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.

Installation

bash
brew install kanmu/tools/dgw

How to use

…
dgw postgres://dbuser@localhost/dbname?sslmode=disable

Excluding columns

--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_data

dgw stops with an error, instead of generating broken code, when the exclusion is:

  • a primary key column (GetXByPk would no longer match the table)
  • a table or column which does not exist in the schema (usually a typo)
  • every column of a table (use --exclude to skip the whole table)

Example

  • https://github.com/kanmu/dgw/tree/master/example
…

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
…

Test

Using Docker (Recommended)

bash
# 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

Manual Setup

bash
$ psql -d template1
> CREATE USER dgw_test;
> CREATE DATABASE  dgw_test OWNER dgw_test;
> \q
$ go test -v

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Godbgeneratorgopostgresql

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库