gen

Converts a database into gorm structs and RESTful api

Github星跟蹤圖

gen

License GoDoc travis Go Report Card

The gen tool produces golang structs from a given database for use in a .go file.
It supports gorm tags and implements some usable methods.
It can also generate RESTful api for those structs.

By reading details from the database about the column structure, gen generates a go compatible struct type
with the required column names, data types, and annotations.

Generated datatypes include support for nullable columns sql.NullX types or guregu null.X types
and the expected basic built in go types.

gen is based/inspired by the work of Seth Shelnutt's db2struct, and Db2Struct is based/inspired by the work of ChimeraCoder's gojson package gojson.

Usage

go get github.com/smallnest/gen
gen --connstr "root@tcp(127.0.0.1:3306)/employees?&parseTime=True" --database employees  --json --gorm --guregu --rest

Supported Databases

Currently Supported

  • MariaDB
  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • SQLite

Planned Support

  • Oracle

MariaDB/MySQL

Structures are created by querying the INFORMATION_SCHEMA.Columns table and then formatting the types, column names,
and metadata to create a usable go compatible struct type.

Supported Datatypes

Currently only a limited number of datatypes are supported. Initial support includes:

  • tinyint (sql.NullInt64 or null.Int)
  • int (sql.NullInt64 or null.Int)
  • smallint (sql.NullInt64 or null.Int)
  • mediumint (sql.NullInt64 or null.Int)
  • bigint (sql.NullInt64 or null.Int)
  • decimal (sql.NullFloat64 or null.Float)
  • float (sql.NullFloat64 or null.Float)
  • double (sql.NullFloat64 or null.Float)
  • datetime (null.Time)
  • time (null.Time)
  • date (null.Time)
  • timestamp (null.Time)
  • var (sql.String or null.String)
  • enum (sql.String or null.String)
  • varchar (sql.String or null.String)
  • longtext (sql.String or null.String)
  • mediumtext (sql.String or null.String)
  • text (sql.String or null.String)
  • tinytext (sql.String or null.String)
  • binary
  • blob
  • longblob
  • mediumblob
  • varbinary

Issues

  • Postgres and SQLite driver support for sql.ColumnType.Nullable() (#3)
  • Can not distinguish primay key of tables. Only set the first field as primay key. So you need to change it in some cases.

主要指標

概覽
名稱與所有者smallnest/gen
主編程語言Go
編程語言Go (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2018-03-07 10:31:36
推送於2023-04-24 10:18:15
最后一次提交2023-04-24 18:18:15
發布數24
最新版本名稱v0.9.29 (發布於 2021-04-18 16:43:09)
第一版名稱v0.9.0 (發布於 2020-05-13 07:12:55)
用户参与
星數1.4k
關注者數34
派生數232
提交數248
已啟用問題?
問題數103
打開的問題數27
拉請求數82
打開的拉請求數2
關閉的拉請求數8
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?