goctl model

概述

goctl model 为 goctl 提供的数据库模型代码生成指令,目前支持 MySQL、PostgreSQL、Mongo 的代码生成,MySQL 支持从 sql 文件和数据库连接两种方式生成,PostgreSQL 仅支持从数据库连接生成。

goctl model 指令

  1. $ goctl model --help
  2. Generate model code
  3. Usage:
  4. goctl model [command]
  5. Available Commands:
  6. mongo Generate mongo model
  7. mysql Generate mysql model
  8. pg Generate postgresql model
  9. Flags:
  10. -h, --help help for model
  11. Use "goctl model [command] --help" for more information about a command.

goctl model mongo 指令

Mongo 的生成不同于 MySQL,MySQL 可以从 scheme_information 库中读取到一张表的信息(字段名称,数据类型,索引等), 而 Mongo 是文档型数据库,我们暂时无法从 db 中读取某一条记录来实现字段信息获取,就算有也不一定是完整信息(某些字段可能是 omitempty 修饰,可有可无), 这里采用 type 自己编写+代码生成方式实现。

  1. $ goctl model mongo --help
  2. Generate mongo model
  3. Usage:
  4. goctl model mongo [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -c, --cache Generate code with cache [optional]
  8. -d, --dir string The target dir
  9. -e, --easy Generate code with auto generated CollectionName for easy declare [optional]
  10. -h, --help help for mongo
  11. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  12. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  13. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  14. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
  15. -t, --type strings Specified model type name
goctl model - 图1 参数字段goctl model - 图2 参数类型goctl model - 图3 是否必填goctl model - 图4 默认值goctl model - 图5 参数说明
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
cachebooleanNOfalse是否生成带缓存的代码
dirstringNO当前工作目录代码输出目录
easybooleanNOfalse是否暴露集合名称变量
homestringNO${HOME}/.goctl本地模板文件目录
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
stylestringNOgozero输出文件和目录的命名风格格式化符号,详情见文件风格
type[]stringYESnil结构体类型名称

使用示例

以下我们以生成一个 User 结构体为例。

  1. 生成带缓存的代码
  1. # 进入用户 Home 目录
  2. $ cd ~
  3. # 创建 demo 目录
  4. $ mkdir demo && cd demo
  5. # 生成 mongo 代码
  6. $ goctl model mongo --type User --dir cache --cache
  7. # 查看目录结构
  8. $ tree
  9. .
  10. └── cache
  11. ├── error.go
  12. ├── usermodel.go
  13. ├── usermodelgen.go
  14. └── usertypes.go
  15. 1 directory, 4 files

查看代码

  • error.go
  • usermodel.go
  • usermodelgen.go
  • usertypes.go
  1. package model
  2. import (
  3. "errors"
  4. "github.com/zeromicro/go-zero/core/stores/mon"
  5. )
  6. var (
  7. ErrNotFound = mon.ErrNotFound
  8. ErrInvalidObjectId = errors.New("invalid objectId")
  9. )
  1. package model
  2. import (
  3. "github.com/zeromicro/go-zero/core/stores/cache"
  4. "github.com/zeromicro/go-zero/core/stores/monc"
  5. )
  6. var _ UserModel = (*customUserModel)(nil)
  7. type (
  8. // UserModel is an interface to be customized, add more methods here,
  9. // and implement the added methods in customUserModel.
  10. UserModel interface {
  11. userModel
  12. }
  13. customUserModel struct {
  14. *defaultUserModel
  15. }
  16. )
  17. // NewUserModel returns a model for the mongo.
  18. func NewUserModel(url, db, collection string, c cache.CacheConf) UserModel {
  19. conn := monc.MustNewModel(url, db, collection, c)
  20. return &customUserModel{
  21. defaultUserModel: newDefaultUserModel(conn),
  22. }
  23. }
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/stores/monc"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. var prefixUserCacheKey = "cache:user:"
  11. type userModel interface {
  12. Insert(ctx context.Context, data *User) error
  13. FindOne(ctx context.Context, id string) (*User, error)
  14. Update(ctx context.Context, data *User) error
  15. Delete(ctx context.Context, id string) error
  16. }
  17. type defaultUserModel struct {
  18. conn *monc.Model
  19. }
  20. func newDefaultUserModel(conn *monc.Model) *defaultUserModel {
  21. return &defaultUserModel{conn: conn}
  22. }
  23. func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
  24. if data.ID.IsZero() {
  25. data.ID = primitive.NewObjectID()
  26. data.CreateAt = time.Now()
  27. data.UpdateAt = time.Now()
  28. }
  29. key := prefixUserCacheKey + data.ID.Hex()
  30. _, err := m.conn.InsertOne(ctx, key, data)
  31. return err
  32. }
  33. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  34. oid, err := primitive.ObjectIDFromHex(id)
  35. if err != nil {
  36. return nil, ErrInvalidObjectId
  37. }
  38. var data User
  39. key := prefixUserCacheKey + id
  40. err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
  41. switch err {
  42. case nil:
  43. return &data, nil
  44. case monc.ErrNotFound:
  45. return nil, ErrNotFound
  46. default:
  47. return nil, err
  48. }
  49. }
  50. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  51. data.UpdateAt = time.Now()
  52. key := prefixUserCacheKey + data.ID.Hex()
  53. _, err := m.conn.ReplaceOne(ctx, key, bson.M{"_id": data.ID}, data)
  54. return err
  55. }
  56. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  57. oid, err := primitive.ObjectIDFromHex(id)
  58. if err != nil {
  59. return ErrInvalidObjectId
  60. }
  61. key := prefixUserCacheKey + id
  62. _, err = m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
  63. return err
  64. }
  1. package model
  2. import (
  3. "time"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. )
  6. type User struct {
  7. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  8. // TODO: Fill your own fields
  9. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  10. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  11. }
  1. 生成不带缓存的代码
  1. # 进入用户 Home 目录
  2. $ cd ~
  3. # 创建 demo 目录
  4. $ mkdir demo && cd demo
  5. # 生成 mongo 代码
  6. $ goctl model mongo --type User --dir nocache
  7. # 查看目录结构
  8. $ tree
  9. .
  10. └── nocache
  11. ├── error.go
  12. ├── usermodel.go
  13. ├── usermodelgen.go
  14. └── usertypes.go
  15. 1 directory, 4 files

