temp
This commit is contained in:
		@@ -1,17 +1,26 @@
 | 
			
		||||
package db
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"context"
 | 
			
		||||
	"sipro-mps/internal/config"
 | 
			
		||||
 | 
			
		||||
	"github.com/jackc/pgx/v5/pgxpool"
 | 
			
		||||
	_ "github.com/lib/pq"
 | 
			
		||||
	"go.uber.org/fx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func NewConnection(dsn string) (*sql.DB, error) {
 | 
			
		||||
	db, err := sql.Open("postgres", dsn)
 | 
			
		||||
func NewPgxPool(lc fx.Lifecycle, config config.Config) (*pgxpool.Pool, error) {
 | 
			
		||||
	ctx := context.Background()
 | 
			
		||||
	pool, err := pgxpool.New(ctx, config.Database.URL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if err := db.Ping(); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return db, nil
 | 
			
		||||
	lc.Append(fx.Hook{
 | 
			
		||||
		OnStop: func(ctx context.Context) error {
 | 
			
		||||
			pool.Close()
 | 
			
		||||
			return nil
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return pool, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user