From bca094c8327f7e11c236fca80284d97db6f54e74 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Sun, 14 Aug 2022 14:20:53 -0600 Subject: [PATCH] Have the JSON DB extend the DB handler --- src/utils/database/json.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/utils/database/json.ts b/src/utils/database/json.ts index 0b68f5e..5177fb9 100644 --- a/src/utils/database/json.ts +++ b/src/utils/database/json.ts @@ -1,11 +1,13 @@ import { databaseOptions } from "$/types/config"; +import { DatabaseHandler } from "../Database"; import fs from "fs"; -export class JSONDatabase { +export class JSONDatabase extends DatabaseHandler { private data: any = {}; private conf: databaseOptions; constructor(conf: databaseOptions) { + super(); this.conf = conf; if (!fs.existsSync(conf.uri)) { @@ -23,24 +25,24 @@ export class JSONDatabase { fs.writeFileSync(this.conf.uri, JSON.stringify(this.data)); }; - public channelExists(channel: string) { + public async channelExists(channel: string) { return this.data[channel] != null; }; - public counterExists(channel: string, counter: string) { - return this.channelExists(channel) && this.data[channel].counters[counter] != null; + public async counterExists(channel: string, counter: string) { + return await this.channelExists(channel) && this.data[channel].counters[counter] != null; }; - public isUserIgnored(channel: string, user: string) { + public async isUserIgnored(channel: string, user: string) { return this.data[channel].ignored_users.includes(user); }; - public getMessage(channel: string, counter: string) { + public async getMessage(channel: string, counter: string) { return this.data[channel].counters[counter].message; }; - public addChannel(channel: string) { - if (this.channelExists(channel)) { + public async addChannel(channel: string) { + if (await this.channelExists(channel)) { throw new Error("Channel already exists"); }; this.data[channel] = { @@ -49,8 +51,8 @@ export class JSONDatabase { }; }; - public addCounter(channel: string, counter: string) { - if (this.counterExists(channel, counter)) { + public async addCounter(channel: string, counter: string) { + if (await this.counterExists(channel, counter)) { throw new Error(`Counter already exists`); }; this.data[channel].counters[counter] = { @@ -59,11 +61,11 @@ export class JSONDatabase { }; }; - public changeCount(channel: string, counter: string, delta: number): number { - if (!this.channelExists(channel)) { + public async changeCount(channel: string, counter: string, delta: number) { + if (!await this.channelExists(channel)) { throw new Error("Channel not found"); }; - if (!this.counterExists(channel, counter)) { + if (!await this.counterExists(channel, counter)) { throw new Error("Counter doesn't exist on that channel"); }; return this.data[channel].counters[counter].value += delta;