Go 实现 Database Seeder
定义 Seeder 接口⌗
package models
type (
Seeder interface {
Seed() error
}
Model interface {
Store() error
Update(id string) error
}
)
定义模型⌗
type Menu struct {
Code string `xorm:"not null pk comment('编码') VARCHAR(255)"`
ParentCode string `xorm:"not null comment('父编码') index VARCHAR(255)"`
PermissionCode string `xorm:"not null comment('权限编码') index VARCHAR(255)"`
Name string `xorm:"not null comment('名称') VARCHAR(255)"`
Route string `xorm:"not null comment('前端路由') VARCHAR(255)"`
Icon string `xorm:"not null comment('图标') VARCHAR(255)"`
}
// 定义模型的数据表名称
func (menu *Menu) TableName() string {
return "menus"
}
// 数据填充器
func (menu *Menu) Seed() error {
return menu.Store()
}
// 创建菜单
func (menu *Menu) Store() error {
_, err := Engine.Insert(menu)
return err
}
定义 Seeder 注册接口和 Seeder 结构⌗
package seeds
import (
"github.com/betterde/ects/models"
"log"
)
type (
SeederInterface interface {
Register(seeder *Seeder)
}
Seeder struct {
instances []models.Seeder
}
)
func (seeder *Seeder) Run() error {
seeders := []SeederInterface{
&MenuSeeder{},
&PermissionSeeder{},
}
for _, item := range seeders {
item.Register(seeder)
}
for _, instalce := range seeder.instances {
if err := instalce.Seed(); err != nil {
log.Println(err)
}
}
return nil
}
实现模型的填充方法⌗
实现菜单模型的 Register
方法,
package seeds
import "github.com/betterde/ects/models"
type (
MenuSeeder struct {
}
)
func (menus *MenuSeeder) Register(seeder *Seeder) {
items := []models.Seeder{
&models.Menu{
Code: "dashboard",
ParentCode: "",
PermissionCode: "dashboard",
Name: "概览",
Route: "/dashboard",
Icon: "",
},
&models.Menu{
Code: "task",
ParentCode: "",
PermissionCode: "task",
Name: "任务",
Route: "/task",
Icon: "",
},
&models.Menu{
Code: "worker",
ParentCode: "",
PermissionCode: "worker",
Name: "节点",
Route: "/worker",
Icon: "",
},
&models.Menu{
Code: "team",
ParentCode: "",
PermissionCode: "team",
Name: "团队",
Route: "/team",
Icon: "",
},
&models.Menu{
Code: "user",
ParentCode: "",
PermissionCode: "user",
Name: "用户",
Route: "/user",
Icon: "",
},
&models.Menu{
Code: "log",
ParentCode: "",
PermissionCode: "log",
Name: "日志",
Route: "/log",
Icon: "",
},
&models.Menu{
Code: "record",
ParentCode: "",
PermissionCode: "record",
Name: "记录",
Route: "/record",
Icon: "",
},
&models.Menu{
Code: "setting",
ParentCode: "",
PermissionCode: "setting",
Name: "设置",
Route: "/setting",
Icon: "",
},
}
seeder.instances = append(seeder.instances, items...)
}
在服务启动时调用⌗
seedService := seeds.Seeder{}
if err := seedService.Run(); err != nil {
return response.InternalServerError("Failed to seed system data", err)
}
I hope this is helpful, Happy hacking…