package database import ( "context" "fmt" "git.toki-labs.com/toki/rara/packages/go/config" "git.toki-labs.com/toki/rara/packages/go/database/dbgen" "github.com/jackc/pgx/v5/pgxpool" "go.uber.org/fx" "go.uber.org/zap" ) type Store struct { Pool *pgxpool.Pool Queries *dbgen.Queries } func New(lifecycle fx.Lifecycle, cfg config.Config, logger *zap.Logger) (*Store, error) { poolConfig, err := pgxpool.ParseConfig(cfg.Database.URL) if err != nil { return nil, fmt.Errorf("parse database config: %w", err) } poolConfig.MaxConns = cfg.Database.MaxConnections poolConfig.MinConns = cfg.Database.MinConnections pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig) if err != nil { return nil, fmt.Errorf("create database pool: %w", err) } lifecycle.Append(fx.Hook{ OnStop: func(context.Context) error { logger.Debug("closing database pool") pool.Close() return nil }, }) return &Store{Pool: pool, Queries: dbgen.New(pool)}, nil } func (s *Store) Ready(ctx context.Context) error { return s.Pool.Ping(ctx) }