api-base/database/database.go

34 lines
480 B
Go
Raw Normal View History

2020-08-15 17:21:27 +00:00
package database
import (
"go-api/pkg/config"
"github.com/jmoiron/sqlx"
)
var x *sqlx.DB
func Init(conf *config.Config) error {
var err error
x, err = sqlx.Connect("postgres", conf.Database.DSN)
if err != nil {
return err
}
x.SetMaxIdleConns(int(conf.Database.IdleConn))
x.SetMaxOpenConns(int(conf.Database.MaxConn))
if err := x.Ping(); err != nil {
return err
}
return nil
}
func Get() *sqlx.DB {
if x == nil {
panic("database not init")
}
return x
}