1
0
mirror of synced 2024-11-25 13:16:02 +03:00
mg-transport-telegram/database.go
DmitryZagorulko 9636237212 minor fixes
2018-06-08 12:20:51 +03:00

40 lines
775 B
Go

package main
import (
"time"
"github.com/getsentry/raven-go"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
// Orm struct
type Orm struct {
DB *gorm.DB
}
// NewDb init new database connection
func NewDb(config *TransportConfig) *Orm {
db, err := gorm.Open("postgres", config.Database.Connection)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
panic(err)
}
db.DB().SetConnMaxLifetime(time.Duration(config.Database.ConnectionLifetime) * time.Second)
db.DB().SetMaxOpenConns(config.Database.MaxOpenConnections)
db.DB().SetMaxIdleConns(config.Database.MaxIdleConnections)
db.SingularTable(true)
db.LogMode(config.Database.Logging)
return &Orm{
DB: db,
}
}
// Close connection
func (orm *Orm) Close() {
orm.DB.Close()
}