32 lines
706 B
Go
32 lines
706 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"strings"
|
|
)
|
|
|
|
var pool *pgxpool.Pool
|
|
|
|
func GetConnectionPool() *pgxpool.Pool {
|
|
return pool
|
|
}
|
|
|
|
func InitPool(ctx context.Context, connString string) error {
|
|
if connString == "" || !strings.HasPrefix(connString, "postgres://") {
|
|
return fmt.Errorf("invalid connection string")
|
|
}
|
|
var err error
|
|
pool, err = pgxpool.New(ctx, connString)
|
|
return err
|
|
}
|
|
|
|
type Repository[T any] interface {
|
|
Create(ctx context.Context, entity *T) error
|
|
FindByID(ctx context.Context, id string) (*T, error)
|
|
Update(ctx context.Context, entity *T) error
|
|
Delete(ctx context.Context, id string) error
|
|
FindAll(ctx context.Context) ([]*T, error)
|
|
}
|