Updated database initialization and connection functions

This commit is contained in:
Gu://em_ 2025-04-24 18:33:05 +02:00
parent 72dbe04b3b
commit 988667bde8
2 changed files with 33 additions and 20 deletions

View file

@ -13,15 +13,20 @@ const config = loadConfig();
app.use(express.json()); // Necessary to parse JSON bodies app.use(express.json()); // Necessary to parse JSON bodies
// Database connection // Database connection
connectDatabase(config.database); (async () => {
initDatabase(config);
// --- 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 --- // --- Launch ---

View file

@ -1,9 +1,15 @@
const { getConfig } = require("../utils/configManager");
const MySQLDatabase = require("./mysql"); const MySQLDatabase = require("./mysql");
const SQLiteDatabase = require("./sqlite"); const SQLiteDatabase = require("./sqlite");
let db; let db;
async function connectDatabase(config) { async function connectDatabase() {
// Get config
const config = await getConfig();
// Choose database type
if (config.type === "mysql") { if (config.type === "mysql") {
db = new MySQLDatabase(config); db = new MySQLDatabase(config);
} else if (config.type === "sqlite") { } else if (config.type === "sqlite") {
@ -12,14 +18,16 @@ async function connectDatabase(config) {
throw new Error("Invalid database type: ", config.type); throw new Error("Invalid database type: ", config.type);
} }
// Connect
await db.connect(); await db.connect();
return db; return db;
} }
// Setups the database by creating the tables and the default objects // Setups the database by creating the tables and the default objects
async function initDatabase(config) { async function initDatabase() {
if (db == null) {
if (!db) {
throw new Error("Database is not connected"); throw new Error("Database is not connected");
} }
@ -33,11 +41,11 @@ async function initDatabase(config) {
);"); );");
// Insert example mod // Insert example mod
if (!(await db.exists("mods", "Name", "example"))) { // if (!(await db.exists("mods", "Name", "example"))) {
console.debug("Creating default mod"); // console.debug("Creating default mod");
db.exec(`INSERT INTO mods (Name, DisplayName, Author, Versions, OtherInfos) \ // db.exec(`INSERT INTO mods (Name, DisplayName, Author, Versions, OtherInfos) \
VALUES ('example', 'Example mod', '${config.users.admin.username}', '', '');`); // VALUES ('example', 'Example mod', '${config.users.admin.username}', '', '');`);
} // }
db.exec("DROP TABLE users"); db.exec("DROP TABLE users");
// Create users table // Create users table
@ -52,11 +60,11 @@ async function initDatabase(config) {
);"); );");
// Insert default admin account // Insert default admin account
if (!(await db.exists("users", "Username", config.users.admin.username))) { // if (!(await db.exists("users", "Username", config.users.admin.username))) {
console.debug("Creating default admin user"); // console.debug("Creating default admin user");
db.exec(`INSERT INTO users (Username, DisplayName, Email, Password, ProfilePicture, Preferences, Favorites) \ // db.exec(`INSERT INTO users (Username, DisplayName, Email, Password, ProfilePicture, Preferences, Favorites) \
VALUES ('${config.users.admin.username}', 'Admin', '', '${config.users.admin.password}', '', '', '' );`); // VALUES ('${config.users.admin.username}', 'Admin', '', '${config.users.admin.password}', '', '', '' );`);
} // }
} }