SqliteDriver.js
1.79 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqliteDriver = void 0;
class SqliteDriver {
database;
constructor(path) {
const sqlite3 = require("better-sqlite3");
this.database = sqlite3(path);
}
async prepare(table) {
this.database
.prepare(`CREATE TABLE IF NOT EXISTS ${table} (ID TEXT, json TEXT)`)
.run();
}
async getAllRows(table) {
const prep = this.database.prepare(`SELECT * FROM ${table}`);
const data = [];
for (const row of prep.iterate()) {
data.push({
id: row.ID,
value: JSON.parse(row.json),
});
}
return data;
}
async getRowByKey(table, key) {
const value = await this.database
.prepare(`SELECT json FROM ${table} WHERE ID = @key`)
.get({
key,
});
return value != null ? JSON.parse(value.json) : null;
}
async setRowByKey(table, key, value, update) {
const stringifiedJson = JSON.stringify(value);
if (update) {
this.database
.prepare(`UPDATE ${table} SET json = (?) WHERE ID = (?)`)
.run(stringifiedJson, key);
}
else {
this.database
.prepare(`INSERT INTO ${table} (ID,json) VALUES (?,?)`)
.run(key, stringifiedJson);
}
return value;
}
async deleteAllRows(table) {
return this.database.prepare(`DELETE FROM ${table}`).run().changes;
}
async deleteRowByKey(table, key) {
return this.database.prepare(`DELETE FROM ${table} WHERE ID=@key`).run({
key,
}).changes;
}
}
exports.SqliteDriver = SqliteDriver;
//# sourceMappingURL=SqliteDriver.js.map