查看代码

  • error.go
  • usermodel.go
  • usermodelgen.go
  • usertypes.go
  1. package model
  2. import (
  3. "errors"
  4. "github.com/zeromicro/go-zero/core/stores/mon"
  5. )
  6. var (
  7. ErrNotFound = mon.ErrNotFound
  8. ErrInvalidObjectId = errors.New("invalid objectId")
  9. )
  1. package model
  2. import "github.com/zeromicro/go-zero/core/stores/mon"
  3. var _ UserModel = (*customUserModel)(nil)
  4. type (
  5. // UserModel is an interface to be customized, add more methods here,
  6. // and implement the added methods in customUserModel.
  7. UserModel interface {
  8. userModel
  9. }
  10. customUserModel struct {
  11. *defaultUserModel
  12. }
  13. )
  14. // NewUserModel returns a model for the mongo.
  15. func NewUserModel(url, db, collection string) UserModel {
  16. conn := mon.MustNewModel(url, db, collection)
  17. return &customUserModel{
  18. defaultUserModel: newDefaultUserModel(conn),
  19. }
  20. }
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/stores/mon"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. type userModel interface {
  11. Insert(ctx context.Context, data *User) error
  12. FindOne(ctx context.Context, id string) (*User, error)
  13. Update(ctx context.Context, data *User) error
  14. Delete(ctx context.Context, id string) error
  15. }
  16. type defaultUserModel struct {
  17. conn *mon.Model
  18. }
  19. func newDefaultUserModel(conn *mon.Model) *defaultUserModel {
  20. return &defaultUserModel{conn: conn}
  21. }
  22. func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
  23. if data.ID.IsZero() {
  24. data.ID = primitive.NewObjectID()
  25. data.CreateAt = time.Now()
  26. data.UpdateAt = time.Now()
  27. }
  28. _, err := m.conn.InsertOne(ctx, data)
  29. return err
  30. }
  31. func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
  32. oid, err := primitive.ObjectIDFromHex(id)
  33. if err != nil {
  34. return nil, ErrInvalidObjectId
  35. }
  36. var data User
  37. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  38. switch err {
  39. case nil:
  40. return &data, nil
  41. case mon.ErrNotFound:
  42. return nil, ErrNotFound
  43. default:
  44. return nil, err
  45. }
  46. }
  47. func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
  48. data.UpdateAt = time.Now()
  49. _, err := m.conn.ReplaceOne(ctx, bson.M{"_id": data.ID}, data)
  50. return err
  51. }
  52. func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
  53. oid, err := primitive.ObjectIDFromHex(id)
  54. if err != nil {
  55. return ErrInvalidObjectId
  56. }
  57. _, err = m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  58. return err
  59. }
  1. package model
  2. import (
  3. "time"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. )
  6. type User struct {
  7. ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
  8. // TODO: Fill your own fields
  9. UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
  10. CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
  11. }

