feat: Add error handling to DB interactions

This commit is contained in:
April Hall 2025-01-09 17:20:55 -05:00
parent 2a5f521bf1
commit c6260f619c
Signed by: arithefirst
GPG Key ID: 4508A15C4DB91C5B

View File

@ -1,6 +1,7 @@
import cassandra from 'cassandra-driver'; import cassandra from 'cassandra-driver';
async function createChannel(client: cassandra.Client, channelName: string) { async function createChannel(client: cassandra.Client, channelName: string) {
try {
await client.execute(` await client.execute(`
CREATE TABLE IF NOT EXISTS channels.channel_${channelName} ( CREATE TABLE IF NOT EXISTS channels.channel_${channelName} (
id UUID PRIMARY KEY, id UUID PRIMARY KEY,
@ -8,12 +9,21 @@ async function createChannel(client: cassandra.Client, channelName: string) {
timestamp TIMESTAMP, timestamp TIMESTAMP,
sender UUID sender UUID
);`); );`);
} catch (e) {
// @ts-expect-error I don't like this thing yelling at me
console.log(`Error creating new channel: ${e.message}`);
}
} }
async function storeMessage(client: cassandra.Client, channelName: string, content: string, sender: string, id: string) { async function storeMessage(client: cassandra.Client, channelName: string, content: string, sender: string, id: string) {
try {
const now = new Date(); const now = new Date();
await client.execute(`INSERT INTO channels.channel_${channelName} (id, message_content, timestamp, sender) await client.execute(`INSERT INTO channels.channel_${channelName} (id, message_content, timestamp, sender)
VALUES (${id}, '${content}', ${now.getTime()}, ${sender})`); VALUES (${id}, '${content}', ${now.getTime()}, ${sender})`);
} catch (e) {
// @ts-expect-error I don't like this thing yelling at me
console.log(`Error storing messages: ${e.message}`);
}
} }
async function getMessages(client: cassandra.Client, channelName: string, limit: number) { async function getMessages(client: cassandra.Client, channelName: string, limit: number) {
@ -28,7 +38,6 @@ async function getMessages(client: cassandra.Client, channelName: string, limit:
return res.rows; return res.rows;
} catch (e) { } catch (e) {
// @ts-expect-error I don't like this thing yelling at me // @ts-expect-error I don't like this thing yelling at me
// We all know it's gonna work
console.log(`Error fetching messages: ${e.message}`); console.log(`Error fetching messages: ${e.message}`);
} }
} }
@ -40,8 +49,19 @@ const client = new cassandra.Client({
// Connect to Cassandra/ScyllaDB and create // Connect to Cassandra/ScyllaDB and create
// the necessary tables, keyspaces, etc. // the necessary tables, keyspaces, etc.
await client.connect(); try {
await client.execute(`CREATE KEYSPACE IF NOT EXISTS users WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`); await client.connect()
await client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`); } catch (e) {
// @ts-expect-error I don't like this thing yelling at me
console.log(`Error connecting: ${e.message}`);
}
try {
await client.execute(`CREATE KEYSPACE IF NOT EXISTS users WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
await client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
} catch (e) {
// @ts-expect-error I don't like this thing yelling at me
console.log(`Error generating keyspaces: ${e.message}`);
}
export { client, createChannel, getMessages, storeMessage }; export { client, createChannel, getMessages, storeMessage };