mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-21 14:44:45 +01:00
34 lines
866 B
Go
34 lines
866 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
"github.com/uptrace/bun/driver/pgdriver"
|
|
"github.com/uptrace/bun/extra/bundebug"
|
|
)
|
|
|
|
func Open(dsn string) (*bun.DB, error) {
|
|
var db *bun.DB
|
|
switch {
|
|
case strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") || strings.HasPrefix(dsn, "unix://"):
|
|
dbConn := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
|
db = bun.NewDB(dbConn, pgdialect.New())
|
|
default:
|
|
return nil, fmt.Errorf("Invalid database connection string %s, only (postgres|postgresql|unix):// is supported", dsn)
|
|
}
|
|
|
|
db.AddQueryHook(bundebug.NewQueryHook(
|
|
// disable the hook
|
|
bundebug.WithEnabled(false),
|
|
// BUNDEBUG=1 logs failed queries
|
|
// BUNDEBUG=2 logs all queries
|
|
bundebug.FromEnv("BUNDEBUG"),
|
|
))
|
|
|
|
return db, nil
|
|
}
|