44 lines
618 B
Go
44 lines
618 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"easywish/config"
|
|
)
|
|
|
|
type DbContext interface {
|
|
Close()
|
|
}
|
|
|
|
type dbContextImpl struct {
|
|
Pool *pgxpool.Pool
|
|
Context context.Context
|
|
}
|
|
|
|
func NewDbContext() DbContext {
|
|
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, config.GetConfig().DatabaseUrl)
|
|
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
if err := pool.Ping(context.Background()); err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
return &dbContextImpl{
|
|
Pool: pool,
|
|
Context: ctx,
|
|
}
|
|
}
|
|
|
|
// Close implements DbContext.
|
|
func (d *dbContextImpl) Close() {
|
|
d.Pool.Close()
|
|
}
|
|
|