From 1dc528fd30009f4ba5fc67e218eb054dcb8337f3 Mon Sep 17 00:00:00 2001 From: ulysia Date: Sat, 28 Dec 2024 13:41:48 +0100 Subject: [PATCH] moved to sqlsystem class --- index.ts | 4 ++-- src/sqlFunctions.ts | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 8847a94..2615df8 100644 --- a/index.ts +++ b/index.ts @@ -3,7 +3,7 @@ import { WebhookClient, EmbedBuilder, Embed } from 'discord.js'; import { Worker } from 'worker_threads'; import { asyncInterval, addNotation } from './src/helperFunctions'; import { string } from 'prismarine-nbt'; -import { InitTable } from './src/sqlFunctions'; +import { SqlSystem } from './src/sqlFunctions'; import { loadConfig } from './src/configLoader'; const config = loadConfig(); @@ -38,7 +38,7 @@ const bazaarPrice = { }; async function initialize() { - await InitTable(); + await SqlSystem.InitTable(); const matches = process.env.WEBHOOK_URL.match(webhookRegex); if (!matches) return console.log(`[Main thread] Couldn't parse Webhook URL`); const webhook = new WebhookClient({ id: matches[1], token: matches[2] }); diff --git a/src/sqlFunctions.ts b/src/sqlFunctions.ts index d673b68..b5f6762 100644 --- a/src/sqlFunctions.ts +++ b/src/sqlFunctions.ts @@ -4,23 +4,25 @@ import sqlite3 from 'sqlite3'; // MUTEX functions for adding/removing/upsert // basic read function by id // complex read function by value range +class SqlSystem { + private static db: sqlite3.Database; -async function InitTable() { + public static async InitTable() { const db = new sqlite3.Database('bot_data'); try{ - await runQuery(db,'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)') + await this.runQuery('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)') console.log('Table created successfully.'); // Insert data - await runQuery(db, 'INSERT INTO users (name) VALUES (?)', ['Alice']); + await this.runQuery('INSERT INTO users (name) VALUES (?)', ['Alice']); console.log('Data inserted successfully.'); // Retrieve a single row - const row = await getQuery(db, 'SELECT * FROM users WHERE name = ?', ['Alice']); + const row = await this.getQuery('SELECT * FROM users WHERE name = ?', ['Alice']); console.log('Retrieved row:', row); // Retrieve all rows - const rows = await allQuery(db, 'SELECT * FROM users'); + const rows = await this.allQuery('SELECT * FROM users'); console.log('All rows:', rows); } catch (err: any) { console.error('Database error:', err.message); @@ -28,9 +30,9 @@ async function InitTable() { db.close(); } } -function runQuery(db: sqlite3.Database, query: string, params: string[] = []) { + public static runQuery(query: string, params: string[] = []) { return new Promise((resolve,reject) => { - db.run(query, params, function (err) { + this.db.run(query, params, function (err: Error | null | undefined) { if(err){ reject(err); } @@ -40,9 +42,9 @@ function runQuery(db: sqlite3.Database, query: string, params: string[] = []) { }); }); } -function getQuery(db: sqlite3.Database, query: string, params: string[] = []) { + public static getQuery(query: string, params: string[] = []) { return new Promise((resolve, reject) => { - db.get(query, params, (err, row) => { + this.db.get(query, params, (err: Error | null | undefined, row: sqlite3.RunResult) => { if (err) { reject(err); } else { @@ -51,9 +53,9 @@ function getQuery(db: sqlite3.Database, query: string, params: string[] = []) { }); }); } -function allQuery(db: sqlite3.Database, query: string, params: string[] = []) { + public static allQuery(query: string, params: string[] = []) { return new Promise((resolve, reject) => { - db.all(query, params, (err, rows) => { + this.db.all(query, params, (err: Error | null | undefined, rows: sqlite3.RunResult) => { if (err) { reject(err); } else { @@ -63,6 +65,8 @@ function allQuery(db: sqlite3.Database, query: string, params: string[] = []) { }); } +} + export { - InitTable + SqlSystem } \ No newline at end of file