diff options
Diffstat (limited to 'src/db/DBController.ts')
-rw-r--r-- | src/db/DBController.ts | 30 |
1 files changed, 27 insertions, 3 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(); + } } |