From 95108ab9ebc2c2db5d77d92dbd280a427b3588fd Mon Sep 17 00:00:00 2001 From: April Hall Date: Wed, 5 Feb 2025 18:31:23 -0500 Subject: [PATCH] fix: Replace the bunch of db functions with a Db class --- src/lib/server/db/index.ts | 104 ++++++++++++++++++++--------------- src/lib/websocketConfig.ts | 6 +- src/routes/+layout.server.ts | 4 +- src/routes/+page.server.ts | 4 +- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/lib/server/db/index.ts b/src/lib/server/db/index.ts index d965504..43b77e9 100644 --- a/src/lib/server/db/index.ts +++ b/src/lib/server/db/index.ts @@ -1,54 +1,66 @@ import cassandra from 'cassandra-driver'; -async function createChannel(client: cassandra.Client, channelName: string) { - try { - await client.execute(` - CREATE TABLE IF NOT EXISTS channels.channel_${channelName} ( - id UUID, - message_content TEXT, - channel_name TEXT, - timestamp TIMESTAMP, - sender UUID, - PRIMARY KEY (channel_name, timestamp) - ) WITH CLUSTERING ORDER BY (timestamp DESC);`); - } catch (e) { - // @ts-expect-error I don't like this thing yelling at me - console.log(`Error creating new channel: ${e.message}`); - } -} +class Db { + private client: cassandra.Client; -async function storeMessage(client: cassandra.Client, channelName: string, content: string, sender: string, id: string) { - try { - const now = new Date(); - await client.execute(`INSERT INTO channels.channel_${channelName} (id, message_content, channel_name, timestamp, sender) - VALUES (${id}, '${content}', '${channelName}', ${now.getTime()}, ${sender})`); - } catch (e) { - // @ts-expect-error I don't like this thing yelling at me - console.log(`Error storing messages: ${e.message}`); + constructor(client: cassandra.Client) { + this.client = client; } -} -async function getChannels(client: cassandra.Client): Promise { - try { - const res = await client.execute(`SELECT table_name FROM system_schema.tables WHERE keyspace_name = 'channels'`); - return res.rows; - } catch (e) { - // @ts-expect-error I don't like this thing yelling at me - console.log(`Error fetching channels: ${e.message}`); - return; + // Create Channel Method + async createChannel(channelName: string) { + try { + await this.client.execute(` + CREATE TABLE IF NOT EXISTS channels.channel_${channelName} ( + id UUID, + message_content TEXT, + channel_name TEXT, + timestamp TIMESTAMP, + sender UUID, + PRIMARY KEY (channel_name, timestamp) + ) WITH CLUSTERING ORDER BY (timestamp DESC);`); + } catch (e) { + // @ts-expect-error I don't like this thing yelling at me + console.log(`Error creating new channel: ${e.message}`); + } } -} -async function getMessages(client: cassandra.Client, channelName: string, limit: number): Promise { - try { - const res = await client.execute( - `SELECT * FROM channels.channel_${channelName} WHERE channel_name = '${channelName}' ORDER BY timestamp DESC LIMIT ${limit}`, - ); - return res.rows; - } catch (e) { - // @ts-expect-error I don't like this thing yelling at me - console.log(`Error fetching messages: ${e.message}`); - return; + // Send message method + async sendMessage(channelName: string, content: string, sender: string, id: string) { + try { + const now = new Date(); + await this.client.execute(`INSERT INTO channels.channel_${channelName} (id, message_content, channel_name, timestamp, sender) + VALUES (${id}, '${content}', '${channelName}', ${now.getTime()}, ${sender})`); + } catch (e) { + // @ts-expect-error I don't like this thing yelling at me + console.log(`Error storing messages: ${e.message}`); + } + } + + // Get Channels method + async getChannels(): Promise { + try { + const res = await this.client.execute(`SELECT table_name FROM system_schema.tables WHERE keyspace_name = 'channels'`); + return res.rows; + } catch (e) { + // @ts-expect-error I don't like this thing yelling at me + console.log(`Error fetching channels: ${e.message}`); + return; + } + } + + // Get messages method + async getMessages(channelName: string, limit: number): Promise { + try { + const res = await this.client.execute( + `SELECT * FROM channels.channel_${channelName} WHERE channel_name = '${channelName}' ORDER BY timestamp DESC LIMIT ${limit}`, + ); + return res.rows; + } catch (e) { + // @ts-expect-error I don't like this thing yelling at me + console.log(`Error fetching messages: ${e.message}`); + return; + } } } @@ -76,4 +88,6 @@ try { process.exit(1); } -export { client, createChannel, getChannels, getMessages, storeMessage }; +const db = new Db(client); + +export { db }; diff --git a/src/lib/websocketConfig.ts b/src/lib/websocketConfig.ts index 73fc793..3dd9143 100644 --- a/src/lib/websocketConfig.ts +++ b/src/lib/websocketConfig.ts @@ -2,7 +2,7 @@ import { Server as SocketIOServer } from 'socket.io'; import type { HttpServer } from 'vite'; // Don't try to replace with $lib alias. Since this // file gets loaded as a vite plugin, it will crash -import { client, createChannel, storeMessage } from './server/db/'; +import { db } from './server/db/'; import { v4 as uuidv4 } from 'uuid'; let io: SocketIOServer | undefined; @@ -21,8 +21,8 @@ export function startupSocketIOServer(httpServer: HttpServer | null) { if (msg.content !== '') { console.log(`[ws:kit] message from ${socket.id}: ${msg.content}`); // Store the message in the database - await createChannel(client, '000'); - await storeMessage(client, '000', msg.content, msg.id, uuidv4()); + await db.createChannel('000'); + await db.sendMessage('000', msg.content, msg.id, uuidv4()); io!.emit('message', { user: msg.id, message: msg.content, diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 4bc4458..8f4aede 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,8 +1,8 @@ -import { getChannels, client } from '$lib/server/db'; +import { db } from '$lib/server/db'; import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async () => { - const rows = await getChannels(client); + const rows = await db.getChannels(); const channels: string[] = rows ? rows.map((value) => { return value.table_name; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index eeae3e9..db90b0b 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,8 +1,8 @@ import type { TypeMessage } from '$lib'; -import { getMessages, client } from '$lib/server/db'; +import { db } from '$lib/server/db'; export async function load(): Promise<{ messages: TypeMessage[] }> { - const rows = await getMessages(client, '000', 50); + const rows = await db.getMessages('000', 50); const messages: TypeMessage[] = rows ? rows.map((value) => { return {