summaryrefslogtreecommitdiff
path: root/api/db/_updateAuthenticator.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/db/_updateAuthenticator.ts')
-rw-r--r--api/db/_updateAuthenticator.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/api/db/_updateAuthenticator.ts b/api/db/_updateAuthenticator.ts
new file mode 100644
index 0000000..45d094c
--- /dev/null
+++ b/api/db/_updateAuthenticator.ts
@@ -0,0 +1,23 @@
+import type Authenticator from '../types/Authenticator';
+
+import { sql } from '@vercel/postgres';
+import { AUTHENTICATORS } from './_tables';
+
+export default async function updateAuthenticator<
+ T extends keyof Omit<Authenticator, 'id'>
+>(id: string, updatedFields: Pick<Authenticator, T>): Promise<void> {
+ try {
+ const assignments = Object.entries(updatedFields)
+ .filter(([key]) => key !== 'id') // never update the ID
+ .map(([key, value]) => `${key}=${value}`)
+ .join(', ');
+
+ await sql.query(`
+ UPDATE ${AUTHENTICATORS.name}
+ SET ${assignments}
+ WHERE ${AUTHENTICATORS.fields.id} = '${id}';
+ `);
+ } catch (err) {
+ throw new Error(`Failed to update authenticator with ID ${id} in database.`, err);
+ }
+}