From 4e5fe4711fe46a6af740aadbf23bde86bb5acf69 Mon Sep 17 00:00:00 2001 From: JoelHMikael Date: Fri, 7 Jan 2022 10:58:02 +0200 Subject: Added the DB class & instructions to set up DB --- database.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 database.js (limited to 'database.js') diff --git a/database.js b/database.js new file mode 100644 index 0000000..3a2822a --- /dev/null +++ b/database.js @@ -0,0 +1,37 @@ +class Database +{ + constructor(credentials, log) + { + this.connection = mysql.createConnection(credentials); + this.log = log; + } + query(q) + { + return new Promise((resolve, reject), () => + { + this.connection.query(q, (err, res, fields) => + { + if (err) + { + this.log(err); + reject(err); + } + resolve(res); + }); + }); + } + close() + { + this.connection.end(err => + { + if (err) + { + this.log(err); + reject(err); + } + resolve(); + }); + } +} + +exports.Database = Database; -- cgit v1.2.3 From 24709fa862636702fd0430e832463b751fce323e Mon Sep 17 00:00:00 2001 From: JoelHMikael Date: Sat, 8 Jan 2022 16:49:37 +0200 Subject: Parsing the shifts (not classes) to the database + some GUI improvements --- database.js | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'database.js') diff --git a/database.js b/database.js index 3a2822a..30396fe 100644 --- a/database.js +++ b/database.js @@ -1,35 +1,55 @@ +const mysql = require("mysql2"); + class Database { - constructor(credentials, log) + constructor(credentials) { this.connection = mysql.createConnection(credentials); - this.log = log; } - query(q) + query(query, values) { - return new Promise((resolve, reject), () => + return new Promise((resolve, reject) => { - this.connection.query(q, (err, res, fields) => + this.connection.query(query, values, (err, res, fields) => { - if (err) - { - this.log(err); - reject(err); - } + if (err) reject(err); + resolve(res); + }); + }); + } + execute(query, values) + { + return new Promise((resolve, reject) => + { + this.connection.execute(query, values, (err, res, fields) => + { + if (err) reject(err); resolve(res); }); }); + + } + query_raw(query) + { + return new Promise((resolve, reject) => + { + this.connection.query(query, (err, res, fields) => + { + if (err) + reject(err) + resolve(res); + }); + }) } close() { - this.connection.end(err => + return new Promise((resolve, reject) => { - if (err) + this.connection.end(err => { - this.log(err); - reject(err); - } - resolve(); + if (err) reject(err); + resolve(); + }); }); } } -- cgit v1.2.3