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:
parent
a98b765b4b
commit
66d328e442
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -135,6 +135,9 @@ dist
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# Vscode files
|
||||||
|
.vscode
|
||||||
|
|
||||||
# Data
|
# Data
|
||||||
data/*
|
data/*
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
{
|
{
|
||||||
"port": 8000,
|
"port": 8000,
|
||||||
|
|
||||||
|
"users": {
|
||||||
|
"admin": {
|
||||||
|
"username": "admin",
|
||||||
|
"password": "admin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
"database": {
|
"database": {
|
||||||
"type": "sqlite"
|
"type": "sqlite"
|
||||||
|
|
|
@ -37,7 +37,7 @@ console.debug("Port: ", port);
|
||||||
|
|
||||||
// Database connection
|
// Database connection
|
||||||
db = connectDatabase(config.database);
|
db = connectDatabase(config.database);
|
||||||
db = initDatabase();
|
db = initDatabase(config);
|
||||||
|
|
||||||
// --- Routing ---
|
// --- Routing ---
|
||||||
|
|
||||||
|
|
|
@ -16,23 +16,34 @@ async function connectDatabase(config) {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDatabase() {
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setups the database by creating the tables and the default objects
|
// Setups the database by creating the tables and the default objects
|
||||||
function initDatabase() {
|
async function initDatabase(config) {
|
||||||
if (db == null) {
|
if (db == null) {
|
||||||
throw new Error("Database is not connected");
|
throw new Error("Database is not connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create mods table
|
||||||
db.exec("CREATE TABLE IF NOT EXISTS mods ( \
|
db.exec("CREATE TABLE IF NOT EXISTS mods ( \
|
||||||
Name tinytext PRIMARY KEY, \
|
Name tinytext PRIMARY KEY, \
|
||||||
DisplayName tinytext, \
|
DisplayName tinytext, \
|
||||||
Author tinytext FOREIGN KEY,\
|
Author tinytext,\
|
||||||
Versions longtext,\
|
Versions longtext,\
|
||||||
OtherInfos 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 };
|
module.exports = { getDatabase, connectDatabase, initDatabase };
|
|
@ -42,10 +42,25 @@ class SQLiteDatabase {
|
||||||
try {
|
try {
|
||||||
return this.db.exec(sql);
|
return this.db.exec(sql);
|
||||||
} catch (err) {
|
} 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;
|
module.exports = SQLiteDatabase;
|
Loading…
Reference in a new issue