-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
generate.go
56 lines (43 loc) · 1.35 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"strings"
"gorm.io/gen"
"gorm.io/gen/examples/conf"
"gorm.io/gen/examples/dal"
)
func init() {
dal.DB = dal.ConnectDB(conf.MySQLDSN)
prepare(dal.DB) // prepare table for generate
}
// dataMap mapping relationship
var dataMap = map[string]func(detailType string) (dataType string){
// int mapping
"int": func(detailType string) (dataType string) { return "int32" },
// bool mapping
"tinyint": func(detailType string) (dataType string) {
if strings.HasPrefix(detailType, "tinyint(1)") {
return "bool"
}
return "byte"
},
}
func main() {
g := gen.NewGenerator(gen.Config{
OutPath: "../../dal/query",
ModelPkgPath: "../../dal/model",
// generate model global configuration
FieldNullable: true, // generate pointer when field is nullable
FieldCoverable: true, // generate pointer when field has default value
FieldWithIndexTag: true, // generate with gorm index tag
FieldWithTypeTag: true, // generate with gorm column type tag
})
g.UseDB(dal.DB)
// specify diy mapping relationship
g.WithDataTypeMap(dataMap)
// generate all field with json tag end with "_example"
g.WithJSONTagNameStrategy(func(c string) string { return c + "_example" })
mytable := g.GenerateModel("mytables")
g.ApplyBasic(mytable)
// g.ApplyBasic(g.GenerateAllTable()...) // generate all table in db server
g.Execute()
}