enables schema saving in db, initial refactor of db schema for revisions

This commit is contained in:
2024-09-02 03:16:10 -06:00
parent fd5e5bcc8b
commit 5b16cc60f7
9 changed files with 190 additions and 122 deletions

View File

@@ -15,9 +15,13 @@ export class DHSecretClient {
* @param dhBaseUri uri for hosted Dragon's Hoard instance
* @param cacheDir path to cache dir
*/
constructor(private dhBaseUri: string, private cacheDir: string) {
constructor(
private dhBaseUri: string,
private cacheDir: string,
) {
this.cacheLocation = this.cacheDir.trim().replace(/\/^/, "") + "/.dh_cache";
mkdirSync(this.cacheDir, { recursive: true });
this.readDiskCache();
this.token = this.fetchToken();
}
@@ -42,12 +46,13 @@ export class DHSecretClient {
}
private readDiskCache() {
const cache = readFileSync(this.cacheLocation, "utf-8");
if (!cache) {
try {
const cache = readFileSync(this.cacheLocation, "utf-8");
this.cache = JSON.parse(cache || "{}");
} catch {
this.cache = {};
this.writeDiskCache();
} else this.cache = JSON.parse(cache || "{}");
this.writeDiskCache().then(this.readDiskCache);
}
}
private async writeDiskCache() {
await writeFile(this.cacheLocation, JSON.stringify(this.cache), "utf-8");