teldrive/database/database.go
2023-08-16 03:23:02 +05:30

56 lines
1,016 B
Go

package database
import (
"time"
"github.com/divyam234/teldrive/utils"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
var DB *gorm.DB
func InitDB() {
var err error
// newLogger := logger.New(
// log.New(os.Stdout, "\r\n", log.LstdFlags),
// logger.Config{
// SlowThreshold: time.Second,
// LogLevel: logger.Silent,
// IgnoreRecordNotFoundError: true,
// ParameterizedQueries: true,
// Colorful: false,
// },
// )
DB, err = gorm.Open(postgres.Open(utils.GetConfig().DatabaseUrl), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "teldrive.",
SingularTable: false,
},
PrepareStmt: false,
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
if err != nil {
panic(err)
}
sqlDB, err := DB.DB()
if err != nil {
panic(err)
}
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(100)
sqlDB.SetConnMaxLifetime(time.Hour)
go func() {
DB.Exec("select 1")
}()
}