generate.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/0xJacky/Nginx-UI/model"
  6. "github.com/0xJacky/Nginx-UI/settings"
  7. cSettings "github.com/uozi-tech/cosy/settings"
  8. "gorm.io/driver/sqlite"
  9. "gorm.io/gen"
  10. "gorm.io/gorm"
  11. "gorm.io/gorm/logger"
  12. "log"
  13. "path"
  14. )
  15. func main() {
  16. // specify the output directory (default: "./query")
  17. // ### if you want to query without context constrain, set mode gen.WithoutContext ###
  18. g := gen.NewGenerator(gen.Config{
  19. OutPath: "../../query",
  20. Mode: gen.WithoutContext | gen.WithDefaultQuery,
  21. //if you want the nullable field generation property to be pointer type, set FieldNullable true
  22. FieldNullable: true,
  23. //if you want to assign field which has the default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
  24. FieldCoverable: true,
  25. // if you want to generate field with an unsigned integer type, set FieldSignable true
  26. /* FieldSignable: true,*/
  27. //if you want to generate index tags from the database, set FieldWithIndexTag true
  28. /* FieldWithIndexTag: true,*/
  29. //if you want to generate type tags from the database, set FieldWithTypeTag true
  30. /* FieldWithTypeTag: true,*/
  31. //if you need unit tests for query code, set WithUnitTest true
  32. /* WithUnitTest: true, */
  33. })
  34. // reuse the database connection in Project or create a connection here
  35. // if you want to use GenerateModel/GenerateModelAs, UseDB is necessary, or it will panic
  36. var confPath string
  37. flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
  38. flag.Parse()
  39. cSettings.Init(confPath)
  40. dbPath := path.Join(path.Dir(confPath), fmt.Sprintf("%s.db", settings.DatabaseSettings.Name))
  41. var err error
  42. db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
  43. Logger: logger.Default.LogMode(logger.Info),
  44. PrepareStmt: true,
  45. DisableForeignKeyConstraintWhenMigrating: true,
  46. })
  47. if err != nil {
  48. log.Fatalln(err)
  49. }
  50. g.UseDB(db)
  51. // apply basic crud api on structs or table models which is specified by table name with function
  52. // GenerateModel/GenerateModelAs. And the generator will generate table models' code when calling Excute.
  53. g.ApplyBasic(model.GenerateAllModel()...)
  54. // apply diy interfaces on structs or table models
  55. g.ApplyInterface(func(method model.Method) {}, model.GenerateAllModel()...)
  56. // execute the action of code generation
  57. g.Execute()
  58. }