// src/db.ts
import { Pool, type QueryResultRow } from "pg";
import { ENV } from "./env.js";

export const pool = new Pool({ connectionString: ENV.POSTGRES_URL });

/**
 * Typed query helper.
 * Usage: const rows = await query<{ id: string }>("SELECT id FROM t");
 */
export async function query<T extends QueryResultRow = QueryResultRow>(
  text: string,
  params?: unknown[]
): Promise<T[]> {
  const res = await (pool.query as <R extends QueryResultRow = QueryResultRow>(
    q: string,
    p?: unknown[]
  ) => Promise<{ rows: R[] }>)<T>(text, params);
  return res.rows as T[];
}



