Add methods to the database handler
This commit is contained in:
parent
ce3dbbfe47
commit
631afe7ed4
1 changed files with 26 additions and 1 deletions
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue