import { Pool } from 'pg'; export const pool = new Pool({ host: process.env.PGHOST, port: parseInt(process.env.PGPORT || '5432', 10), user: process.env.PGUSER, password: process.env.PGPASSWORD, database: process.env.PGDATABASE }); /** * The single row a query is guaranteed to have returned. * * For `INSERT ... RETURNING` and `UPDATE ... WHERE id = $1 RETURNING` after the * row's existence has already been established: Postgres returns exactly one * row, so there is nothing to branch on, but `noUncheckedIndexedAccess` is right * that `rows[0]` is `T | undefined` and the compiler cannot know better. * * A thrown error rather than a non-null assertion. If the assumption is ever * wrong the assertion would hand `undefined` to the next line and fail somewhere * unrelated, whereas this fails here and says which query. `asyncRoute` turns it * into a 500, which is the right answer for "the database did not do what the * statement says it does". * * Reads that legitimately might find nothing do not use this — they destructure * and branch, so the check and the use are the same thing. */ export function requireRow(rows: T[], what: string): T { const row = rows[0]; if (!row) { throw new Error(`expected ${what} to return a row, got none`); } return row; }