From e39dbbaeaee6d3dcf40bf74aba932aacfa34fa18 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sun, 12 Jul 2020 18:07:59 +0200 Subject: Example code --- src/db/DBController.ts | 31 +++++++++++++++++++++++++++++++ src/db/connector.ts | 17 ----------------- src/db/tables.ts | 1 + src/db/user.ts | 6 ++++++ 4 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 src/db/DBController.ts delete mode 100644 src/db/connector.ts create mode 100644 src/db/tables.ts create mode 100644 src/db/user.ts (limited to 'src/db') diff --git a/src/db/DBController.ts b/src/db/DBController.ts new file mode 100644 index 0000000..67c6afc --- /dev/null +++ b/src/db/DBController.ts @@ -0,0 +1,31 @@ +import { Client } from "https://deno.land/x/mysql/mod.ts"; +import tables from "./tables.ts"; + +export default class DBController { + async init() { + const conn = await this.connect(); + try { + tables.forEach((table) => conn.execute(table)); + console.log("Tables created"); + } catch (e) { + console.error("Could not create tables"); + throw e; + } finally { + conn.close(); + } + } + + async connect(): Promise { + try { + return await new Client().connect({ + hostname: Deno.env.get("DBHost"), + username: Deno.env.get("DBUser"), + db: Deno.env.get("DBName"), + password: Deno.env.get("DBPassword"), + }); + } catch (e) { + console.error("Could not connect to database"); + throw e; + } + } +} diff --git a/src/db/connector.ts b/src/db/connector.ts deleted file mode 100644 index 6b9bfec..0000000 --- a/src/db/connector.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Client } from "https://deno.land/x/mysql/mod.ts"; - -export default class Connector { - async connect(): Promise { - try { - return await new Client().connect({ - hostname: Deno.env.get("DBHost"), - username: Deno.env.get("DBUser"), - db: Deno.env.get("DBName"), - password: Deno.env.get("DBPassword"), - }); - } catch (e) { - console.error("Could not connect to database!"); - throw e; - } - } -} diff --git a/src/db/tables.ts b/src/db/tables.ts new file mode 100644 index 0000000..d6d1738 --- /dev/null +++ b/src/db/tables.ts @@ -0,0 +1 @@ +export default []; diff --git a/src/db/user.ts b/src/db/user.ts new file mode 100644 index 0000000..c245b5f --- /dev/null +++ b/src/db/user.ts @@ -0,0 +1,6 @@ +import DBController from "./DBController.ts"; + +class User extends DBController { +} + +export default new User(); -- cgit v1.2.3