goctl model mysql 指令

goctl model mysql 指令用于生成基于 MySQL 的 model 代码,支持生成带缓存和不带缓存的代码。MySQL 代码生成支持从 sql 文件,数据库连接两个来源生成代码。

  1. $ goctl model mysql --help
  2. Generate mysql model
  3. Usage:
  4. goctl model mysql [command]
  5. Available Commands:
  6. datasource Generate model from datasource
  7. ddl Generate mysql model from ddl
  8. Flags:
  9. -h, --help help for mysql
  10. -i, --ignore-columns strings Ignore columns while creating or updating rows (default [create_at,created_at,create_time,update_at,updated_at,update_time])
  11. --strict Generate model in strict mode
  12. Use "goctl model mysql [command] --help" for more information about a command.

goctl model mysql datasource 指令

goctl model mysql datasource 指令用于从数据库连接生成 model 代码。

  1. $ goctl model mysql datasource --help
  2. Generate model from datasource
  3. Usage:
  4. goctl model mysql datasource [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -c, --cache Generate code with cache [optional]
  8. -d, --dir string The target dir
  9. -h, --help help for datasource
  10. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  11. --idea For idea plugin [optional]
  12. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  13. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  14. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
  15. -t, --table strings The table or table globbing patterns in the database
  16. --url string The data source of database,like "root:password@tcp(127.0.0.1:3306)/database"
  17. Global Flags:
  18. -i, --ignore-columns strings Ignore columns while creating or updating rows (default [create_at,created_at,create_time,update_at,updated_at,update_time])
  19. --strict Generate model in strict mode
goctl model - 图6 参数字段goctl model - 图7 参数类型goctl model - 图8 是否必填goctl model - 图9 默认值goctl model - 图10 参数说明
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
cachebooleanNOfalse是否生成带缓存的代码
dirstringNO当前工作目录代码输出目录
easybooleanNOfalse是否暴露集合名称变量
homestringNO${HOME}/.goctl本地模板文件目录
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
stylestringNOgozero输出文件和目录的命名风格格式化符号,详情见文件风格
table[]stringYESnil需要生成代码的表
urlstringYES空字符串数据库连接,格式{{username}}:{{password}}@tcp({{host_port}})/{{db}}
ignore-columns[]stringNOnil需要忽略的字段,插入或者更新时需要忽略的字段,如 create_time
strictbooleanNOfalse是否是严格模式,如果是严格模式下,会对 unsigned 修饰的字段转换为对应的数据类型,主要针对数值型,例如:如果数据库中列为 bigint 类型,如果为unsigned 修饰则对应的 golang 数据类型就为 int64,否则为 uint64,如果 strict 为 false,则不关注 unsigned 修饰

goctl model mysql ddl 指令

goctl model mysql ddl 指令用于从 sql 文件生成 model 代码。

  1. $ goctl model mysql ddl --help
  2. Generate mysql model from ddl
  3. Usage:
  4. goctl model mysql ddl [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -c, --cache Generate code with cache [optional]
  8. --database string The name of database [optional]
  9. -d, --dir string The target dir
  10. -h, --help help for ddl
  11. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  12. --idea For idea plugin [optional]
  13. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  14. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  15. -s, --src string The path or path globbing patterns of the ddl
  16. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
  17. Global Flags:
  18. -i, --ignore-columns strings Ignore columns while creating or updating rows (default [create_at,created_at,create_time,update_at,updated_at,update_time])
  19. --strict Generate model in strict mode
goctl model - 图11 参数字段goctl model - 图12 参数类型goctl model - 图13 是否必填goctl model - 图14 默认值goctl model - 图15 参数说明
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
cachebooleanNOfalse是否生成带缓存的代码
dirstringNO当前工作目录代码输出目录
easybooleanNOfalse是否暴露集合名称变量
homestringNO${HOME}/.goctl本地模板文件目录
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
srcstringYES空字符串sql 文件路径
stylestringNOgozero输出文件和目录的命名风格格式化符号,详情见文件风格
ignore-columns[]stringNOnil需要忽略的字段,插入或者更新时需要忽略的字段,如 create_time
strictbooleanNOfalse是否是严格模式,如果是严格模式下,会对 unsigned 修饰的字段转换为对应的数据类型,主要针对数值型,例如:如果数据库中列为 bigint 类型,如果为unsigned 修饰则对应的 golang 数据类型就为 int64,否则为 uint64,如果 strict 为 false,则不关注 unsigned 修饰

MySQL 类型映射关系

  • strict 为 true 时,且 unsigned 修饰
  • strict 不为 true 时
goctl model - 图16 MySQL 类型goctl model - 图17 是否为 null 约束goctl model - 图18 Golang 类型
bitNObyte
tinyintNOuint64
tinyintYESsql.NullInt64
smallintNOuint64
smallintYESsql.NullInt64
mediumintNOuint64
mediumintYESsql.NullInt64
intNOuint64
intYESsql.NullInt64
middleintNOuint64
middleintYESsql.NullInt64
int1NOuint64
int1YESsql.NullInt64
int2NOuint64
int2YESsql.NullInt64
int3NOuint64
int3YESsql.NullInt64
int4NOuint64
int4YESsql.NullInt64
int8NOiunt64
int8YESsql.NullInt64
integerNOuint64
integerYESsql.NullInt64
bigintNOuint64
bigintYESsql.NullInt64
floatNOfloat64
floatYESsql.NullFloat64
float4NOfloat64
float4YESsql.NullFloat64
float8NOfloat64
float8YESsql.NullFloat64
dateNOtime.Time
datetimeNOtime.Time
timstampNOtime.Time
timeNOstring
yearNOint64
charNOstring
varcharNOstring
nvarcharNOstring
ncharNOstring
characterNOstring
longvarcharNOstring
linestringNOstring
multilinestringNOstring
binaryNOstring
varbinaryNOstring
tinytextNOstring
textNOstring
mediumtextNOstring
longtextNOstring
enumNOstring
setNOstring
jsonNOstring
blobNOstring
longblobNOstring
mediumblobNOstring
tinyblobNOstring
boolNObool
blleanNObool
goctl model - 图19 MySQL 类型goctl model - 图20 是否为 null 约束goctl model - 图21 Golang 类型
bitNObyte
tinyintNOint64
tinyintYESsql.NullInt64
smallintNOint64
smallintYESsql.NullInt64
mediumintNOint64
mediumintYESsql.NullInt64
intNOint64
intYESsql.NullInt64
middleintNOint64
middleintYESsql.NullInt64
int1NOint64
int1YESsql.NullInt64
int2NOint64
int2YESsql.NullInt64
int3NOint64
int3YESsql.NullInt64
int4NOint64
int4YESsql.NullInt64
int8NOint64
int8YESsql.NullInt64
integerNOint64
integerYESsql.NullInt64
bigintNOint64
bigintYESsql.NullInt64
floatNOfloat64
floatYESsql.NullFloat64
float4NOfloat64
float4YESsql.NullFloat64
float8NOfloat64
float8YESsql.NullFloat64
dateNOtime.Time
datetimeNOtime.Time
timstampNOtime.Time
timeNOstring
yearNOint64
charNOstring
varcharNOstring
nvarcharNOstring
ncharNOstring
characterNOstring
longvarcharNOstring
linestringNOstring
multilinestringNOstring
binaryNOstring
varbinaryNOstring
tinytextNOstring
textNOstring
mediumtextNOstring
longtextNOstring
enumNOstring
setNOstring
jsonNOstring
blobNOstring
longblobNOstring
mediumblobNOstring
tinyblobNOstring
boolNObool
blleanNObool

goctl model pg 指令

goctl model pg 指令用于从 PostgreSQL 数据库中生成 Go 语言代码。

  1. $ goctl model pg --help
  2. Generate postgresql model
  3. Usage:
  4. goctl model pg [flags]
  5. goctl model pg [command]
  6. Available Commands:
  7. datasource Generate model from datasource
  8. Flags:
  9. -h, --help help for pg
  10. Use "goctl model pg [command] --help" for more information about a command.

goctl model pg datasource 指令

  1. $ goctl model pg datasource --help
  2. Generate model from datasource
  3. Usage:
  4. goctl model pg datasource [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -c, --cache Generate code with cache [optional]
  8. -d, --dir string The target dir
  9. -h, --help help for datasource
  10. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  11. --idea For idea plugin [optional]
  12. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  13. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  14. -s, --schema string The table schema (default "public")
  15. --strict Generate model in strict mode
  16. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
  17. -t, --table string The table or table globbing patterns in the database
  18. --url string The data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"
goctl model - 图22 参数字段goctl model - 图23 参数类型goctl model - 图24 是否必填goctl model - 图25 默认值goctl model - 图26 参数说明
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
cachebooleanNOfalse是否生成带缓存的代码
dirstringNO当前工作目录代码输出目录
easybooleanNOfalse是否暴露集合名称变量
homestringNO${HOME}/.goctl本地模板文件目录
ideabooleanNOfalse是否为 idea 使用,终端请忽略此字段
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
strictbooleanNOfalse是否是严格模式,如果是严格模式下,会对 unsigned 修饰的字段转换为对应的数据类型,主要针对数值型,例如:如果数据库中列为 bigint 类型,如果为unsigned 修饰则对应的 golang 数据类型就为 int64,否则为 uint64,如果 strict 为 false,则不关注 unsigned 修饰
stylestringNOgozero输出文件和目录的命名风格格式化符号,详情见文件风格
table[]stringYESnil需要生成代码的表
urlstringYES空字符串数据库连接,格式 postgres://{{username}}:{{password}}@{{host_port}}/{{db}}?sslmode=disable

PostgreSQL 类型映射关系

goctl model - 图27 PostgreSQL 类型goctl model - 图28 Golang 类型
boolbool
_boolpq.BoolArray
booleanbool
tinyintint64
smallintint64
mediumintint64
intint64
int1int64
int2int64
_int2pq.Int64Array
int3int64
int4int64
_int4pq.Int64Array
int8int64
_int8pq.Int64Array
integerint64
_integerpq.Int64Array
bigintint64
floatfloat64
float4float64
_float4pq.Float64Array
float8float64
_float8pq.Float64Array
doublefloat64
decimalfloat64
decfloat64
fixedfloat64
realfloat64
bitbyte
datetime.Time
datetimetime.Time
timestamptime.Time
timestring
yearint64
linestringstring
multilinestringstring
nvarcharstring
ncharstring
charstring
_charpq.StringArray
characterstring
varcharstring
_varcharpq.StringArray
binarystring
byteastring
longvarbinarystring
varbinarystring
tinytextstring
textstring
_textpq.StringArray
mediumtextstring
longtextstring
enumstring
setstring
jsonstring
jsonbstring
blobstring
longblobstring
mediumblobstring
tinyblobstring
ltree[]byte