30 lines
548 B
Go
30 lines
548 B
Go
|
|
package database
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"go.b0esche.cloud/backend/internal/config"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
"github.com/jackc/pgx/v5/stdlib"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Connect(cfg *config.Config) (*sql.DB, error) {
|
||
|
|
ctx := context.Background()
|
||
|
|
|
||
|
|
pool, err := pgxpool.New(ctx, cfg.DatabaseURL)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to create pool: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := pool.Ping(ctx); err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
db := stdlib.OpenDBFromPool(pool)
|
||
|
|
|
||
|
|
return db, nil
|
||
|
|
}
|