3 Star 5 Fork 3

三三物联网/ssiot-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
initialize.go 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
三三物联网 提交于 2023-04-03 00:22 . 重构
package database
import (
toolsConfig "gitee.com/sansaniot/ssiot-core/config"
"gitee.com/sansaniot/ssiot-core/facade/runtime"
log "gitee.com/sansaniot/ssiot-core/logger"
"gitee.com/sansaniot/ssiot-core/storage/database/dbconfig"
gormLogger "gitee.com/sansaniot/ssiot-core/storage/database/gorm/logger"
"gitee.com/sansaniot/ssiot-core/utils"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"time"
)
func SetupDatabase(host string, c *toolsConfig.Database) {
log.Infof("%s => %s", host, utils.Green(c.Source))
registers := make([]dbconfig.ResolverConfigure, len(c.Registers))
for i := range c.Registers {
registers[i] = dbconfig.NewResolverConfigure(
c.Registers[i].Sources,
c.Registers[i].Replicas,
c.Registers[i].Policy,
c.Registers[i].Tables)
}
resolverConfig := dbconfig.NewConfigure(c.Source, c.MaxIdleConns, c.MaxOpenConns, c.ConnMaxIdleTime, c.ConnMaxLifeTime, registers)
db, err := resolverConfig.Init(&gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: gormLogger.New(
logger.Config{
SlowThreshold: time.Second,
Colorful: true,
LogLevel: logger.LogLevel(
log.DefaultLogger.Options().Level.LevelForGorm()),
},
),
}, opens[c.Driver])
if err != nil {
log.Fatal(utils.Red(c.Driver+" connect error :"), err)
} else {
log.Info(utils.Green(c.Driver + " connect success !"))
}
runtime.Runtime.SetDb(host, db)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sansaniot/ssiot-core.git
git@gitee.com:sansaniot/ssiot-core.git
sansaniot
ssiot-core
ssiot-core
v1.7.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385