Add methods to the database handler

This commit is contained in:
Oliver Akins 2022-08-09 19:10:49 -06:00
parent ce3dbbfe47
commit 631afe7ed4
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -2,7 +2,7 @@ import { databaseOptions } from "$/types/config";
import fs from "fs";
export class JSONDatabase {
private data = {};
private data: any = {};
private conf: databaseOptions;
constructor(conf: databaseOptions) {
@ -22,4 +22,29 @@ export class JSONDatabase {
public shutdown() {
fs.writeFileSync(this.conf.uri, JSON.stringify(this.data));
};
public 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 addChannel(channel: string) {
if (this.channelExists(channel)) {
throw new Error("Channel already exists");
};
this.data[channel] = { counters: {} };
};
public changeCount(channel: string, counter: string, delta: number): number {
if (!this.channelExists(channel)) {
throw new Error("Channel not found");
};
if (!this.counterExists(channel, counter)) {
throw new Error("Counter doesn't exist on that channel");
};
return this.data[channel].counters[counter] += delta;
};
};