added postgres conn pool
This commit is contained in:
parent
0eddac604b
commit
0fdc2400ae
@ -2,8 +2,8 @@ package repos
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/src/core/models"
|
"backend/src/core/models"
|
||||||
|
"backend/src/integrations"
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ActionTokenRepo interface {
|
type ActionTokenRepo interface {
|
||||||
@ -11,14 +11,14 @@ type ActionTokenRepo interface {
|
|||||||
PopActionToken(ctx context.Context, userId, value string, target models.ActionTokenTarget) (*models.ActionTokenDTO, error)
|
PopActionToken(ctx context.Context, userId, value string, target models.ActionTokenTarget) (*models.ActionTokenDTO, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewActionTokenRepo(db *sql.DB) ActionTokenRepo {
|
func NewActionTokenRepo(db integrations.SqlDB) ActionTokenRepo {
|
||||||
return &actionTokenRepo{
|
return &actionTokenRepo{
|
||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type actionTokenRepo struct {
|
type actionTokenRepo struct {
|
||||||
db *sql.DB
|
db integrations.SqlDB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *actionTokenRepo) CreateActionToken(ctx context.Context, dto models.ActionTokenDTO) (*models.ActionTokenDTO, error) {
|
func (a *actionTokenRepo) CreateActionToken(ctx context.Context, dto models.ActionTokenDTO) (*models.ActionTokenDTO, error) {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package repos
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/src/core/models"
|
"backend/src/core/models"
|
||||||
|
"backend/src/integrations"
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
@ -21,12 +22,12 @@ type UserRepo interface {
|
|||||||
GetUserByEmail(ctx context.Context, login string) (*models.UserDTO, error)
|
GetUserByEmail(ctx context.Context, login string) (*models.UserDTO, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserRepo(db *sql.DB) UserRepo {
|
func NewUserRepo(db integrations.SqlDB) UserRepo {
|
||||||
return &userRepo{db}
|
return &userRepo{db}
|
||||||
}
|
}
|
||||||
|
|
||||||
type userRepo struct {
|
type userRepo struct {
|
||||||
db *sql.DB
|
db integrations.SqlDB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userRepo) CreateUser(ctx context.Context, dto models.UserDTO) (*models.UserDTO, error) {
|
func (u *userRepo) CreateUser(ctx context.Context, dto models.UserDTO) (*models.UserDTO, error) {
|
||||||
|
|||||||
@ -3,23 +3,65 @@ package integrations
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"github.com/jackc/pgx/stdlib"
|
"github.com/jackc/pgx/v5/stdlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: wrapper, connection pool
|
type SqlDB interface {
|
||||||
func NewPostgresConn(ctx context.Context, connUrl string) (*sql.DB, error) {
|
Begin() (*sql.Tx, error)
|
||||||
connConf, err := pgx.ParseConnectionString(connUrl)
|
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
||||||
|
Close() error
|
||||||
|
Conn(ctx context.Context) (*sql.Conn, error)
|
||||||
|
Driver() driver.Driver
|
||||||
|
Exec(query string, args ...any) (sql.Result, error)
|
||||||
|
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||||
|
Ping() error
|
||||||
|
PingContext(ctx context.Context) error
|
||||||
|
Prepare(query string) (*sql.Stmt, error)
|
||||||
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
||||||
|
Query(query string, args ...any) (*sql.Rows, error)
|
||||||
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
||||||
|
QueryRow(query string, args ...any) *sql.Row
|
||||||
|
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
||||||
|
SetConnMaxIdleTime(d time.Duration)
|
||||||
|
SetConnMaxLifetime(d time.Duration)
|
||||||
|
SetMaxIdleConns(n int)
|
||||||
|
SetMaxOpenConns(n int)
|
||||||
|
Stats() sql.DBStats
|
||||||
|
}
|
||||||
|
|
||||||
|
type Postgres struct {
|
||||||
|
*sql.DB
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPostgresConn(ctx context.Context, postgresUrl string) (SqlDB, error) {
|
||||||
|
connUrl, err := url.Parse(postgresUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed parsing postgres connection string: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlDb := stdlib.OpenDB(connConf)
|
query, _ := url.ParseQuery(connUrl.RawQuery)
|
||||||
if err := sqlDb.PingContext(ctx); err != nil {
|
query.Set("pool_max_conns", "16")
|
||||||
|
connUrl.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, connUrl.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
db := stdlib.OpenDBFromPool(pool)
|
||||||
|
if err := db.PingContext(ctx); err != nil {
|
||||||
return nil, fmt.Errorf("failed pinging postgres db: %v", err)
|
return nil, fmt.Errorf("failed pinging postgres db: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlDb, nil
|
return &Postgres{
|
||||||
|
DB: db,
|
||||||
|
pool: pool,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user