Restructured and organized the whole project, fixed SQLite database handler (now using better-sqlite3), made first model functions to interact with the database

This commit is contained in:
Gu://em_ 2025-03-10 22:51:57 +01:00
parent 99591a7191
commit 0ee5eedcfd
14 changed files with 140 additions and 854 deletions

15
src/database/index.js Normal file
View file

@ -0,0 +1,15 @@
const MySQLDatabase = require("./mysql");
const SQLiteDatabase = require("./sqlite");
function getDatabase(config) {
if (config.type === "mysql") {
return new MySQLDatabase(config);
} else if (config.type === "sqlite") {
return new SQLiteDatabase(config);
} else {
throw new Error("Invalid database type: ", config.type);
}
}
module.exports = { getDatabase };

30
src/database/mysql.js Normal file
View file

@ -0,0 +1,30 @@
class MySQLDatabase {
constructor(config) {
const mysql = require("mysql2/promise");
this.config = config;
}
async connect() {
this.db = await mysql.createConnection({
host: this.config.host,
user: this.config.user,
password: this.config.password,
database: this.config.database,
});
console.log("Connected to MySQL");
}
async close() {
await this.db.end();
}
async query(sql, params) {
throw new Error("Not implemented"); //TODO
const [results] = await this.db.execute(sql, params);
return results;
}
}
module.exports = MySQLDatabase;

41
src/database/sqlite.js Normal file
View file

@ -0,0 +1,41 @@
class SQLiteDatabase {
constructor(config) {
const sqlite = require("better-sqlite3");
this.config = config;
this.db = null;
}
async connect() {
try {
const db = new sqlite("./data/sqlite.db")
// db.pragma("journal_mode = WAL")
console.log("Connected to SQLite");
} catch (err) {
console.error("Error connecting to SQLite database: ", err);
process.exit(1);
}
}
async close() {
if (this.db && this.db.open) {
this.db.close();
console.debug("Closed database connection");
}
}
async query(sql, params = []) {
try {
if (params.length > 0) {
return this.db.prepare(sql).all(params);
} else {
return this.db.prepare(sql).all();
}
} catch (err) {
console.error("Error executing query: ", err);
throw err;
}
}
}
module.exports = SQLiteDatabase;

25
src/models/mod.js Normal file
View file

@ -0,0 +1,25 @@
// const db = require("...");
async function getAllMods() {
return db.query("SELECT * FROM mods");
}
async function getModsByName(name) {
return db.query("SELECT * FROM mods WHERE name = ?", [name]);
}
// --- WIP ---
async function createMod(mod_data) {
console.log("WARNING: using a WIP function : createMod (models/mods.js)")
const { name, description } = mod_data;
return db.query("INSERT INTO mods (name, description) VALUES (?, ?)", [name, description]);
}
async function deleteMod(name) {
console.log("WARNING: using a WIP function : deleteMod (models/mods.js)")
return db.query("DELETE FROM mods WHERE name = ?", [name]);
}
module.exports = { getAllMods, getModsByName }

0
src/models/modpacks.js Normal file
View file

0
src/routes/modpacks.js Normal file
View file

11
src/routes/mods.js Normal file
View file

@ -0,0 +1,11 @@
const express = require("express");
const router = express.Router();
// List mods
router.get("/list", async (req,res) => {
console.debug("Accessing mods list");
res.send("No list yet");
});
module.exports = router;

View file

View file