index.js
5.01 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuickDB = exports.MySQLDriver = exports.SqliteDriver = void 0;
const SqliteDriver_1 = require("./drivers/SqliteDriver");
const lodash_1 = require("lodash");
var SqliteDriver_2 = require("./drivers/SqliteDriver");
Object.defineProperty(exports, "SqliteDriver", { enumerable: true, get: function () { return SqliteDriver_2.SqliteDriver; } });
var MySQLDriver_1 = require("./drivers/MySQLDriver");
Object.defineProperty(exports, "MySQLDriver", { enumerable: true, get: function () { return MySQLDriver_1.MySQLDriver; } });
class QuickDB {
options;
tableName;
driver;
constructor(options = {}) {
options.filePath ??= "json.sqlite";
options.driver ??= new SqliteDriver_1.SqliteDriver(options.filePath);
options.table ??= "json";
this.options = options;
this.tableName = options.table;
this.driver = options.driver;
this.driver.prepare(this.tableName);
}
async addSubtract(key, value, sub = false) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
if (value == null)
throw new Error("Missing second argument (value)");
let currentNumber = await this.get(key);
if (currentNumber == null)
currentNumber = 0;
if (typeof currentNumber != "number") {
try {
currentNumber = parseFloat(currentNumber);
}
catch (_) {
throw new Error(`Current value with key: (${key}) is not a number and couldn't be parsed to a number`);
}
}
sub ? (currentNumber -= value) : (currentNumber += value);
return this.set(key, currentNumber);
}
async getArray(key) {
const currentArr = (await this.get(key)) ?? [];
if (!Array.isArray(currentArr))
throw new Error(`Current value with key: (${key}) is not an array`);
return currentArr;
}
async all() {
return this.driver.getAllRows(this.tableName);
}
async get(key) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
if (key.includes(".")) {
const keySplit = key.split(".");
const result = await this.driver.getRowByKey(this.tableName, keySplit[0]);
return (0, lodash_1.get)(result, keySplit.slice(1).join("."));
}
return this.driver.getRowByKey(this.tableName, key);
}
async set(key, value) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
if (value == null)
throw new Error("Missing second argument (value)");
if (key.includes(".")) {
const keySplit = key.split(".");
const obj = await this.get(keySplit[0]);
const valueSet = (0, lodash_1.set)(obj ?? {}, keySplit.slice(1).join("."), value);
return this.driver.setRowByKey(this.tableName, keySplit[0], valueSet, obj != null);
}
const update = await this.has(key);
return this.driver.setRowByKey(this.tableName, key, value, update);
}
async has(key) {
return (await this.get(key)) != null;
}
async delete(key) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
return this.driver.deleteRowByKey(this.tableName, key);
}
async deleteAll() {
return this.driver.deleteAllRows(this.tableName);
}
async add(key, value) {
return this.addSubtract(key, value);
}
async sub(key, value) {
return this.addSubtract(key, value, true);
}
async push(key, value) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
if (value == null)
throw new Error("Missing second argument (value)");
let currentArr = await this.getArray(key);
if (Array.isArray(value))
currentArr = currentArr.concat(value);
else
currentArr.push(value);
return this.set(key, currentArr);
}
async pull(key, value) {
if (typeof key != "string")
throw new Error("First argument (key) needs to be a string");
if (value == null)
throw new Error("Missing second argument (value)");
let currentArr = await this.getArray(key);
if (!Array.isArray(value) && typeof value != "function")
value = [value];
currentArr = currentArr.filter((e) => Array.isArray(value) ? !value.includes(e) : !value(e));
return this.set(key, currentArr);
}
table(table) {
if (typeof table != "string")
throw new Error("First argument (table) needs to be a string");
const options = { ...this.options };
options.driver = this.options.driver;
options.table = table;
return new QuickDB(options);
}
}
exports.QuickDB = QuickDB;
//# sourceMappingURL=index.js.map