package main import ( "fmt" "git.rtzhtech.cn/iss/public-lib/config" "git.rtzhtech.cn/iss/public-lib/dao" "gorm.io/gen" "regexp" ) func main() { // specify the output directory (default: "./query") // ### if you want to query without context constrain, set mode gen.WithoutContext ### g := gen.NewGenerator(gen.Config{ OutPath: "./dao", ModelPkgPath: "./model", Mode: gen.WithoutContext, /* Mode: gen.WithoutContext|gen.WithDefaultQuery*/ //if you want the nullable field generation property to be pointer type, set FieldNullable true /* FieldNullable: true,*/ //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 /* FieldCoverable: true,*/ // if you want generate field with unsigned integer type, set FieldSignable true /* FieldSignable: true,*/ //if you want to generate index tags from database, set FieldWithIndexTag true /* FieldWithIndexTag: true,*/ //if you want to generate type tags from database, set FieldWithTypeTag true /* FieldWithTypeTag: true,*/ //if you need unit tests for query code, set WithUnitTest true //WithUnitTest: true, }) // reuse the database connection in Project or create a connection here // if you want to use GenerateModel/GenerateModelAs, UseDB is necessary or it will panic //db, err := dao.ConnectDb(config.Cfg()) //if err != nil { // return //} //g.UseDB(db) // apply basic crud api on structs or table models which is specified by table name with function // GenerateModel/GenerateModelAs. And generator will generate table models' code when calling Excute. // 想对已有的model生成crud等基础方法可以直接指定model struct ,例如model.User{} // 如果是想直接生成表的model和crud方法,则可以指定表的名称,例如g.GenerateModel("company") // 想自定义某个表生成特性,比如struct的名称/字段类型/tag等,可以指定opt,例如g.GenerateModel("company",gen.FieldIgnore("address")), g.GenerateModelAs("people", "Person", gen.FieldIgnore("address")) //g.ApplyBasic(model.User{}, g.GenerateModel("company"), g.GenerateModelAs("people", "Person", gen.FieldIgnore("address"))) //g.WithNewTagNameStrategy(func(c string) string { // return fmt.Sprintf("c:\"%s\"", c) //}) //g.WithJSONTagNameStrategy(func(string) string { return "" }) b := true //b = false if b { //g.ApplyBasic(g.GenerateAllTable( // gen.FieldGORMTag("create_at", "->"), // gen.FieldNewTag("create_at", "c:\"-\""), // gen.FieldGORMTag("update_at", "->"), // gen.FieldNewTag("update_at", "c:\"-\""), //)...) tableList, err := db.Migrator().GetTables() if err != nil { panic(fmt.Errorf("get all tables fail: %w", err)) } var tableModels []any reg := regexp.MustCompile(`.*_copy[0-9]+`) for _, tableName := range tableList { if tableName == "ienv_history" || reg.MatchString(tableName) { continue } tableModels = append(tableModels, g.GenerateModel(tableName, gen.FieldGORMTag("create_at", "->"), gen.FieldNewTag("create_at", "c:\"-\""), gen.FieldGORMTag("update_at", "->"), gen.FieldNewTag("update_at", "c:\"-\""), gen.FieldTypeJsonTag("int64", ",string"), )) } g.ApplyBasic(tableModels...) } else { g.ApplyBasic( g.GenerateModel("sys_usr", gen.FieldGORMTag("create_at", "->"), gen.FieldNewTag("create_at", "c:\"-\""), gen.FieldGORMTag("update_at", "->"), gen.FieldNewTag("update_at", "c:\"-\""), ), ) } // apply diy interfaces on structs or table models // 如果想给某些表或者model生成自定义方法,可以用ApplyInterface,第一个参数是方法接口,可以参考DIY部分文档定义 //g.ApplyInterface(func(method model.Method) {}, model.User{}, g.GenerateModel("company")) // execute the action of code generation g.Execute() }