db.go 951 B

123456789101112131415161718192021222324252627282930313233
  1. // Package repository реализует слой доступа к данным (PostgreSQL через pgxpool).
  2. // Предоставляет репозитории: UserRepo, RefreshTokenRepo, PlaceRepo, BookingRepo,
  3. // ReviewRepo, ServiceRepo, TagRepo, FeatureRepo.
  4. // Содержит функции для CRUD, фильтрации, работы с тегами/фичами мест и услуг.
  5. package repository
  6. import (
  7. "context"
  8. "fmt"
  9. "github.com/jackc/pgx/v5/pgxpool"
  10. )
  11. func NewPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
  12. cfg, err := pgxpool.ParseConfig(databaseURL)
  13. if err != nil {
  14. return nil, fmt.Errorf("parse config: %w", err)
  15. }
  16. cfg.MaxConns = 20
  17. cfg.MinConns = 2
  18. pool, err := pgxpool.NewWithConfig(ctx, cfg)
  19. if err != nil {
  20. return nil, fmt.Errorf("create pool: %w", err)
  21. }
  22. if err := pool.Ping(ctx); err != nil {
  23. return nil, fmt.Errorf("ping db: %w", err)
  24. }
  25. return pool, nil
  26. }