diff options
author | LarsVomMars | 2020-07-12 18:07:59 +0200 |
---|---|---|
committer | LarsVomMars | 2020-07-12 18:07:59 +0200 |
commit | e39dbbaeaee6d3dcf40bf74aba932aacfa34fa18 (patch) | |
tree | 09a40a99e40f765f569d14a7ce678df9135f27a5 /src/db | |
parent | 7023dc2a6920aa6389ac2b28bcdbaacdb4413e00 (diff) |
Example code
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/DBController.ts | 31 | ||||
-rw-r--r-- | src/db/connector.ts | 17 | ||||
-rw-r--r-- | src/db/tables.ts | 1 | ||||
-rw-r--r-- | src/db/user.ts | 6 |
4 files changed, 38 insertions, 17 deletions
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<Client> { + 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<Client> { - 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(); |