package ent import ( "context" "fmt" "github.com/jackc/pgx/v5/pgxpool" sharedpostgres "knowfoolery/backend/shared/infra/database/postgres" ) // Client wraps the pgx pool used by repository implementations. type Client struct { Pool *pgxpool.Pool } // NewClient creates a persistence client and verifies connectivity. func NewClient(ctx context.Context, cfg sharedpostgres.Config) (*Client, error) { poolCfg, err := pgxpool.ParseConfig(cfg.URL()) if err != nil { return nil, fmt.Errorf("parse postgres config: %w", err) } poolCfg.MaxConns = clampIntToInt32(cfg.MaxOpenConns) poolCfg.MinConns = clampIntToInt32(cfg.MaxIdleConns) poolCfg.MaxConnLifetime = cfg.ConnMaxLifetime poolCfg.MaxConnIdleTime = cfg.ConnMaxIdleTime pool, err := pgxpool.NewWithConfig(ctx, poolCfg) if err != nil { return nil, fmt.Errorf("create postgres pool: %w", err) } if err := pool.Ping(ctx); err != nil { pool.Close() return nil, fmt.Errorf("ping postgres: %w", err) } return &Client{Pool: pool}, nil } // Close closes the underlying pool. func (c *Client) Close() { if c != nil && c.Pool != nil { c.Pool.Close() } } func clampIntToInt32(v int) int32 { const maxInt32 = int(^uint32(0) >> 1) if v <= 0 { return 0 } if v > maxInt32 { return int32(maxInt32) } return int32(v) }