Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/backend/storage/BaseDb.ts
1030 views
1
import * as Fs from 'fs';
2
import { app } from 'electron';
3
4
export default abstract class BaseDb<T> {
5
protected readonly allData: T;
6
private readonly path: string;
7
8
protected constructor(readonly tableName: string, defaultData: T) {
9
const dbDir = app.getPath('userData');
10
this.path = `${dbDir}/${tableName}.json`;
11
if (Fs.existsSync(this.path)) {
12
this.allData = JSON.parse(Fs.readFileSync(this.path, 'utf-8'));
13
} else {
14
this.allData = defaultData;
15
this.persist();
16
}
17
console.log(`Loaded ${tableName} DB: ${this.path}`);
18
}
19
20
public persist() {
21
// don't overwrite existing file in case we bail half way through
22
Fs.writeFileSync(`${this.path}.tmp`, JSON.stringify(this.allData, null, 2));
23
Fs.renameSync(`${this.path}.tmp`, this.path);
24
}
25
}
26
27