ent 基本使用九 代码生成

ent 提供了cli 工具,可以方便我们进行schema 以及代码生成,同时目前提供的cli已经够用了

安装 cli

go get github.com/facebookincubator/ent/cmd/entc

创建schema

entc init User Pet

生成代码

基于schema 的定义(字段,索引,边,配置)

entc generate ./ent/schema

说明:
生成的代码包含了crud、migrate、entity、client

版本兼容处理

使用独立的cli以及生成的代码,可能会和我们的框架不兼容 ,处理方法使用go:generate
参考配置:

package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate ./schema
 

代码生成选项

  • 命令参数
generate go code for the schema directory
Usage:
  entc generate [flags] path
Examples:
  entc generate ./ent/schema
  entc generate github.com/a8m/x
Flags:
      --header string override codegen header
  -h, --help help for generate
      --idtype [int string] type of the id field (default int)
      --storage strings list of storage drivers to support (default [sql])
      --target string target directory for codegen
      --template strings external templates to execute
 
 
  • 存储配置
    目前ent 生成可以支持sql方言以及Gremlin方言,后期应该还会扩展的
  • 外部模版
    当前官方提供了一个graphql 的 demo

entc 做为包依赖

entc 可以做为一个包在我们的项目中使用

 
package main
import (
    "log"
    "github.com/facebookincubator/ent/entc"
    "github.com/facebookincubator/ent/entc/gen"
    "github.com/facebookincubator/ent/schema/field"
)
func main() {
    err := entc.Generate("./schema", &gen.Config{
        Header: "// Your Custom Header",
        IDType: &field.TypeInfo{Type: field.TypeInt},
    })
    if err != nil {
        log.Fatal("running ent codegen:", err)
    }
}
 
 

查看schema 的描述信息

  • 命令
entc describe ./ent/schema
  • 参考
City:
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        | Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        | id | <nil> | false | false | false | false | false | false | json:"id,omitempty" | 0 |
        | name | string | false | false | false | false | false | false | json:"name,omitempty" | 0 |
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        +---------+--------+---------+---------+----------+--------+----------+
        | Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
        +---------+--------+---------+---------+----------+--------+----------+
        | streets | Street | false | | O2M | false | true |
        +---------+--------+---------+---------+----------+--------+----------+
Street:
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        | Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        | id | <nil> | false | false | false | false | false | false | json:"id,omitempty" | 0 |
        | name | string | false | false | false | false | false | false | json:"name,omitempty" | 0 |
        +-------+--------+--------+----------+----------+---------+---------------+-----------+-----------------------+------------+
        +------+------+---------+---------+----------+--------+----------+
        | Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
        +------+------+---------+---------+----------+--------+----------+
        | city | City | true | streets | M2O | true | true |
        +------+------+---------+---------+----------+--------+----------+

参考资料

https://entgo.io/docs/code-gen/

原文地址:https://www.cnblogs.com/rongfengliang/p/11675348.html