Skip to content
/ grom Public

一个可以将 mysql 的表字段转换为 golang 的模型结构的命令行工具。A powerful command line tool for converting mysql table fields to golang model structure.

License

Notifications You must be signed in to change notification settings

sliveryou/grom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grom

English简体中文

Github License Go Doc Go Report Github Latest Release Github Latest Tag Github Stars

Grom is a powerful command line tool that can convert mysql table fields to golang model structure. Its full name is golang relational object mapping (GROM).

Installation

Download package by using:

$ go get -u github.com/sliveryou/grom

# if the go version is before 1.16, use the following command to install:
$ GO111MODULE=on go get -u github.com/sliveryou/grom@latest

# if the go version is 1.16 and later, use the following command to install:
$ GO111MODULE=on go install github.com/sliveryou/grom@latest

To build from source code, you need Go environment (1.16 or newer) and use the following commands:

$ git clone https://github.com/sliveryou/grom.git
$ cd grom
$ sh scripts/install.sh

Or download a pre-compiled binary from the release page.

Grom CLI

$ grom -h
Get golang model structure by mysql information schema

Usage:
  grom [command]

Examples:
  grom generate -n ./grom.json
  grom convert -n ./grom.json

Available Commands:
  convert     Convert mysql table fields to golang model structure
  generate    Generate grom configuration file
  help        Help about any command
  version     Show the grom version information

Flags:
  -h, --help   help for grom

Use "grom [command] --help" for more information about a command.

$ grom generate -h
Generate grom configuration file like this:
{
    "host": "localhost",
    "port": 3306,
    "user": "user",
    "password": "password",
    "database": "database",
    "table": "table",
    "package_name": "package_name",
    "struct_name": "struct_name",
    "enable_initialism": true,
    "enable_field_comment": true,
    "enable_sql_null": false,
    "enable_guregu_null": false,
    "enable_json_tag": true,
    "enable_xml_tag": false,
    "enable_gorm_tag": false,
    "enable_xorm_tag": false,
    "enable_beego_tag": false,
    "enable_gorose_tag": false,
    "enable_gorm_v2_tag": true,
    "disable_unsigned": false
}

Usage:
  grom generate [flags]

Examples:
  grom generate -n ./grom.json

Flags:
  -h, --help          help for generate
  -n, --name string   the name of the generated grom configuration file (default "grom.json")

$ grom convert -h
Convert mysql table fields to golang model structure by information_schema.columns and information_schema.statistics

Usage:
  grom convert [flags]

Examples:
  grom convert -n ./grom.json
  grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG --package PACKAGE_NAME --struct STRUCT_NAME

Flags:
  -d, --database string   the database of mysql
  -e, --enable strings    enable services (must in [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG,GORM_V2_TAG,DISABLE_UNSIGNED])
  -h, --help              help for convert
  -H, --host string       the host of mysql
  -n, --name string       the name of the grom configuration file
  -o, --output string     the name of the file used to store the grom output
      --package string    the package name of the converted model structure
  -p, --password string   the password of mysql
  -P, --port int          the port of mysql
      --struct string     the struct name of the converted model structure
  -t, --table string      the table of mysql
  -u, --user string       the user of mysql

$ grom version -h
Show the grom version information, such as project name, project version, go version, git commit id, build time, etc

Usage:
  grom version [flags]

Flags:
  -h, --help   help for version

Supported Generated Types And Tags

Types:

Tags:

Supported Tag Generation Rules

Tag PrimaryKey AutoIncrement ColumnName Type IsNullable Indexes Uniques Default Comment ForeignKey
json × × × × × × × × ×
xml × × × × × × × × ×
gorm v1 ×
xorm ×
beego orm × × ×
gorose × × × × × × × × ×
gorm v2 ×

Supported Function Generation Rules

Tag TableName TableIndex TableUnique
json × × ×
xml × × ×
gorm v1 × ×
xorm × ×
beego orm
gorose × ×
gorm v2 × ×

Usage Example

  1. Create the table named api by following sql:
CREATE TABLE `api` (
    `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '接口id',
    `path` varchar(255) NULL DEFAULT NULL COMMENT '接口路径',
    `description` varchar(255) NULL DEFAULT NULL COMMENT '接口描述',
    `group` varchar(255) NULL DEFAULT NULL COMMENT '接口属组',
    `method` varchar(255) NULL DEFAULT 'POST' COMMENT '接口方法',
    `create_time` bigint(20) NULL DEFAULT NULL COMMENT '创建时间',
    `update_time` bigint(20) NULL DEFAULT NULL COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE INDEX `path_method`(`path`, `method`),
    INDEX `group`(`group`)
) ENGINE = InnoDB AUTO_INCREMENT = 1;
  1. Generate the grom configuration file and edit it:
$ grom generate -n grom.json 
$ vim grom.json
{
    "host": "localhost",
    "port": 3306,
    "user": "user",
    "password": "password",
    "database": "database",
    "table": "api",
    "package_name": "model",
    "struct_name": "API",
    "enable_initialism": true,
    "enable_field_comment": true,
    "enable_sql_null": false,
    "enable_guregu_null": false,
    "enable_json_tag": true,
    "enable_xml_tag": false,
    "enable_gorm_tag": false,
    "enable_xorm_tag": false,
    "enable_beego_tag": false,
    "enable_gorose_tag": false,
    "enable_gorm_v2_tag": true,
    "disable_unsigned": false
}
$ grom convert -n grom.json -o output.go

You can also fill in the parameters on the command line without generating a configuration file:

$ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG

Then you will get the generated code:

package model

type API struct {
    ID          int    `json:"id" gorm:"primaryKey;column:id;type:int(11) auto_increment;comment:接口id"`                           // 接口id
    Path        string `json:"path" gorm:"column:path;type:varchar(255);uniqueIndex:path_method;comment:接口路径"`                  // 接口路径
    Description string `json:"description" gorm:"column:description;type:varchar(255);comment:接口描述"`                            // 接口描述
    Group       string `json:"group" gorm:"column:group;type:varchar(255);index:group;comment:接口属组"`                            // 接口属组
    Method      string `json:"method" gorm:"column:method;type:varchar(255);uniqueIndex:path_method;default:POST;comment:接口方法"` // 接口方法
    CreateTime  int64  `json:"create_time" gorm:"column:create_time;type:bigint(20);comment:创建时间"`                              // 创建时间
    UpdateTime  int64  `json:"update_time" gorm:"column:update_time;type:bigint(20);comment:更新时间"`                              // 更新时间
}

// TableName returns the table name of the API model
func (a *API) TableName() string {
    return "api"
}
  1. Enjoy yourself!

About

一个可以将 mysql 的表字段转换为 golang 的模型结构的命令行工具。A powerful command line tool for converting mysql table fields to golang model structure.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published