github.com/retailcrm/mg-bot-helper@v0.0.0-20201229112329-a17255681a84/src/orm.go (about) 1 package main 2 3 import ( 4 "time" 5 6 "github.com/jinzhu/gorm" 7 _ "github.com/jinzhu/gorm/dialects/postgres" 8 ) 9 10 // Orm struct 11 type Orm struct { 12 DB *gorm.DB 13 } 14 15 // NewDb init new database connection 16 func NewDb(config *BotConfig) *Orm { 17 db, err := gorm.Open("postgres", config.Database.Connection) 18 if err != nil { 19 panic(err) 20 } 21 22 db.DB().SetConnMaxLifetime(time.Duration(config.Database.ConnectionLifetime) * time.Second) 23 db.DB().SetMaxOpenConns(config.Database.MaxOpenConnections) 24 db.DB().SetMaxIdleConns(config.Database.MaxIdleConnections) 25 26 db.SingularTable(true) 27 db.LogMode(config.Database.Logging) 28 29 return &Orm{ 30 DB: db, 31 } 32 } 33 34 // Close connection 35 func (orm *Orm) Close() { 36 orm.DB.Close() 37 }