From 5c106de4c1157494d610b4326fb3d7fc8c59fd0f Mon Sep 17 00:00:00 2001 From: ulysia Date: Mon, 6 Jan 2025 18:04:47 +0100 Subject: [PATCH] Switch: mariadb -> postgres Addendum: MariaDB sucks dick with teeth --- docker-compose.yml | 19 ++++--------- package.json | 3 +- src/sqlFunctions.ts | 69 +++++++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 012d292..29c9977 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,7 @@ services: environment: NODE_ENV: "production" DB_HOST: db - DB_USER: root + DB_USER: postgres DB_PASSWORD: example DATABASE: hypixel volumes: @@ -21,21 +21,12 @@ services: db: - image: mariadb:11.7.1-noble-rc + image: postgres:17.2 restart: always environment: - MARIADB_ROOT_PASSWORD: example # Change this to a secure password. Has to be identical to DB_PASSWORD in the bot service - MARIADB_DATABASE: hypixel + POSTGRES_PASSWORD: example # Change this to a secure password. Has to be identical to DB_PASSWORD in the bot service + POSTGRES_DB: hypixel volumes: - - ./db_data:/var/lib/mysql + - ./db_data:/var/lib/postgres/data - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro - -## optional service for quick management of the database. -# adminer: -# image: adminer -# restart: always -# depends_on: -# - db -# ports: -# - 8080:8080 \ No newline at end of file diff --git a/package.json b/package.json index 7fed9de..1050297 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,15 @@ }, "dependencies": { "@types/node": "^22.10.2", + "@types/pg": "^8.11.10", "@types/toastify-js": "^1.12.3", "axios": "^0.24.0", "copy-paste": "^1.5.3", "discord.js": "^14.16.3", "express": "^4.21.2", - "mariadb": "^3.4.0", "node-notifier": "^10.0.1", "open": "^8.4.2", + "pg": "^8.13.1", "prismarine-nbt": "^2.7.0", "socket.io": "^4.8.1", "toastify-js": "^1.12.0", diff --git a/src/sqlFunctions.ts b/src/sqlFunctions.ts index 8af7f50..7653a99 100644 --- a/src/sqlFunctions.ts +++ b/src/sqlFunctions.ts @@ -1,24 +1,31 @@ -import mariadb from 'mariadb'; +import { Pool, PoolClient } from 'pg'; import { loadConfig } from './configLoader'; const config = loadConfig(); class SqlSystem { - private static pool: mariadb.Pool = mariadb.createPool({ database: process.env.DATABASE, host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, connectionLimit: config.data.worker_count ?? 10 }) + private static pool: Pool = new Pool({ + database: process.env.DATABASE, + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + max: config.data.worker_count ?? 10 + }) + public static async InitDB() { - let conn: mariadb.PoolConnection | undefined; + let conn: PoolClient | undefined; try { - conn = await this.pool.getConnection(); + conn = await this.pool.connect(); //Setup tables for auctions and lifetimes await conn.query(` CREATE TABLE if NOT EXISTS auctions ( - id INT AUTO_INCREMENT PRIMARY KEY, + id UUID PRIMARY KEY, auctionid VARCHAR(255) NOT NULL, - lbin DECIMAL(65,5) SIGNED, - UNIQUE INDEX 'auctionid' ('auctionid') + lbin DECIMAL(65,5), + UNIQUE (auctionid) ); - CREATE TABLE if NOT EXISTS lifetimes (id INT PRIMARY KEY, insertedon DATETIME(2)); + CREATE TABLE if NOT EXISTS lifetimes (id UUID PRIMARY KEY, insertedon DATETIME(2)); `); //Setup lifetime Trigger await conn.query(` @@ -88,10 +95,10 @@ class SqlSystem { } //INSERT ELEMENT IN AUCTIONS TABLE OR UPDATE IF IT ALREADY EXISTS public static async Upsert(auctionid: string|string[], lbin: number|number[]) { - let conn: mariadb.PoolConnection | undefined; + let conn: PoolClient | undefined; try { - conn = await this.pool.getConnection(); - await conn.beginTransaction(); + conn = await this.pool.connect(); + await conn.query('BEGIN'); if(Array.isArray(auctionid) && Array.isArray(lbin)) if(auctionid.length === lbin.length) { await conn.query(` @@ -111,12 +118,12 @@ class SqlSystem { LBin = VALUES(LBin); `, [auctionid, lbin]); } - await conn.commit(); + await conn.query('COMMIT'); } catch(error) { console.error("message_before_error: ", error); - if(conn) await conn.rollback(); + if (conn) await conn.query('ROLLBACK'); throw error; } finally @@ -126,18 +133,18 @@ class SqlSystem { } //REMOVE ELEMENT IN AUCTIONS TABLE public static async Remove(auctionid: string|string[]) { - let conn: mariadb.PoolConnection | undefined; + let conn: PoolClient | undefined; try { - conn = await this.pool.getConnection(); - await conn.beginTransaction(); + conn = await this.pool.connect(); + await conn.query('BEGIN'); await conn.query(` DELETE FROM auctions WHERE auctionid = ? `,[auctionid]); - await conn.commit(); + await conn.query('COMMIT'); } catch (error) { console.error("InitDB Error: ", error); - if(conn) await conn.rollback(); + if (conn) await conn.query('ROLLBACK'); throw error; } finally { @@ -146,16 +153,16 @@ class SqlSystem { } public static async Query(query:string,use_transaction:boolean = true) { - let conn: mariadb.PoolConnection | undefined; + let conn: PoolClient | undefined; try{ - conn = await this.pool.getConnection() - if(use_transaction) await conn.beginTransaction(); + conn = await this.pool.connect() + if (use_transaction) await conn.query('BEGIN'); await conn.query(query); - if(use_transaction) await conn.commit(); + if (use_transaction) await conn.query('COMMIT'); } catch(error) { console.error("InitDB Error: ", error); - if(use_transaction && conn) await conn.rollback(); + if (use_transaction && conn) await conn.query('ROLLBACK'); throw error; } finally @@ -165,10 +172,10 @@ class SqlSystem { } //MATCH PROVIDED ELEMENTS IN AUCTIONS TABLE - returns true/false public static async Match(auctionid:string|string[],lbin:number|number[]): Promise { - let conn: mariadb.PoolConnection | undefined; + let conn: PoolClient | undefined; let result; try{ - conn = await this.pool.getConnection(); + conn = await this.pool.connect(); if(Array.isArray(auctionid) && Array.isArray(lbin)) if(auctionid.length === lbin.length) { result = await conn.query(` @@ -198,7 +205,7 @@ class SqlSystem { END AS user_exists FROM auctions WHERE auctionID = ?; `,[auctionid]); - return Boolean(result[0].result); + return Boolean(result.rows.length); } } catch(error) @@ -215,19 +222,19 @@ class SqlSystem { //EXAMPLE BLOCK OF CODE FOR ADDING DATABASE FUNCTIONS /* public static async example_name() { - let conn:mariadb.PoolConnection|undefined; + let conn:PoolClient|undefined; try{ - conn = await this.pool.getConnection(); - await conn.beginTransaction(); + conn = await this.pool.connect(); + await conn.query('BEGIN'); //CODE HERE// - await conn.commit(); + await conn.query('COMMIT'); } catch(error) { console.error("message_before_error: ", error); - if(conn) await conn.rollback(); + if(conn) await conn.query('ROLLBACK'); throw error; } finally