feat: dbcontext
This commit is contained in:
43
backend/internal/database/dbContext.go
Normal file
43
backend/internal/database/dbContext.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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()
|
||||
}
|
||||
|
||||
13
backend/internal/database/setup.go
Normal file
13
backend/internal/database/setup.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Module("database",
|
||||
fx.Provide(
|
||||
NewDbContext,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user