40 lines
775 B
Go
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()
|
||
|
}
|