Have the JSON DB extend the DB handler

This commit is contained in:
Oliver Akins 2022-08-14 14:20:53 -06:00
parent 4045b3d859
commit bca094c832
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -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;