From 66d328e4422f07eadba48c81ea5641f6354fa2d9 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Sat, 29 Mar 2025 20:01:57 +0100 Subject: [PATCH] feat: New database functions prepare() and exists() feat: Create an example mod by default when initializing database feat: Added config fields for default admin user fix: Init database working but yet still not complete fix: added .vscode to gitignore --- .gitignore | 3 +++ config/config.json | 7 +++++++ server.js | 2 +- src/database/index.js | 23 +++++++++++++++++------ src/database/sqlite.js | 17 ++++++++++++++++- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 6121f23..af33bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -135,6 +135,9 @@ dist .yarn/install-state.gz .pnp.* +# Vscode files +.vscode + # Data data/* diff --git a/config/config.json b/config/config.json index 9a80fe6..f798a38 100644 --- a/config/config.json +++ b/config/config.json @@ -1,5 +1,12 @@ { "port": 8000, + + "users": { + "admin": { + "username": "admin", + "password": "admin" + } + }, "database": { "type": "sqlite" diff --git a/server.js b/server.js index 63a2b4d..b9c37b6 100644 --- a/server.js +++ b/server.js @@ -37,7 +37,7 @@ console.debug("Port: ", port); // Database connection db = connectDatabase(config.database); -db = initDatabase(); +db = initDatabase(config); // --- Routing --- diff --git a/src/database/index.js b/src/database/index.js index c019df0..0049147 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -16,23 +16,34 @@ async function connectDatabase(config) { return db; } -function getDatabase() { - return db; -} // Setups the database by creating the tables and the default objects -function initDatabase() { +async function initDatabase(config) { if (db == null) { throw new Error("Database is not connected"); } + // Create mods table db.exec("CREATE TABLE IF NOT EXISTS mods ( \ Name tinytext PRIMARY KEY, \ DisplayName tinytext, \ - Author tinytext FOREIGN KEY,\ + Author tinytext,\ Versions longtext,\ OtherInfos longtext \ - )"); + );"); + + // 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}', '', '');`); + } } + +function getDatabase() { + return db; +} + + module.exports = { getDatabase, connectDatabase, initDatabase }; \ No newline at end of file diff --git a/src/database/sqlite.js b/src/database/sqlite.js index b9712bb..2267d7b 100644 --- a/src/database/sqlite.js +++ b/src/database/sqlite.js @@ -42,10 +42,25 @@ class SQLiteDatabase { try { return this.db.exec(sql); } catch (err) { - console.error("Error executing query:", err)} + console.error("Error executing statement:", err)} } + async prepare(sql) { + try { + return this.db.prepare(sql); + } catch (err) { + console.error("Error executing prepared statement:", err)} + } + + async exists(table, attribute, value) { + try { + return this.db.prepare(`SELECT COUNT(*) FROM ${table} WHERE ${attribute} = ?`).get(value)['COUNT(*)'] > 0; + } catch (err) { + console.error("Error checking item existence"); + } + } + } module.exports = SQLiteDatabase; \ No newline at end of file