summaryrefslogtreecommitdiff
path: root/api/db/_getAuthenticator.ts
blob: e93023bb0aa3c713ee61b624786d3576b3ca5212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { sql } from '@vercel/postgres';
import { AUTHENTICATORS } from './_tables';
import Authenticator from '../types/Authenticator';

export default async function getAuthenticator<
  T extends keyof Authenticator
>(id: string, fields: T[]): Promise<Pick<Authenticator, T>> {
  const query = `
    SELECT ${fields.join(', ')}
    FROM ${AUTHENTICATORS.name}
    WHERE ${AUTHENTICATORS.fields.id} = '${id}';
  `;

  try {
    const result = await sql.query(query);

    const row = result.rows.at(0);
    if (!row) {
      throw new Error(`No authenticator with ID ${id} found in database.`);
    }

    const authenticator: Partial<Pick<Authenticator, T>> = {};
    for (const field in fields) {
      // Assume type conversion is already done by node-postgres
      authenticator[field] = row[AUTHENTICATORS.fields[field]];
    }
    return authenticator as Pick<Authenticator, T>;
  } catch (err) {
    throw new Error(`Failed to get authenticator with ID ${id} from database. Query was ${query.replace(/\s+/g, ' ')}. ${err}`);
  }
}