初始化
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/internal/config"
|
||||
"bj_power_wms/internal/db"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
EntClient *ent.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
entClient := db.MustNewDB(db.DatabaseConf{
|
||||
Host: c.Postgres.Host,
|
||||
Port: c.Postgres.Port,
|
||||
User: c.Postgres.User,
|
||||
Password: c.Postgres.Password,
|
||||
Dbname: c.Postgres.DBName,
|
||||
Debug: false,
|
||||
})
|
||||
|
||||
// 启动时自动建表
|
||||
if err := db.AutoMigrate(entClient); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 首次启动预置基础数据
|
||||
if err := db.SeedIfEmpty(entClient); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
EntClient: entClient,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user