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
|
||||
}
|
||||
|
||||
16
internal/db/fx.go
Normal file
16
internal/db/fx.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"sipro-mps/internal/marketplace/db"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Options(
|
||||
fx.Provide(
|
||||
fx.Annotate(
|
||||
NewPgxPool,
|
||||
fx.As(new(db.DBTX)),
|
||||
),
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user