36 lines
992 B
Go
36 lines
992 B
Go
package config
|
|
|
|
import (
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func NewDB(dsn string) (*gorm.DB, error) {
|
|
// Configure GORM with custom settings
|
|
// The logger helps with debugging SQL queries during development
|
|
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
})
|
|
// if err != nil {
|
|
// log.Fatal("Failed to connect to database:", err)
|
|
// }
|
|
|
|
// // Get the underlying SQL database connection for pool configuration
|
|
// sqlDB, err := db.DB()
|
|
// if err != nil {
|
|
// log.Fatal("Failed to get database instance:", err)
|
|
// }
|
|
|
|
// // Configure connection pool settings
|
|
// // MaxIdleConns: number of connections kept open when idle
|
|
// sqlDB.SetMaxIdleConns(10)
|
|
// // MaxOpenConns: maximum total connections to the database
|
|
// sqlDB.SetMaxOpenConns(100)
|
|
// // ConnMaxLifetime: how long a connection can be reused
|
|
// sqlDB.SetConnMaxLifetime(time.Hour)
|
|
|
|
// slog.Info("Database connected successfully")
|
|
|
|
}
|