main.go 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "fmt"
  4. "git.rtzhtech.cn/iss/public-lib/config"
  5. "git.rtzhtech.cn/iss/public-lib/dao"
  6. "gorm.io/gen"
  7. "regexp"
  8. )
  9. func main() {
  10. // specify the output directory (default: "./query")
  11. // ### if you want to query without context constrain, set mode gen.WithoutContext ###
  12. g := gen.NewGenerator(gen.Config{
  13. OutPath: "./dao",
  14. ModelPkgPath: "./model",
  15. Mode: gen.WithoutContext,
  16. /* Mode: gen.WithoutContext|gen.WithDefaultQuery*/
  17. //if you want the nullable field generation property to be pointer type, set FieldNullable true
  18. /* FieldNullable: true,*/
  19. //if you want to assign field which has default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
  20. /* FieldCoverable: true,*/
  21. // if you want generate field with unsigned integer type, set FieldSignable true
  22. /* FieldSignable: true,*/
  23. //if you want to generate index tags from database, set FieldWithIndexTag true
  24. /* FieldWithIndexTag: true,*/
  25. //if you want to generate type tags from database, set FieldWithTypeTag true
  26. /* FieldWithTypeTag: true,*/
  27. //if you need unit tests for query code, set WithUnitTest true
  28. //WithUnitTest: true,
  29. })
  30. // reuse the database connection in Project or create a connection here
  31. // if you want to use GenerateModel/GenerateModelAs, UseDB is necessary or it will panic
  32. //db, err := dao.ConnectDb(config.Cfg())
  33. //if err != nil {
  34. // return
  35. //}
  36. //g.UseDB(db)
  37. // apply basic crud api on structs or table models which is specified by table name with function
  38. // GenerateModel/GenerateModelAs. And generator will generate table models' code when calling Excute.
  39. // 想对已有的model生成crud等基础方法可以直接指定model struct ,例如model.User{}
  40. // 如果是想直接生成表的model和crud方法,则可以指定表的名称,例如g.GenerateModel("company")
  41. // 想自定义某个表生成特性,比如struct的名称/字段类型/tag等,可以指定opt,例如g.GenerateModel("company",gen.FieldIgnore("address")), g.GenerateModelAs("people", "Person", gen.FieldIgnore("address"))
  42. //g.ApplyBasic(model.User{}, g.GenerateModel("company"), g.GenerateModelAs("people", "Person", gen.FieldIgnore("address")))
  43. //g.WithNewTagNameStrategy(func(c string) string {
  44. // return fmt.Sprintf("c:\"%s\"", c)
  45. //})
  46. //g.WithJSONTagNameStrategy(func(string) string { return "" })
  47. b := true
  48. //b = false
  49. if b {
  50. //g.ApplyBasic(g.GenerateAllTable(
  51. // gen.FieldGORMTag("create_at", "->"),
  52. // gen.FieldNewTag("create_at", "c:\"-\""),
  53. // gen.FieldGORMTag("update_at", "->"),
  54. // gen.FieldNewTag("update_at", "c:\"-\""),
  55. //)...)
  56. tableList, err := db.Migrator().GetTables()
  57. if err != nil {
  58. panic(fmt.Errorf("get all tables fail: %w", err))
  59. }
  60. var tableModels []any
  61. reg := regexp.MustCompile(`.*_copy[0-9]+`)
  62. for _, tableName := range tableList {
  63. if tableName == "ienv_history" || reg.MatchString(tableName) {
  64. continue
  65. }
  66. tableModels = append(tableModels, g.GenerateModel(tableName, gen.FieldGORMTag("create_at", "->"),
  67. gen.FieldNewTag("create_at", "c:\"-\""),
  68. gen.FieldGORMTag("update_at", "->"),
  69. gen.FieldNewTag("update_at", "c:\"-\""),
  70. gen.FieldTypeJsonTag("int64", ",string"),
  71. ))
  72. }
  73. g.ApplyBasic(tableModels...)
  74. } else {
  75. g.ApplyBasic(
  76. g.GenerateModel("sys_usr",
  77. gen.FieldGORMTag("create_at", "->"),
  78. gen.FieldNewTag("create_at", "c:\"-\""),
  79. gen.FieldGORMTag("update_at", "->"),
  80. gen.FieldNewTag("update_at", "c:\"-\""),
  81. ),
  82. )
  83. }
  84. // apply diy interfaces on structs or table models
  85. // 如果想给某些表或者model生成自定义方法,可以用ApplyInterface,第一个参数是方法接口,可以参考DIY部分文档定义
  86. //g.ApplyInterface(func(method model.Method) {}, model.User{}, g.GenerateModel("company"))
  87. // execute the action of code generation
  88. g.Execute()
  89. }