fix: Replace the bunch of db functions with a Db class

This commit is contained in:
April Hall 2025-02-05 18:31:23 -05:00
parent fd8b8cdfd3
commit 95108ab9eb
Signed by: arithefirst
GPG Key ID: 4508A15C4DB91C5B
4 changed files with 66 additions and 52 deletions

View File

@ -1,8 +1,16 @@
import cassandra from 'cassandra-driver';
async function createChannel(client: cassandra.Client, channelName: string) {
class Db {
private client: cassandra.Client;
constructor(client: cassandra.Client) {
this.client = client;
}
// Create Channel Method
async createChannel(channelName: string) {
try {
await client.execute(`
await this.client.execute(`
CREATE TABLE IF NOT EXISTS channels.channel_${channelName} (
id UUID,
message_content TEXT,
@ -17,10 +25,11 @@ async function createChannel(client: cassandra.Client, channelName: string) {
}
}
async function storeMessage(client: cassandra.Client, channelName: string, content: string, sender: string, id: string) {
// Send message method
async sendMessage(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)
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
@ -28,9 +37,10 @@ async function storeMessage(client: cassandra.Client, channelName: string, conte
}
}
async function getChannels(client: cassandra.Client): Promise<cassandra.types.Row[] | undefined> {
// Get Channels method
async getChannels(): Promise<cassandra.types.Row[] | undefined> {
try {
const res = await client.execute(`SELECT table_name FROM system_schema.tables WHERE keyspace_name = 'channels'`);
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
@ -39,9 +49,10 @@ async function getChannels(client: cassandra.Client): Promise<cassandra.types.Ro
}
}
async function getMessages(client: cassandra.Client, channelName: string, limit: number): Promise<cassandra.types.Row[] | undefined> {
// Get messages method
async getMessages(channelName: string, limit: number): Promise<cassandra.types.Row[] | undefined> {
try {
const res = await client.execute(
const res = await this.client.execute(
`SELECT * FROM channels.channel_${channelName} WHERE channel_name = '${channelName}' ORDER BY timestamp DESC LIMIT ${limit}`,
);
return res.rows;
@ -51,6 +62,7 @@ async function getMessages(client: cassandra.Client, channelName: string, limit:
return;
}
}
}
const client = new cassandra.Client({
contactPoints: ['localhost'],
@ -76,4 +88,6 @@ try {
process.exit(1);
}
export { client, createChannel, getChannels, getMessages, storeMessage };
const db = new Db(client);
export { db };

View File

@ -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,

View File

@ -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;

View File

@ -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 {