grom

command module
v1.0.3-0...-832441f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2020 License: MIT Imports: 1 Imported by: 0

README

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).

fork下来未提交pr,自己先研究着

Installation

Download package by using:

原著作者
$ go get -u github.com/sliveryou/grom
此版本
$ go get -u github.com/LeeMyGithub/grom

将作者的sliveryou更换为LeeMyGithub 可为此版本 此版本更新一下小功能 增加功能 json 配置中表名TABLE 不填写则为填写的数据库进行映射 struct名称与表明一致 To build from source code, you need Go environment (1.14 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
}

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])
  -h, --help              help for convert
  -H, --host string       the host of mysql
  -n, --name string       the name of the grom configuration file
      --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
}
$ grom convert -n grom.json

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"
}

增加功能 json 配置中表名TABLE 不填写则为填写的数据库进行映射 struct名称与表明一致

  1. Enjoy yourself!

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL