switch to pgx driver + create merch
This commit is contained in:
parent
546fe13107
commit
97f8d27430
13 changed files with 289 additions and 69 deletions
|
|
@ -1,9 +1,10 @@
|
|||
package dbase
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"context"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/jackc/pgx"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Deps struct {
|
||||
|
|
@ -14,20 +15,24 @@ type Deps struct {
|
|||
DBName string
|
||||
}
|
||||
|
||||
func Connect(deps Deps) (*sql.DB, error) {
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
func ConnectPool(ctx context.Context, deps Deps) (*pgxpool.Pool, error) {
|
||||
connString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s",
|
||||
deps.Host, deps.Port, deps.Username, deps.Password, deps.DBName)
|
||||
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
config, err := pgxpool.ParseConfig(connString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = db.Ping(); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("failed to connect to DB: %w", err)
|
||||
pool, err := pgxpool.NewWithConfig(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
if err = pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue