34 lines
480 B
Go
34 lines
480 B
Go
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
|
|
}
|