diff options
Diffstat (limited to 'src/db/DBController.ts')
-rw-r--r-- | src/db/DBController.ts | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/src/db/DBController.ts b/src/db/DBController.ts index 67c6afc..57c640b 100644 --- a/src/db/DBController.ts +++ b/src/db/DBController.ts @@ -1,31 +1,34 @@ import { Client } from "https://deno.land/x/mysql/mod.ts"; -import tables from "./tables.ts"; +import { readFileStr } from "https://deno.land/std/fs/mod.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 init() { + const conn = await this.connect(); + try { + const sql = await readFileStr("./src/db/tables.sql"); + const queries = sql.split(";"); + queries.pop(); + queries.forEach((query) => conn.execute(query)); + 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; + 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; + } } - } } |