You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package utils
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"log"
"time"
)
// InitDB 初始化数据库连接
func InitDB() {
// 1. 配置
logLevel := gormLogger.Warn
// 根据应用的mod控制级别
switch gin.Mode() {
case gin.ReleaseMode:
logLevel = gormLogger.Warn
case gin.TestMode, gin.DebugMode:
fallthrough
default:
// 最多的日志
logLevel = gormLogger.Info
}
// db 日志
unionLogger := gormLogger.New(
log.New(LogWriter(), "\n", log.LstdFlags),
gormLogger.Config{
SlowThreshold: time.Second,
Colorful: false,
IgnoreRecordNotFoundError: false,
ParameterizedQueries: false,
LogLevel: logLevel,
},
)
// gorm 连接配置
conf := &gorm.Config{
SkipDefaultTransaction: false,
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 单数表名
}, // 数据表命名策略
FullSaveAssociations: false,
Logger: unionLogger,
NowFunc: nil,
DryRun: false,
PrepareStmt: false,
DisableAutomaticPing: false,
DisableForeignKeyConstraintWhenMigrating: true, // 数据表迁移时禁用外键约束
IgnoreRelationshipsWhenMigrating: false,
DisableNestedTransaction: false,
AllowGlobalUpdate: false,
QueryFields: false,
CreateBatchSize: 0,
TranslateError: false,
PropagateUnscoped: false,
ClauseBuilders: nil,
ConnPool: nil,
Dialector: nil,
Plugins: nil,
}
// 2. 创建db对象
dsn := viper.GetString("db.dsn")
if dbNew, err := gorm.Open(mysql.Open(dsn), conf); err != nil {
log.Fatalln(err)
} else {
db = dbNew
}
}
// 全局的db对象
var db *gorm.DB
// DB 全局访问db对象的方法
func DB() *gorm.DB {
return db
}