Initial commit
This commit is contained in:
commit
233c1c4da6
14 changed files with 1085 additions and 0 deletions
25
src/utils/database/json.ts
Normal file
25
src/utils/database/json.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { databaseOptions } from "$/types/config";
|
||||
import fs from "fs";
|
||||
|
||||
export class JSONDatabase {
|
||||
private data = {};
|
||||
private conf: databaseOptions;
|
||||
|
||||
constructor(conf: databaseOptions) {
|
||||
this.conf = conf;
|
||||
|
||||
if (!fs.existsSync(conf.uri)) {
|
||||
console.error(`Can't find database file, creating default`);
|
||||
try {
|
||||
fs.writeFileSync(conf.uri, `{}`);
|
||||
} catch (_) {
|
||||
console.log(`Couldn't create database file, ensure the uri is a valid filepath`);
|
||||
};
|
||||
};
|
||||
this.data = JSON.parse(fs.readFileSync(conf.uri, `utf-8`));
|
||||
};
|
||||
|
||||
public shutdown() {
|
||||
fs.writeFileSync(this.conf.uri, JSON.stringify(this.data));
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue