-
Notifications
You must be signed in to change notification settings - Fork 65
/
db.ts
64 lines (51 loc) · 1.28 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { unlinkSync } from "fs";
import { JSONFileSync, LowSync } from "lowdb";
import path from "path";
import rimraf from "rimraf";
import { appDir, createAppDir } from "./configuration.js";
import { Decision } from "./constants.js";
createAppDir();
export interface SearcheeEntry {
infoHash?: string;
firstSearched: number;
lastSearched: number;
}
export interface DecisionEntry {
decision: Decision;
firstSeen: number;
lastSeen: number;
infoHash?: string;
}
export interface TorrentEntry {
filepath: string;
infoHash: string;
name: string;
}
export interface Schema {
searchees: Record<string, SearcheeEntry>;
decisions: Record<string, Record<string, DecisionEntry>>;
indexedTorrents: TorrentEntry[];
dbVersion: number;
}
const db = new LowSync<Schema>(
new JSONFileSync<Schema>(path.join(appDir(), "cache.json"))
);
const emptyDatabase = {
searchees: {},
decisions: {},
indexedTorrents: [],
dbVersion: 3,
};
db.data ??= emptyDatabase;
const dbVersion = db.data.dbVersion;
if (!dbVersion || dbVersion < emptyDatabase.dbVersion) {
db.data = emptyDatabase;
}
db.write();
export function dropDatabase(): void {
db.data = emptyDatabase;
db.write();
unlinkSync(path.join(appDir(), "cache.json"));
rimraf.rimrafSync(path.join(appDir(), "torrent_cache"));
}
export default db;