39 lines
929 B
Go
39 lines
929 B
Go
// Package database implements postgres connection and queries.
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
"github.com/uptrace/bun/driver/pgdriver"
|
|
"github.com/uptrace/bun/extra/bundebug"
|
|
)
|
|
|
|
// DBConn returns a postgres connection pool.
|
|
func DBConn() (*bun.DB, error) {
|
|
viper.SetDefault("db_dsn", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
|
|
|
|
dsn := viper.GetString("db_dsn")
|
|
|
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
|
|
|
db := bun.NewDB(sqldb, pgdialect.New())
|
|
|
|
if err := checkConn(db); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if viper.GetBool("db_debug") {
|
|
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func checkConn(db *bun.DB) error {
|
|
var n int
|
|
return db.NewSelect().ColumnExpr("1").Scan(context.Background(), &n)
|
|
}
|