From 988667bde88493605c177560f92222633fcfcd04 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Thu, 24 Apr 2025 18:33:05 +0200 Subject: [PATCH] Updated database initialization and connection functions --- server.js | 19 ++++++++++++------- src/database/index.js | 34 +++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/server.js b/server.js index 70ba88a..cf1216d 100644 --- a/server.js +++ b/server.js @@ -13,15 +13,20 @@ const config = loadConfig(); app.use(express.json()); // Necessary to parse JSON bodies // Database connection -connectDatabase(config.database); -initDatabase(config); +(async () => { -// --- Routing --- + // --- Database connection --- + await connectDatabase(); + await initDatabase(); + + // --- Routing --- + app.use("/", require("./src/routes/index")); + app.use("/mods", require("./src/routes/mods")); + app.use("/users", require("./src/routes/users")); + app.use("/login", require("./src/routes/login")); + +})(); -app.use("/", require("./src/routes/index")); -app.use("/mods", require("./src/routes/mods")); -app.use("/users", require("./src/routes/users")); -app.use("/login", require("./src/routes/login")); // --- Launch --- diff --git a/src/database/index.js b/src/database/index.js index 7576626..116b623 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -1,9 +1,15 @@ +const { getConfig } = require("../utils/configManager"); const MySQLDatabase = require("./mysql"); const SQLiteDatabase = require("./sqlite"); let db; -async function connectDatabase(config) { +async function connectDatabase() { + + // Get config + const config = await getConfig(); + + // Choose database type if (config.type === "mysql") { db = new MySQLDatabase(config); } else if (config.type === "sqlite") { @@ -12,14 +18,16 @@ async function connectDatabase(config) { throw new Error("Invalid database type: ", config.type); } + // Connect await db.connect(); return db; } // Setups the database by creating the tables and the default objects -async function initDatabase(config) { - if (db == null) { +async function initDatabase() { + + if (!db) { throw new Error("Database is not connected"); } @@ -33,11 +41,11 @@ async function initDatabase(config) { );"); // Insert example mod - if (!(await db.exists("mods", "Name", "example"))) { - console.debug("Creating default mod"); - db.exec(`INSERT INTO mods (Name, DisplayName, Author, Versions, OtherInfos) \ - VALUES ('example', 'Example mod', '${config.users.admin.username}', '', '');`); - } + // if (!(await db.exists("mods", "Name", "example"))) { + // console.debug("Creating default mod"); + // db.exec(`INSERT INTO mods (Name, DisplayName, Author, Versions, OtherInfos) \ + // VALUES ('example', 'Example mod', '${config.users.admin.username}', '', '');`); + // } db.exec("DROP TABLE users"); // Create users table @@ -52,11 +60,11 @@ async function initDatabase(config) { );"); // Insert default admin account - if (!(await db.exists("users", "Username", config.users.admin.username))) { - console.debug("Creating default admin user"); - db.exec(`INSERT INTO users (Username, DisplayName, Email, Password, ProfilePicture, Preferences, Favorites) \ - VALUES ('${config.users.admin.username}', 'Admin', '', '${config.users.admin.password}', '', '', '' );`); - } + // if (!(await db.exists("users", "Username", config.users.admin.username))) { + // console.debug("Creating default admin user"); + // db.exec(`INSERT INTO users (Username, DisplayName, Email, Password, ProfilePicture, Preferences, Favorites) \ + // VALUES ('${config.users.admin.username}', 'Admin', '', '${config.users.admin.password}', '', '', '' );`); + // } }