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