Files
sqlc_example/backend/internal/db/connection.go
2025-10-27 14:22:19 +03:00

29 lines
482 B
Go

package db
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"go.uber.org/fx"
)
func NewDBConnection(lc fx.Lifecycle) *pgxpool.Pool {
pool, err := pgxpool.New(context.Background(), "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable");
if err != nil {
panic(err.Error())
}
if err := pool.Ping(context.Background()); err != nil {
pool.Close()
panic(err.Error())
}
lc.Append(
fx.StopHook(func() {
pool.Close()
}),
)
return pool
}