mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 13:16:04 +03:00
Neur0toxine
d15ed7ffec
* lint stage for a workflow * golangci-lint config * lint only new code or last commit * run lint only for pull requests
43 lines
808 B
Go
43 lines
808 B
Go
package core
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
// PostgreSQL is an default.
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
)
|
|
|
|
// ORM struct.
|
|
type ORM struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// NewORM will init new database connection.
|
|
func NewORM(config DatabaseConfig) *ORM {
|
|
orm := &ORM{}
|
|
orm.createDB(config)
|
|
return orm
|
|
}
|
|
|
|
func (orm *ORM) createDB(config DatabaseConfig) {
|
|
db, err := gorm.Open("postgres", config.Connection)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
db.DB().SetConnMaxLifetime(time.Duration(config.ConnectionLifetime) * time.Second)
|
|
db.DB().SetMaxOpenConns(config.MaxOpenConnections)
|
|
db.DB().SetMaxIdleConns(config.MaxIdleConnections)
|
|
|
|
db.SingularTable(true)
|
|
db.LogMode(config.Logging)
|
|
|
|
orm.DB = db
|
|
}
|
|
|
|
// CloseDB close database connection.
|
|
func (orm *ORM) CloseDB() {
|
|
_ = orm.DB.Close()
|
|
}
|