2023-01-22 02:33:52 +00:00
|
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
|
|
|
|
|
|
const db = new sqlite3.Database('./data/config.db', (err) => {
|
|
|
|
if (err) throw `E: DB Connection: ${err.message}`;
|
|
|
|
console.log(`I: Connected to the database.`);
|
|
|
|
});
|
2023-01-21 15:48:16 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2023-01-22 02:33:52 +00:00
|
|
|
run(query) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.serialize(() => {
|
|
|
|
db.run(query, function(err) {
|
|
|
|
if (err) {
|
|
|
|
reject("Problem executing the query: " + err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve( { "status": "Query executed successfully: " + query, "data": this });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
all(query) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.serialize(() => {
|
|
|
|
db.all(query, (err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
reject("Problem executing the query: " + err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// [ { key: 'key_name', value: '0' }, { key: 'key_name', value: '0' } ]
|
|
|
|
let organizedRows = {};
|
|
|
|
rows.forEach(row => {
|
|
|
|
organizedRows[row.key] = row.value;
|
|
|
|
});
|
|
|
|
resolve({ "status": "Query executed successfully: " + query, "rows": organizedRows });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
showTables() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.serialize(() => {
|
|
|
|
db.all("SELECT name FROM sqlite_master WHERE type='table'", (err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
reject("Problem executing the query: " + err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve({ "status": "Tables retreived successfully", "rows": rows });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-01-21 15:48:16 +00:00
|
|
|
};
|