@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.
@@ -1,21 +1,19 @@
1
1
  "use strict";
2
2
 
3
- const errors = require('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
- if(isNaN(value) || value <= 0) throw new TypeError(errors.numberType.replace("{received}", typeof value));
10
-
11
- let res;
12
- let tables = await this.tables();
13
- if(!tables.includes(table)){
14
- res = await this.set(table, key, Number(value));
15
- }else{
16
- let data = await this.get(table, key) || 0;
17
- if(isNaN(data)) throw new TypeError(errors.notNumber.replace("{key}", key));
18
- res = await this.set(table, key, Number(data) + Number(value));
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
-
8
- let all = await this.query(`SELECT * from \`${table}\``);
9
- let res = [];
10
- all.forEach(obj => {
11
- obj.ID = obj.key_name
12
- let data = obj.value;
13
- if(!isNaN(data)) data = Number(data);
14
- try{
15
- data = JSON.parse(data);
16
- }catch(e){}
17
- obj.data = data;
18
- delete obj.id;
19
- delete obj.key_name;
20
- delete obj.value;
21
- res.push(obj);
22
- })
23
- return res;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, number){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(number == null || typeof number !== "number") throw new TypeError(errors.number.replace("{received}", typeof number));
8
- if(isNaN(number) || number < 1) throw new TypeError(errors.numberType.replace("{received}", typeof number));
9
-
10
- await this.query(`ALTER TABLE \`${table}\` AUTO_INCREMENT = ${number};`);
11
- let res = await this.stats(table);
12
- return res.Auto_increment;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
-
9
- let data = await this.get(table, key);
10
- if(typeof data !== "string") throw new TypeError(errors.baseNotString);
11
- return Buffer.from(data, 'base64').toString('binary');
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
- if(typeof value !== "string") throw new TypeError(errors.baseNotString);
10
-
11
- await this.set(table, key, Buffer.from(value, 'binary').toString('base64'), true);
12
- return this.base_get(table, key);
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
-
8
- let tables = await this.tables();
9
- if(!tables.includes(table)) return false;
10
- let all = await this.all(table);
11
- if(!all.length) return false;
12
- await this.drop(table, true);
13
- this.emit("tableClear", table);
14
- return this.create(table, true);
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('lodash/unset');
4
- const errors = require('../errors/strings.js');
3
+ const unset = require("lodash/unset");
4
+ const errors = require("../errors/strings.js");
5
5
 
6
- module.exports = async function(table, key){
7
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
8
- if(!key || typeof key !== "string") 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') throw new TypeError(errors.targetNotObject.replace("{key}", key));
16
- res = unset(data, keys.join("."));
17
- await this.set(table, key, data);
18
- }else{
19
- let oldData = await this.get(table, key);
20
- await this.query(`DELETE FROM \`${table}\` WHERE key_name = '${key}'`);
21
- this.emit("dataModification", {oldData, newData: null, type: "DELETE", table, modifiedAt: Date.now()});
22
- }
23
- return res;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
-
9
- let tables = await this.tables();
10
- if(!tables.includes(table)) return false;
11
- let data = await this.get(table, key);
12
- return (data != null) ? true : false;
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('lodash/get');
4
- const errors = require('../errors/strings.js');
3
+ const get = require("lodash/get");
4
+ const errors = require("../errors/strings.js");
5
5
 
6
- module.exports = async function(table, key){
7
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
8
- if(!key || typeof key !== "string") 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
- }
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('lodash/get');
4
- const errors = require('../errors/strings.js');
3
+ const get = require("lodash/get");
4
+ const errors = require("../errors/strings.js");
5
5
 
6
- module.exports = async function(table, key){
7
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
8
- if(!key || typeof key !== "string") 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 ? false : true;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
-
10
- let data = await this.get(table, key) || [];
11
- if(!Array.isArray(data)) throw new TypeError(errors.array.replace("{key}", key));
12
- return data.includes(value);
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value, option){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
-
10
- let data = await this.get(table, key);
11
- if(!data) throw new TypeError(errors.dataNotFound.replace("{key}", key));
12
- if(!Array.isArray(data)) throw new TypeError(errors.array.replace("{key}", key));
13
- if(option && (option === true || option.toLowerCase() === "all")){
14
- data = data.filter(obj => obj !== value);
15
- }else{
16
- if(data.includes(value)) data.splice(data.indexOf(value), 1);
17
- }
18
- return this.set(table, key, data);
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value, option){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
-
10
- await this.create(table);
11
-
12
- let data = await this.get(table, key) || [];
13
- if(!Array.isArray(data)) throw new TypeError(errors.array.replace("{key}", key));
14
- if(option === true){
15
- if(!data.includes(value)) data.push(value);
16
- }else{
17
- data.push(value);
18
- }
19
- return this.set(table, key, data);
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('lodash/set');
4
- const errors = require('../errors/strings.js');
5
-
6
- module.exports = async function(table, key, value, isBaseValue){
7
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
8
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
9
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
10
-
11
- await this.create(table);
12
-
13
- let oldData = await this.get(table, key);
14
-
15
- let keys = key.split('.'),
16
- keys2 = key.split('.');
17
- if(keys.length > 1) key = keys.shift();
18
-
19
- let data = await this.query({
20
- sql: `SELECT value FROM \`${table}\` WHERE \`key_name\` = ?`,
21
- values: [key]
22
- });
23
- if(!data.length){
24
- await this.query({
25
- sql: `INSERT INTO \`${table}\` (\`key_name\`, \`value\`) VALUES (?, ?)`,
26
- values: [key,JSON.stringify({})]
27
- });
28
- }
29
- data = await this.get(table, key) || {};
30
- if(keys2.length > 1 && typeof data === 'object'){
31
- value = set(data, keys.join("."), value);
32
- }else if(keys2.length > 1){
33
- throw new ReferenceError(errors.targetNotObject.replace("{key}", key));
34
- }
35
-
36
- try{
37
- value = JSON.stringify(value);
38
- }catch(e){}
39
- await this.query({
40
- sql: `UPDATE \`${table}\` SET \`value\` = ? WHERE \`key_name\` = ?`,
41
- values: [value,key]
42
- });
43
- let modifiedAt = Date.now()
44
-
45
- let newData = await this.get(table, (keys2.length > 1) ? key + "." + keys.join(".") : key);
46
- this.emit("dataModification", {oldData: (isBaseValue ? Buffer.from(oldData, 'base64').toString('binary') : oldData), newData: (isBaseValue ? Buffer.from(newData, 'base64').toString('binary') : newData), type: (oldData == null && newData != null) ? "SET" : "UPDATE", table, modifiedAt});
47
- return newData;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
-
8
- let res = await this.query(`SHOW TABLE STATUS FROM ${this.db.pool.config.connectionConfig.database} WHERE name LIKE '${table}';`);
9
- return res[0] || null;
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('../errors/strings.js');
3
+ const errors = require("../errors/strings.js");
4
4
 
5
- module.exports = async function(table, key, value){
6
- if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7
- if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8
- if(value === undefined) throw new TypeError(errors.value.replace("{received}", typeof value));
9
- if(isNaN(value) || value <= 0) throw new TypeError(errors.numberType.replace("{received}", typeof value));
10
-
11
- let res;
12
- let tables = await this.tables();
13
- if(!tables.includes(table)){
14
- res = await this.set(table, key, 0 - Number(value));
15
- }else{
16
- let data = await this.get(table, key) || 0;
17
- if(isNaN(data)) throw new TypeError(errors.notNumber.replace("{key}", key));
18
- res = await this.set(table, key, data - Number(value));
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"
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("table", "foo", "bar");
233
- await db.push("table", "array", "x");
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.add("table", "number", 1);
237
- await db.sub("table", "number", 1);
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
- console.log(await db.get("table", "foo"));
240
- console.log(await db.has("table", "foo"));
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
- console.log(await db.ping());
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