forked from mtusi2026/mtusi2026lab2
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
// Package store contains the PostgreSQL persistence layer. It uses plain
|
|
// parameterized SQL, one bounded pool and explicit transaction boundaries.
|
|
// Contract errors are returned as common.AppError so HTTP status mapping stays
|
|
// in one place.
|
|
//
|
|
// AGENT: Persistence was split from one large file by aggregate (users, chats,
|
|
// messages). Queries, transaction boundaries and error mapping are unchanged;
|
|
// only file organization and scan helpers changed.
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
timeout time.Duration
|
|
}
|
|
|
|
func New(pool *pgxpool.Pool, timeout time.Duration) *Store {
|
|
return &Store{pool: pool, timeout: timeout}
|
|
}
|
|
|
|
func (s *Store) withTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(ctx, s.timeout)
|
|
}
|
|
|
|
func (s *Store) Ping(ctx context.Context) error {
|
|
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
return s.pool.Ping(pingCtx)
|
|
}
|
|
|
|
// rowScanner is the common subset of pgx.Row and pgx.Rows used by scan helpers.
|
|
type rowScanner interface {
|
|
Scan(dest ...any) error
|
|
}
|