From b42bfa3abcd29cb977fbdc41a02d9f7f1ffeb1a2 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Fri, 21 Feb 2025 18:18:38 +0100 Subject: chore: Rename db{Conn => Client} --- app/src/lib/server/assignments.ts | 22 +++++++++++----------- app/src/lib/server/db.ts | 2 +- app/src/lib/server/sessions.ts | 23 +++++++++++++---------- app/src/lib/server/users.ts | 4 ++-- 4 files changed, 27 insertions(+), 24 deletions(-) (limited to 'app/src/lib/server') diff --git a/app/src/lib/server/assignments.ts b/app/src/lib/server/assignments.ts index 787865c..c66f25c 100644 --- a/app/src/lib/server/assignments.ts +++ b/app/src/lib/server/assignments.ts @@ -5,11 +5,11 @@ import type { CemetaryPlot, Assignment, AssignmentState } from "../common/assign /** * Retrieves all assignments for the given user. * - * @param dbConn Connection to database. + * @param dbClient Connection to database. * @param userId ID used to identify user. */ -export async function getAssignments(dbConn: ClientBase, userId: number): Promise { - const result = await dbConn.query( +export async function getAssignments(dbClient: ClientBase, userId: number): Promise { + const result = await dbClient.query( "SELECT * FROM assignments WHERE gardener_id = $1 ORDER BY date", [userId], ); @@ -33,7 +33,7 @@ type GetAssignmentResult = * Retrieves a specfic assignment, along with relevant cemetary plot. */ export async function getAssignmentAndCemetaryById( - dbConn: ClientBase, + dbClient: ClientBase, assignmentId: number, ): Promise { const queryText = `SELECT @@ -51,7 +51,7 @@ export async function getAssignmentAndCemetaryById( INNER JOIN cemetary_plots AS C ON c.id = a.cemetary_plot_id WHERE c.id = $1`; - const result = await dbConn.query({ rowMode: "array", text: queryText }, [assignmentId]); + const result = await dbClient.query({ rowMode: "array", text: queryText }, [assignmentId]); if (result.rowCount == 0) { return { assignment: null, cemetaryPlot: null }; } @@ -80,7 +80,7 @@ export interface FinishAssignmentArgs { // TODO: Error recovery. export async function finishAssignment( - dbConn: ClientBase, + dbClient: ClientBase, s3Client: S3Client, { images, note, assignmentId }: FinishAssignmentArgs, ): Promise { @@ -103,13 +103,13 @@ export async function finishAssignment( // TODO: Add beanstalkd job - await dbConn.query("BEGIN"); + await dbClient.query("BEGIN"); try { // Create 'images' row for each image. // FIXME: Apparently node-pg doesn't have an equivalent to Python's `insert_many`?? for (const image of uploadedImages) { - dbConn.query({ + dbClient.query({ name: "insert-assignment-image", text: "INSERT INTO images(s3_path, original_filename, assignment_id) VALUES ($1, $2, $3)", values: [image.key, image.name, assignmentId], @@ -117,7 +117,7 @@ export async function finishAssignment( } // Update the assingment's state. - dbConn.query( + dbClient.query( `UPDATE assignments SET @@ -128,13 +128,13 @@ export async function finishAssignment( [note, assignmentId], ); - dbConn.query("COMMIT"); + dbClient.query("COMMIT"); } catch (err) { // We should probably try to delete S3 objects. // We should probably try to delete the job. - await dbConn.query("ROLLBACK"); + await dbClient.query("ROLLBACK"); throw err; } } diff --git a/app/src/lib/server/db.ts b/app/src/lib/server/db.ts index dcfcfd0..db001b7 100644 --- a/app/src/lib/server/db.ts +++ b/app/src/lib/server/db.ts @@ -15,6 +15,6 @@ pool.on("error", (err, client) => { console.error("Database error: ", err, client); }); -export function getDbConnection(): Promise { +export function getDbClient(): Promise { return pool.connect(); } diff --git a/app/src/lib/server/sessions.ts b/app/src/lib/server/sessions.ts index 69a8b46..502ab74 100644 --- a/app/src/lib/server/sessions.ts +++ b/app/src/lib/server/sessions.ts @@ -51,14 +51,14 @@ function encodeBase32LowerCaseNoPadding(input: Uint8Array): string { const VALID_MILLISECONDS = 1000 * 60 * 60 * 24 * 30; /** Creates a new session for the user with the given `userId`. */ -export async function createSession(dbConn: pg.ClientBase, userId: number): Promise { +export async function createSession(dbClient: pg.ClientBase, userId: number): Promise { const token = generateSessionToken(); const session: Session = { token: token, userId, expiresAt: new Date(Date.now() + VALID_MILLISECONDS), }; - await dbConn.query("INSERT INTO sessions(token, user_id, expires_at) VALUES ($1, $2, $3);", [ + await dbClient.query("INSERT INTO sessions(token, user_id, expires_at) VALUES ($1, $2, $3);", [ session.token, session.userId, session.expiresAt, @@ -78,11 +78,11 @@ export async function createSession(dbConn: pg.ClientBase, userId: number): Prom * @returns A session + user pair if the session is valid, or `null` for both otherwise. */ export async function validateSessionToken( - dbConn: pg.ClientBase, + dbClient: pg.ClientBase, token: string, ): Promise { // Step 1 - const result = await dbConn.query( + const result = await dbClient.query( `SELECT * FROM sessions INNER JOIN users ON users.id = sessions.user_id WHERE token = $1;`, @@ -110,7 +110,7 @@ export async function validateSessionToken( // Step 2. const now = Date.now(); if (now >= session.expiresAt.getTime()) { - await invalidateSession(dbConn, session.token); + await invalidateSession(dbClient, session.token); return { session: null, user: null }; } @@ -118,7 +118,7 @@ export async function validateSessionToken( // We only do this a bit into the period to avoid superflous database writes. if (now >= session.expiresAt.getTime() - VALID_MILLISECONDS / 2) { session.expiresAt = new Date(session.expiresAt.getTime() + VALID_MILLISECONDS / 2); - await dbConn.query("UPDATE sessions SET expires_at = ? WHERE id = ?;", [ + await dbClient.query("UPDATE sessions SET expires_at = ? WHERE id = ?;", [ session.expiresAt, session.token, ]); @@ -129,13 +129,16 @@ export async function validateSessionToken( /** Invalidates the session with token `sessionToken`. */ export async function invalidateSession( - dbConn: pg.ClientBase, + dbClient: pg.ClientBase, sessionToken: string, ): Promise { - await dbConn.query("DELETE sessions WHERE token = ?;", [sessionToken]); + await dbClient.query("DELETE sessions WHERE token = ?;", [sessionToken]); } /** Invalidates all sessions for the user with the id `userId`. */ -export async function invalidateAllSessions(dbConn: pg.ClientBase, userId: number): Promise { - await dbConn.query("DELETE sessions WHERE user_id = ?", [userId]); +export async function invalidateAllSessions( + dbClient: pg.ClientBase, + userId: number, +): Promise { + await dbClient.query("DELETE sessions WHERE user_id = ?", [userId]); } diff --git a/app/src/lib/server/users.ts b/app/src/lib/server/users.ts index a2e734f..6998c66 100644 --- a/app/src/lib/server/users.ts +++ b/app/src/lib/server/users.ts @@ -17,11 +17,11 @@ export interface User { * @returns The user, or `undefined` on authentication failure. */ export async function getUser( - dbConn: pg.ClientBase, + dbClient: pg.ClientBase, email: string, password: string, ): Promise { - let result = await dbConn.query( + let result = await dbClient.query( "SELECT * FROM users WHERE email = $1 AND password_hash = crypt($2, password_hash);", [email, password], ); -- cgit v1.2.3