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
This commit is contained in:
Gu://em_ 2025-03-29 20:01:57 +01:00
parent a98b765b4b
commit 66d328e442
5 changed files with 44 additions and 8 deletions

View file

@ -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 };

View file

@ -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;