aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarvin Borner2020-07-18 15:54:13 +0200
committerMarvin Borner2020-07-18 15:54:13 +0200
commit47f66e980761ad1218f51c7f51e51dd550bf0b35 (patch)
treeac296348b105e750d8d142cad74f09a7afb29be0 /src
parent476715384f4a5d0b7d887961b43b7aa271c08495 (diff)
Data, data, daaaaaaaaataaaaaaaaaaaa!
Diffstat (limited to 'src')
-rw-r--r--src/db/DBController.ts30
-rw-r--r--src/db/tables.sql20
2 files changed, 44 insertions, 6 deletions
diff --git a/src/db/DBController.ts b/src/db/DBController.ts
index 57c640b..2cfd1f2 100644
--- a/src/db/DBController.ts
+++ b/src/db/DBController.ts
@@ -2,19 +2,21 @@ import { Client } from "https://deno.land/x/mysql/mod.ts";
import { readFileStr } from "https://deno.land/std/fs/mod.ts";
export default class DBController {
+ private client?: Client;
+
async init() {
- const conn = await this.connect();
+ this.client = await this.connect();
try {
const sql = await readFileStr("./src/db/tables.sql");
const queries = sql.split(";");
queries.pop();
- queries.forEach((query) => conn.execute(query));
+ queries.forEach(async (query) => await this.execute(query));
console.log("Tables created");
} catch (e) {
console.error("Could not create tables");
throw e;
} finally {
- conn.close();
+ this.client.close();
}
}
@@ -31,4 +33,26 @@ export default class DBController {
throw e;
}
}
+
+ async execute(query: string) {
+ if (this.client) {
+ try {
+ return await this.client.execute(query);
+ } catch (e) {
+ throw e;
+ }
+ } else throw Error("Database isn't initialized yet!");
+ }
+
+ async execute_multiple(queries: string[]) {
+ await this.client!.transaction(async (conn) => {
+ queries.forEach(async (query) => {
+ await conn.execute(query);
+ });
+ });
+ }
+
+ async close() {
+ await this.client!.close();
+ }
}
diff --git a/src/db/tables.sql b/src/db/tables.sql
index 4523522..9883ea1 100644
--- a/src/db/tables.sql
+++ b/src/db/tables.sql
@@ -1,5 +1,19 @@
-DROP TABLE IF EXISTS test;
+DROP TABLE IF EXISTS access;
+DROP TABLE IF EXISTS users;
-CREATE TABLE IF NOT EXISTS test (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
+CREATE TABLE IF NOT EXISTS users (
+ id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
+ email VARCHAR(24) NOT NULL UNIQUE,
+ username VARCHAR(24) NOT NULL UNIQUE,
+ password VARCHAR(64) NOT NULL,
+ verification VARCHAR(64) NOT NULL UNIQUE,
+ dark_theme BOOLEAN NOT NULL DEFAULT true,
+ is_admin BOOLEAN NOT NULL DEFAULT false
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+CREATE TABLE IF NOT EXISTS access (
+ id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
+ uid INT(6) UNSIGNED,
+ path VARCHAR(64) NOT NULL,
+ FOREIGN KEY (uid) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;