package models import ( "dorisbot/pkg/config" "errors" "fmt" "log" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) // Errors var ( ErrDBNotInit = errors.New("database not init") ) var x *sqlx.DB // NewDB - func NewDB(conf *config.Database) (*sqlx.DB, error) { var err error connStr := fmt.Sprintf("dbname=%s user=%s password=%s host=%s port=%d sslmode=disable", conf.DBName, conf.User, conf.Pass, conf.Host, conf.Port, ) log.Println("DB Connstr :: ", connStr) x, err = sqlx.Connect("postgres", connStr) if err != nil { return nil, err } return x, nil } // Ping database connection func Ping() error { if x == nil { return ErrDBNotInit } return x.Ping() } // GetConn database connection func GetConn() *sqlx.DB { if x == nil { panic(errors.New("database object not init")) } return x }