blob: 55c4b2f197958f9a2a5f62c412ff021652fdc867 (
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
|
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>> {
try {
const result = await sql.query(`
SELECT ${fields.join(', ')}
FROM ${AUTHENTICATORS.name}
WHERE ${AUTHENTICATORS.fields.id} = '${id}';
`);
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.`, err);
}
}
|