moved to sqlsystem class

This commit is contained in:
Ulysia 2024-12-28 13:41:48 +01:00
parent ebfd6c96ed
commit 1dc528fd30
2 changed files with 18 additions and 14 deletions

View file

@ -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] });

View file

@ -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
}
export {
SqlSystem
}