diff --git a/orm.go b/orm.go new file mode 100644 index 0000000..39c1eb9 --- /dev/null +++ b/orm.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "zero-files/api/local/internal/config" + + OrmStructs "zero-files/api/local/orm/structs" + + "github.com/zeromicro/go-zero/core/conf" + "gorm.io/driver/mysql" + "gorm.io/gen" + "gorm.io/gorm" +) + +func main() { + var configFile = flag.String("f", "../etc/local-api.yaml", "the config file") + flag.Parse() + var c config.Config + conf.MustLoad(*configFile, &c) + db, err := gorm.Open(mysql.New(mysql.Config{ + DSN: c.MySQLConn, // DSN data source name + }), &gorm.Config{}) + if err != nil { + panic(err) + } + // 自动迁移 + err = db.AutoMigrate(&OrmStructs.File{}) + if err != nil { + panic(err) + } + + // 自动迁移 + err = db.AutoMigrate(&OrmStructs.Path{}) + if err != nil { + panic(err) + } + + // 配置生成器 + g := gen.NewGenerator(gen.Config{ + OutPath: "./dal", + Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode + }) + // 使用数据库 + g.UseDB(db) + + g.ApplyBasic( + // 生成所有表 + g.GenerateAllTable()..., + ) + // 执行 + g.Execute() +}