@onurege3467/zerohelper 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/database/mysql/structures/methods/add.js +16 -18
- package/database/mysql/structures/methods/all.js +22 -21
- package/database/mysql/structures/methods/auto_increment.js +13 -10
- package/database/mysql/structures/methods/base_get.js +11 -9
- package/database/mysql/structures/methods/base_set.js +18 -10
- package/database/mysql/structures/methods/clear.js +13 -12
- package/database/mysql/structures/methods/delete.js +28 -21
- package/database/mysql/structures/methods/exists.js +12 -10
- package/database/mysql/structures/methods/get.js +37 -37
- package/database/mysql/structures/methods/has.js +39 -37
- package/database/mysql/structures/methods/includes.js +14 -10
- package/database/mysql/structures/methods/pull.js +20 -16
- package/database/mysql/structures/methods/push.js +20 -17
- package/database/mysql/structures/methods/set.js +58 -46
- package/database/mysql/structures/methods/stats.js +10 -7
- package/database/mysql/structures/methods/sub.js +16 -18
- package/package.json +1 -1
- package/readme.md +47 -8
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return res;
|
|
21
|
-
}
|
|
5
|
+
module.exports = async function (key, value, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
if (isNaN(value) || value <= 0)
|
|
13
|
+
throw new TypeError(errors.numberType.replace("{received}", typeof value));
|
|
14
|
+
|
|
15
|
+
await this.create(table);
|
|
16
|
+
let data = (await this.get(table, key)) || 0;
|
|
17
|
+
if (isNaN(data)) throw new TypeError(errors.notNumber.replace("{key}", key));
|
|
18
|
+
return await this.set(key, Number(data) + Number(value), table);
|
|
19
|
+
};
|
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(table){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
module.exports = async function (table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
|
|
9
|
+
let all = await this.query(`SELECT * from \`${table}\``);
|
|
10
|
+
let res = [];
|
|
11
|
+
all.forEach((obj) => {
|
|
12
|
+
obj.ID = obj.key_name;
|
|
13
|
+
let data = obj.value;
|
|
14
|
+
if (!isNaN(data)) data = Number(data);
|
|
15
|
+
try {
|
|
16
|
+
data = JSON.parse(data);
|
|
17
|
+
} catch (e) {}
|
|
18
|
+
obj.data = data;
|
|
19
|
+
delete obj.id;
|
|
20
|
+
delete obj.key_name;
|
|
21
|
+
delete obj.value;
|
|
22
|
+
res.push(obj);
|
|
23
|
+
});
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
5
|
+
module.exports = async function (number, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (number == null || typeof number !== "number")
|
|
9
|
+
throw new TypeError(errors.number.replace("{received}", typeof number));
|
|
10
|
+
if (isNaN(number) || number < 1)
|
|
11
|
+
throw new TypeError(errors.numberType.replace("{received}", typeof number));
|
|
12
|
+
|
|
13
|
+
await this.query(`ALTER TABLE \`${table}\` AUTO_INCREMENT = ${number};`);
|
|
14
|
+
let res = await this.stats(table);
|
|
15
|
+
return res.Auto_increment;
|
|
16
|
+
};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
module.exports = async function (key, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
|
|
11
|
+
let data = await this.get(table, key);
|
|
12
|
+
if (typeof data !== "string") throw new TypeError(errors.baseNotString);
|
|
13
|
+
return Buffer.from(data, "base64").toString("binary");
|
|
14
|
+
};
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
module.exports = async function (key, value, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
if (typeof value !== "string") throw new TypeError(errors.baseNotString);
|
|
13
|
+
|
|
14
|
+
await this.set(
|
|
15
|
+
table,
|
|
16
|
+
key,
|
|
17
|
+
Buffer.from(value, "binary").toString("base64"),
|
|
18
|
+
true
|
|
19
|
+
);
|
|
20
|
+
return this.base_get(table, key);
|
|
21
|
+
};
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(table){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
module.exports = async function (table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
|
|
9
|
+
let tables = await this.tables();
|
|
10
|
+
if (!tables.includes(table)) return false;
|
|
11
|
+
let all = await this.all(table);
|
|
12
|
+
if (!all.length) return false;
|
|
13
|
+
await this.drop(table, true);
|
|
14
|
+
this.emit("tableClear", table);
|
|
15
|
+
return this.create(table, true);
|
|
16
|
+
};
|
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const unset = require(
|
|
4
|
-
const errors = require(
|
|
3
|
+
const unset = require("lodash/unset");
|
|
4
|
+
const errors = require("../errors/strings.js");
|
|
5
5
|
|
|
6
|
-
module.exports = async function(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
6
|
+
module.exports = async function (key, table = "default") {
|
|
7
|
+
if (!key || typeof key !== "string")
|
|
8
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
9
|
+
|
|
10
|
+
let res = true;
|
|
11
|
+
let keys = key.split(".");
|
|
12
|
+
if (keys.length > 1) {
|
|
13
|
+
key = keys.shift();
|
|
14
|
+
let data = (await this.get(table, key)) || {};
|
|
15
|
+
if (typeof data !== "object")
|
|
16
|
+
throw new TypeError(errors.targetNotObject.replace("{key}", key));
|
|
17
|
+
res = unset(data, keys.join("."));
|
|
18
|
+
await this.set(key, data, table);
|
|
19
|
+
} else {
|
|
20
|
+
let oldData = await this.get(table, key);
|
|
21
|
+
await this.query(`DELETE FROM \`${table}\` WHERE key_name = '${key}'`);
|
|
22
|
+
this.emit("dataModification", {
|
|
23
|
+
oldData,
|
|
24
|
+
newData: null,
|
|
25
|
+
type: "DELETE",
|
|
26
|
+
table,
|
|
27
|
+
modifiedAt: Date.now(),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return res;
|
|
31
|
+
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
module.exports = async function (key, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
|
|
11
|
+
let tables = await this.tables();
|
|
12
|
+
if (!tables.includes(table)) return false;
|
|
13
|
+
let data = await this.get(table, key);
|
|
14
|
+
return data != null ? true : false;
|
|
15
|
+
};
|
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const get = require(
|
|
4
|
-
const errors = require(
|
|
3
|
+
const get = require("lodash/get");
|
|
4
|
+
const errors = require("../errors/strings.js");
|
|
5
5
|
|
|
6
|
-
module.exports = async function(
|
|
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
|
-
}
|
|
6
|
+
module.exports = async function (key, table = "default") {
|
|
7
|
+
if (!key || typeof key !== "string")
|
|
8
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
9
|
+
|
|
10
|
+
let tables = await this.tables();
|
|
11
|
+
if (!tables.includes(table)) return null;
|
|
12
|
+
|
|
13
|
+
let keys = key.split("."),
|
|
14
|
+
keys2 = key.split(".");
|
|
15
|
+
if (keys.length > 1) {
|
|
16
|
+
key = keys.shift();
|
|
17
|
+
}
|
|
18
|
+
let res = await this.query({
|
|
19
|
+
sql: `SELECT value FROM \`${table}\` WHERE \`key_name\` = ?`,
|
|
20
|
+
values: [key],
|
|
21
|
+
});
|
|
22
|
+
if (!res.length) return null;
|
|
23
|
+
let value = null;
|
|
24
|
+
if (res.length) {
|
|
25
|
+
value = res[0].value;
|
|
26
|
+
if (!isNaN(value)) {
|
|
27
|
+
value = Number(value);
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
value = JSON.parse(value);
|
|
31
|
+
} catch (e) {}
|
|
32
|
+
}
|
|
33
|
+
if (keys2.length > 1 && typeof value === "object") {
|
|
34
|
+
value = get(value, keys.join("."));
|
|
35
|
+
if (value == undefined) value = null;
|
|
36
|
+
} else if (keys2.length > 1) {
|
|
37
|
+
throw new ReferenceError(errors.targetNotObject.replace("{key}", key));
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
};
|
|
@@ -1,40 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const get = require(
|
|
4
|
-
const errors = require(
|
|
3
|
+
const get = require("lodash/get");
|
|
4
|
+
const errors = require("../errors/strings.js");
|
|
5
5
|
|
|
6
|
-
module.exports = async function(
|
|
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
|
-
}
|
|
6
|
+
module.exports = async function (key, table = "default") {
|
|
7
|
+
if (!table || typeof table !== "string")
|
|
8
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
9
|
+
if (!key || typeof key !== "string")
|
|
10
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
11
|
+
|
|
12
|
+
let tables = await this.tables();
|
|
13
|
+
if (!tables.includes(table)) return null;
|
|
14
|
+
|
|
15
|
+
let keys = key.split("."),
|
|
16
|
+
keys2 = key.split(".");
|
|
17
|
+
if (keys.length > 1) {
|
|
18
|
+
key = keys.shift();
|
|
19
|
+
}
|
|
20
|
+
let res = await this.query({
|
|
21
|
+
sql: `SELECT value FROM \`${table}\` WHERE \`key_name\` = ?`,
|
|
22
|
+
values: [key],
|
|
23
|
+
});
|
|
24
|
+
if (!res.length) return null;
|
|
25
|
+
let value = null;
|
|
26
|
+
if (res.length) {
|
|
27
|
+
value = res[0].value;
|
|
28
|
+
if (!isNaN(value)) {
|
|
29
|
+
value = Number(value);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
value = JSON.parse(value);
|
|
33
|
+
} catch (e) {}
|
|
34
|
+
}
|
|
35
|
+
if (keys2.length > 1 && typeof value === "object") {
|
|
36
|
+
value = get(value, keys.join("."));
|
|
37
|
+
if (value == undefined) value = null;
|
|
38
|
+
} else if (keys2.length > 1) {
|
|
39
|
+
throw new ReferenceError(errors.targetNotObject.replace("{key}", key));
|
|
40
|
+
}
|
|
41
|
+
return value ? false : true;
|
|
42
|
+
};
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
module.exports = async function (key, value, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
|
|
13
|
+
let data = (await this.get(table, key)) || [];
|
|
14
|
+
if (!Array.isArray(data))
|
|
15
|
+
throw new TypeError(errors.array.replace("{key}", key));
|
|
16
|
+
return data.includes(value);
|
|
17
|
+
};
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
5
|
+
module.exports = async function (key, value, table = "default", option) {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
|
|
13
|
+
let data = await this.get(table, key);
|
|
14
|
+
if (!data) throw new TypeError(errors.dataNotFound.replace("{key}", key));
|
|
15
|
+
if (!Array.isArray(data))
|
|
16
|
+
throw new TypeError(errors.array.replace("{key}", key));
|
|
17
|
+
if (option && (option === true || option.toLowerCase() === "all")) {
|
|
18
|
+
data = data.filter((obj) => obj !== value);
|
|
19
|
+
} else {
|
|
20
|
+
if (data.includes(value)) data.splice(data.indexOf(value), 1);
|
|
21
|
+
}
|
|
22
|
+
return await this.set(key, data, table);
|
|
23
|
+
};
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
5
|
+
module.exports = async function (key, value, table = "default", option) {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
|
|
13
|
+
await this.create(table);
|
|
14
|
+
let data = (await this.get(table, key)) || [];
|
|
15
|
+
if (!Array.isArray(data))
|
|
16
|
+
throw new TypeError(errors.array.replace("{key}", key));
|
|
17
|
+
if (option === true) {
|
|
18
|
+
if (!data.includes(value)) data.push(value);
|
|
19
|
+
} else {
|
|
20
|
+
data.push(value);
|
|
21
|
+
}
|
|
22
|
+
return await this.set(key, data, table);
|
|
23
|
+
};
|
|
@@ -1,48 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const set = require(
|
|
4
|
-
const errors = require(
|
|
5
|
-
|
|
6
|
-
module.exports = async function(
|
|
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
|
-
|
|
3
|
+
const set = require("lodash/set");
|
|
4
|
+
const errors = require("../errors/strings.js");
|
|
5
|
+
|
|
6
|
+
module.exports = async function (key, value, table = "default") {
|
|
7
|
+
if (!table || typeof table !== "string")
|
|
8
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
9
|
+
if (!key || typeof key !== "string")
|
|
10
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
13
|
+
|
|
14
|
+
await this.create(table);
|
|
15
|
+
|
|
16
|
+
let oldData = await this.get(table, key);
|
|
17
|
+
|
|
18
|
+
let keys = key.split("."),
|
|
19
|
+
keys2 = key.split(".");
|
|
20
|
+
if (keys.length > 1) key = keys.shift();
|
|
21
|
+
|
|
22
|
+
let data = await this.query({
|
|
23
|
+
sql: `SELECT value FROM \`${table}\` WHERE \`key_name\` = ?`,
|
|
24
|
+
values: [key],
|
|
25
|
+
});
|
|
26
|
+
if (!data.length) {
|
|
27
|
+
await this.query({
|
|
28
|
+
sql: `INSERT INTO \`${table}\` (\`key_name\`, \`value\`) VALUES (?, ?)`,
|
|
29
|
+
values: [key, JSON.stringify({})],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
data = (await this.get(table, key)) || {};
|
|
33
|
+
if (keys2.length > 1 && typeof data === "object") {
|
|
34
|
+
value = set(data, keys.join("."), value);
|
|
35
|
+
} else if (keys2.length > 1) {
|
|
36
|
+
throw new ReferenceError(errors.targetNotObject.replace("{key}", key));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
value = JSON.stringify(value);
|
|
41
|
+
} catch (e) {}
|
|
42
|
+
await this.query({
|
|
43
|
+
sql: `UPDATE \`${table}\` SET \`value\` = ? WHERE \`key_name\` = ?`,
|
|
44
|
+
values: [value, key],
|
|
45
|
+
});
|
|
46
|
+
let modifiedAt = Date.now();
|
|
47
|
+
|
|
48
|
+
let newData = await this.get(
|
|
49
|
+
table,
|
|
50
|
+
keys2.length > 1 ? key + "." + keys.join(".") : key
|
|
51
|
+
);
|
|
52
|
+
this.emit("dataModification", {
|
|
53
|
+
oldData,
|
|
54
|
+
newData,
|
|
55
|
+
type: oldData == null && newData != null ? "SET" : "UPDATE",
|
|
56
|
+
table,
|
|
57
|
+
modifiedAt,
|
|
58
|
+
});
|
|
59
|
+
return newData;
|
|
60
|
+
};
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(table){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
5
|
+
module.exports = async function (table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
|
|
9
|
+
let res = await this.query(
|
|
10
|
+
`SHOW TABLE STATUS FROM ${this.db.pool.config.connectionConfig.database} WHERE name LIKE '${table}';`
|
|
11
|
+
);
|
|
12
|
+
return res[0] || null;
|
|
13
|
+
};
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const errors = require(
|
|
3
|
+
const errors = require("../errors/strings.js");
|
|
4
4
|
|
|
5
|
-
module.exports = async function(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return res;
|
|
21
|
-
}
|
|
5
|
+
module.exports = async function (key, value, table = "default") {
|
|
6
|
+
if (!table || typeof table !== "string")
|
|
7
|
+
throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
8
|
+
if (!key || typeof key !== "string")
|
|
9
|
+
throw new TypeError(errors.key.replace("{received}", typeof key));
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
throw new TypeError(errors.value.replace("{received}", typeof value));
|
|
12
|
+
if (isNaN(value) || value <= 0)
|
|
13
|
+
throw new TypeError(errors.numberType.replace("{received}", typeof value));
|
|
14
|
+
|
|
15
|
+
await this.create(table);
|
|
16
|
+
let data = (await this.get(table, key)) || 0;
|
|
17
|
+
if (isNaN(data)) throw new TypeError(errors.notNumber.replace("{key}", key));
|
|
18
|
+
return await this.set(key, data - Number(value), table);
|
|
19
|
+
};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -229,17 +229,56 @@ ZeroHelper provides multiple database utilities for seamless integration with va
|
|
|
229
229
|
console.log("Database Connected");
|
|
230
230
|
});
|
|
231
231
|
|
|
232
|
-
await db.set("
|
|
233
|
-
await db.
|
|
234
|
-
await db.delete("table", "foo");
|
|
232
|
+
await db.set("key", "value"); // Uses the default table
|
|
233
|
+
await db.set("key", "value", "custom_table"); // Uses the specified table
|
|
235
234
|
|
|
236
|
-
await db.
|
|
237
|
-
await db.
|
|
235
|
+
const value = await db.get("key"); // Uses the default table
|
|
236
|
+
const valueInCustomTable = await db.get("key", "custom_table"); // Uses the specified table
|
|
238
237
|
|
|
239
|
-
|
|
240
|
-
|
|
238
|
+
await db.add("count", 10); // Uses the default table
|
|
239
|
+
await db.add("count", 10, "custom_table"); // Uses the specified table
|
|
241
240
|
|
|
242
|
-
|
|
241
|
+
await db.sub("count", 5); // Uses the default table
|
|
242
|
+
await db.sub("count", 5, "custom_table"); // Uses the specified table
|
|
243
|
+
|
|
244
|
+
await db.push("array", "value"); // Uses the default table
|
|
245
|
+
await db.push("array", "value", "custom_table"); // Uses the specified table
|
|
246
|
+
|
|
247
|
+
await db.pull("array", "value"); // Uses the default table
|
|
248
|
+
await db.pull("array", "value", "custom_table"); // Uses the specified table
|
|
249
|
+
|
|
250
|
+
await db.delete("key"); // Uses the default table
|
|
251
|
+
await db.delete("key", "custom_table"); // Uses the specified table
|
|
252
|
+
|
|
253
|
+
const exists = await db.exists("key"); // Uses the default table
|
|
254
|
+
const existsInCustomTable = await db.exists("key", "custom_table"); // Uses the specified table
|
|
255
|
+
|
|
256
|
+
const includes = await db.includes("array", "value"); // Uses the default table
|
|
257
|
+
const includesInCustomTable = await db.includes(
|
|
258
|
+
"array",
|
|
259
|
+
"value",
|
|
260
|
+
"custom_table"
|
|
261
|
+
); // Uses the specified table
|
|
262
|
+
|
|
263
|
+
const allData = await db.all(); // Uses the default table
|
|
264
|
+
const allDataInCustomTable = await db.all("custom_table"); // Uses the specified table
|
|
265
|
+
|
|
266
|
+
await db.clear(); // Clears the default table
|
|
267
|
+
await db.clear("custom_table"); // Clears the specified table
|
|
268
|
+
|
|
269
|
+
await db.drop(); // Drops the default table
|
|
270
|
+
await db.drop("custom_table"); // Drops the specified table
|
|
271
|
+
|
|
272
|
+
await db.rename("old_table", "new_table");
|
|
273
|
+
|
|
274
|
+
const ping = await db.ping();
|
|
275
|
+
console.log(`Ping: ${ping}ms`);
|
|
276
|
+
|
|
277
|
+
// Sets MySQL global variables
|
|
278
|
+
await db.variables({
|
|
279
|
+
max_connections: 100000,
|
|
280
|
+
wait_timeout: 60,
|
|
281
|
+
});
|
|
243
282
|
})();
|
|
244
283
|
```
|
|
245
284
|
|