mirror of
https://gitee.com/blackfox/geekai.git
synced 2024-12-11 07:40:08 +08:00
37 lines
763 B
Go
37 lines
763 B
Go
|
package store
|
||
|
|
||
|
import (
|
||
|
"chatplus/core/types"
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/gorm/logger"
|
||
|
"gorm.io/gorm/schema"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func NewGormConfig() *gorm.Config {
|
||
|
return &gorm.Config{
|
||
|
Logger: logger.Default.LogMode(logger.Warn),
|
||
|
NamingStrategy: schema.NamingStrategy{
|
||
|
TablePrefix: "chatgpt_", // 设置表前缀
|
||
|
SingularTable: false, // 使用单数表名形式
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewMysql(config *gorm.Config, appConfig *types.AppConfig) (*gorm.DB, error) {
|
||
|
db, err := gorm.Open(mysql.Open(appConfig.MysqlDns), config)
|
||
|
|
||
|
sqlDB, err := db.DB()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
sqlDB.SetMaxIdleConns(32)
|
||
|
sqlDB.SetMaxOpenConns(512)
|
||
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return db, nil
|
||
|
}
|