@onurege3467/zerohelper 5.0.3 → 6.0.1
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/data/test_db.json +3 -0
- package/data/test_db.sqlite +0 -0
- package/data/test_db_cached.sqlite +0 -0
- package/database/cacheWrapper.js +121 -0
- package/database/index.js +24 -6
- package/database/{adapters/json.js → json.js} +9 -9
- package/database/{adapters/mongodb.js → mongodb.js} +1 -0
- package/database/{adapters/mysql.js → mysql.js} +12 -12
- package/database/{adapters/sqlite.js → sqlite.js} +86 -77
- package/functions/index.js +14 -4
- package/package.json +4 -3
- package/readme.md +111 -324
- package/test.js +244 -0
- package/database/csvdb/index.js +0 -90
- package/database/jsondatabase/index.js +0 -132
- package/database/migrate/index.js +0 -68
- package/database/mongodb/index.js +0 -49
- package/database/mongodb/src/client/Client.js +0 -37
- package/database/mongodb/src/structers/Collection.js +0 -136
- package/database/mongodb/src/structers/Data.js +0 -282
- package/database/mongodb/src/structers/Database.js +0 -53
- package/database/mongodb/src/tools/FormatTool.js +0 -5
- package/database/mysql/examples/example.js +0 -301
- package/database/mysql/index.js +0 -1
- package/database/mysql/structures/classes/MySQL.js +0 -41
- package/database/mysql/structures/errors/strings.js +0 -23
- package/database/mysql/structures/methods/add.js +0 -19
- package/database/mysql/structures/methods/all.js +0 -25
- package/database/mysql/structures/methods/auto_increment.js +0 -16
- package/database/mysql/structures/methods/base_get.js +0 -14
- package/database/mysql/structures/methods/base_set.js +0 -21
- package/database/mysql/structures/methods/clear.js +0 -16
- package/database/mysql/structures/methods/connect.js +0 -15
- package/database/mysql/structures/methods/create.js +0 -11
- package/database/mysql/structures/methods/create_db.js +0 -10
- package/database/mysql/structures/methods/delete.js +0 -31
- package/database/mysql/structures/methods/drop.js +0 -13
- package/database/mysql/structures/methods/end.js +0 -7
- package/database/mysql/structures/methods/exists.js +0 -15
- package/database/mysql/structures/methods/get.js +0 -40
- package/database/mysql/structures/methods/getAllData.js +0 -35
- package/database/mysql/structures/methods/has.js +0 -42
- package/database/mysql/structures/methods/includes.js +0 -17
- package/database/mysql/structures/methods/ping.js +0 -11
- package/database/mysql/structures/methods/process.js +0 -7
- package/database/mysql/structures/methods/pull.js +0 -23
- package/database/mysql/structures/methods/push.js +0 -23
- package/database/mysql/structures/methods/query.js +0 -9
- package/database/mysql/structures/methods/rename.js +0 -16
- package/database/mysql/structures/methods/set.js +0 -60
- package/database/mysql/structures/methods/stats.js +0 -13
- package/database/mysql/structures/methods/sub.js +0 -19
- package/database/mysql/structures/methods/tables.js +0 -8
- package/database/mysql/structures/methods/variables.js +0 -20
- package/database/newMongoDB/index.js +0 -94
- package/database/newMySQL/index.js +0 -205
- package/database/newSQLite/index.js +0 -240
- package/database/postgresql/index.js +0 -150
- package/database/redis/index.js +0 -125
- package/database/sqldb/index.js +0 -243
- package/database/yamldatabase/index.js +0 -76
- /package/database/{adapters/IDatabase.js → IDatabase.js} +0 -0
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const get = require("lodash/get");
|
|
4
|
-
const errors = require("../errors/strings.js");
|
|
5
|
-
|
|
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,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require("../errors/strings.js");
|
|
4
|
-
|
|
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,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require('../errors/strings.js');
|
|
4
|
-
|
|
5
|
-
module.exports = async function(){
|
|
6
|
-
let tables = await this.tables();
|
|
7
|
-
if(!tables.length) throw new TypeError(errors.noTablesExisted);
|
|
8
|
-
let ms = Date.now();
|
|
9
|
-
await this.query(`SELECT * from \`${tables[0]}\``);
|
|
10
|
-
return Date.now() - ms;
|
|
11
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require("../errors/strings.js");
|
|
4
|
-
|
|
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,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require("../errors/strings.js");
|
|
4
|
-
|
|
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,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require('../errors/strings.js');
|
|
4
|
-
|
|
5
|
-
module.exports = async function(table, newTable){
|
|
6
|
-
if(!table || !newTable || typeof table !== "string" || typeof newTable !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
|
|
7
|
-
if(table === newTable) throw new TypeError(errors.tableRenameSameName);
|
|
8
|
-
|
|
9
|
-
let tables = await this.tables();
|
|
10
|
-
if(!tables.includes(table)) throw new TypeError(errors.tableNotFound.replace("{table}", table));
|
|
11
|
-
if(tables.includes(newTable)) throw new TypeError(errors.tableAlreadyExists.replace("{table}", newTable));
|
|
12
|
-
|
|
13
|
-
await this.query(`ALTER TABLE \`${table}\` RENAME TO \`${newTable}\``);
|
|
14
|
-
this.emit("tableRename", table, newTable);
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
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,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require("../errors/strings.js");
|
|
4
|
-
|
|
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,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require("../errors/strings.js");
|
|
4
|
-
|
|
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
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const errors = require('../errors/strings.js');
|
|
4
|
-
|
|
5
|
-
module.exports = async function(variables){
|
|
6
|
-
if(!variables) throw new TypeError(errors.variables.replace("{received}", variables));
|
|
7
|
-
if(typeof variables !== "object" || Array.isArray(variables)) throw new TypeError(errors.variablesNotObject.replace("{received}", typeof variables));
|
|
8
|
-
|
|
9
|
-
let res = {};
|
|
10
|
-
let keys = Object.keys(variables);
|
|
11
|
-
for(var i = 0; i < keys.length; i++){
|
|
12
|
-
let returned = await this.query(`SET GLOBAL ${keys[i]}=${variables[keys[i]]};`);
|
|
13
|
-
if(!returned.message){
|
|
14
|
-
res[keys[i]] = variables[keys[i]];
|
|
15
|
-
}else{
|
|
16
|
-
res[keys[i]] = returned;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return res;
|
|
20
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
const { MongoClient, ObjectId } = require("mongodb");
|
|
2
|
-
|
|
3
|
-
class MongoDBDatabase {
|
|
4
|
-
constructor(config) {
|
|
5
|
-
this.config = config;
|
|
6
|
-
this.client = new MongoClient(config.url, {
|
|
7
|
-
useNewUrlParser: true,
|
|
8
|
-
useUnifiedTopology: true,
|
|
9
|
-
});
|
|
10
|
-
this.dbPromise = this.client.connect().then(() => this.client.db(config.database));
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async ensureCollection(collection) {
|
|
14
|
-
const db = await this.dbPromise;
|
|
15
|
-
const collections = await db.listCollections({ name: collection }).toArray();
|
|
16
|
-
if (collections.length === 0) {
|
|
17
|
-
await db.createCollection(collection);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async insert(collection, data) {
|
|
22
|
-
await this.ensureCollection(collection);
|
|
23
|
-
const db = await this.dbPromise;
|
|
24
|
-
const result = await db.collection(collection).insertOne(data);
|
|
25
|
-
return result.insertedId;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async bulkInsert(collection, dataArray) {
|
|
29
|
-
if (!Array.isArray(dataArray) || dataArray.length === 0) return 0;
|
|
30
|
-
await this.ensureCollection(collection);
|
|
31
|
-
const db = await this.dbPromise;
|
|
32
|
-
const result = await db.collection(collection).insertMany(dataArray);
|
|
33
|
-
return result.insertedCount;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async update(collection, data, where) {
|
|
37
|
-
await this.ensureCollection(collection);
|
|
38
|
-
const db = await this.dbPromise;
|
|
39
|
-
const result = await db.collection(collection).updateMany(where, { $set: data });
|
|
40
|
-
return result.modifiedCount;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async updateOne(collection, data, where) {
|
|
44
|
-
await this.ensureCollection(collection);
|
|
45
|
-
const db = await this.dbPromise;
|
|
46
|
-
const result = await db.collection(collection).updateOne(where, { $set: data });
|
|
47
|
-
return result.modifiedCount;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async delete(collection, where) {
|
|
51
|
-
await this.ensureCollection(collection);
|
|
52
|
-
const db = await this.dbPromise;
|
|
53
|
-
const result = await db.collection(collection).deleteMany(where);
|
|
54
|
-
return result.deletedCount;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async deleteOne(collection, where) {
|
|
58
|
-
await this.ensureCollection(collection);
|
|
59
|
-
const db = await this.dbPromise;
|
|
60
|
-
const result = await db.collection(collection).deleteOne(where);
|
|
61
|
-
return result.deletedCount;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async select(collection, where = {}) {
|
|
65
|
-
await this.ensureCollection(collection);
|
|
66
|
-
const db = await this.dbPromise;
|
|
67
|
-
return await db.collection(collection).find(where).toArray();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async selectOne(collection, where = {}) {
|
|
71
|
-
await this.ensureCollection(collection);
|
|
72
|
-
const db = await this.dbPromise;
|
|
73
|
-
return await db.collection(collection).findOne(where);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async set(collection, data, where) {
|
|
77
|
-
await this.ensureCollection(collection);
|
|
78
|
-
const db = await this.dbPromise;
|
|
79
|
-
const existing = await db.collection(collection).findOne(where);
|
|
80
|
-
if (!existing) {
|
|
81
|
-
const result = await db.collection(collection).insertOne({ ...where, ...data });
|
|
82
|
-
return result.insertedId;
|
|
83
|
-
} else {
|
|
84
|
-
const result = await db.collection(collection).updateOne(where, { $set: data });
|
|
85
|
-
return result.modifiedCount;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async close() {
|
|
90
|
-
await this.client.close();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
module.exports = MongoDBDatabase;
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
const mysql = require("mysql2/promise");
|
|
2
|
-
|
|
3
|
-
class MySQLDatabase {
|
|
4
|
-
constructor(config) {
|
|
5
|
-
this.config = config;
|
|
6
|
-
this.pool = null;
|
|
7
|
-
|
|
8
|
-
this.poolPromise = (async () => {
|
|
9
|
-
const connection = await mysql.createConnection({
|
|
10
|
-
host: config.host,
|
|
11
|
-
port: config.port || 3306,
|
|
12
|
-
user: config.user,
|
|
13
|
-
password: config.password,
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${config.database}\``);
|
|
17
|
-
await connection.end();
|
|
18
|
-
|
|
19
|
-
this.pool = mysql.createPool({
|
|
20
|
-
host: config.host,
|
|
21
|
-
port: config.port || 3306,
|
|
22
|
-
user: config.user,
|
|
23
|
-
password: config.password,
|
|
24
|
-
database: config.database,
|
|
25
|
-
waitForConnections: true,
|
|
26
|
-
connectionLimit: config.connectionLimit || 10,
|
|
27
|
-
queueLimit: 0,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return this.pool;
|
|
31
|
-
})();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async query(sql, params = []) {
|
|
35
|
-
const pool = await this.poolPromise;
|
|
36
|
-
const [rows] = await pool.execute(sql, params);
|
|
37
|
-
return rows;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async ensureTable(table, data = {}) {
|
|
41
|
-
if (!data || Object.keys(data).length === 0) return;
|
|
42
|
-
|
|
43
|
-
const escapedTable = mysql.escape(table);
|
|
44
|
-
const tables = await this.query(`SHOW TABLES LIKE ${escapedTable}`);
|
|
45
|
-
if (tables.length === 0) {
|
|
46
|
-
const columns = Object.keys(data).map(col => `\`${col}\` VARCHAR(255)`).join(", ");
|
|
47
|
-
const createTableSQL = `
|
|
48
|
-
CREATE TABLE \`${table}\` (
|
|
49
|
-
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
50
|
-
${columns}
|
|
51
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
52
|
-
`;
|
|
53
|
-
await this.query(createTableSQL);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async insert(table, data) {
|
|
58
|
-
const copy = { ...data };
|
|
59
|
-
await this.ensureTable(table, copy);
|
|
60
|
-
const existingColumns = await this.query(`DESCRIBE \`${table}\``).catch(() => null);
|
|
61
|
-
if (!existingColumns) throw new Error(`Table ${table} does not exist.`);
|
|
62
|
-
|
|
63
|
-
const existingNames = existingColumns.map(col => col.Field);
|
|
64
|
-
for (const key of Object.keys(copy)) {
|
|
65
|
-
if (!existingNames.includes(key)) {
|
|
66
|
-
await this.query(`ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const keys = Object.keys(copy);
|
|
71
|
-
const placeholders = keys.map(() => "?").join(",");
|
|
72
|
-
const values = Object.values(copy);
|
|
73
|
-
const sql = `INSERT INTO \`${table}\` (${keys.map(k => `\`${k}\``).join(",")}) VALUES (${placeholders})`;
|
|
74
|
-
|
|
75
|
-
const result = await this.query(sql, values);
|
|
76
|
-
return result.insertId;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async update(table, data, where) {
|
|
80
|
-
await this.ensureTable(table, { ...data, ...where });
|
|
81
|
-
const existingColumns = await this.query(`DESCRIBE \`${table}\``).catch(() => null);
|
|
82
|
-
if (!existingColumns) throw new Error(`Table ${table} does not exist.`);
|
|
83
|
-
|
|
84
|
-
const existingColumnNames = existingColumns.map(col => col.Field);
|
|
85
|
-
for (const key of Object.keys(data)) {
|
|
86
|
-
if (!existingColumnNames.includes(key)) {
|
|
87
|
-
const alterSQL = `ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`;
|
|
88
|
-
await this.query(alterSQL);
|
|
89
|
-
console.log(`Added missing column '${key}' to table '${table}'`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
const setString = Object.keys(data).map(k => `\`${k}\` = ?`).join(", ");
|
|
93
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(" AND ");
|
|
94
|
-
const sql = `UPDATE \`${table}\` SET ${setString} WHERE ${whereString}`;
|
|
95
|
-
const result = await this.query(sql, [...Object.values(data), ...Object.values(where)]);
|
|
96
|
-
return result.affectedRows;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async delete(table, where) {
|
|
100
|
-
if (!where || Object.keys(where).length === 0) return 0;
|
|
101
|
-
await this.ensureTable(table, { ...where });
|
|
102
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(" AND ");
|
|
103
|
-
const sql = `DELETE FROM \`${table}\` WHERE ${whereString}`;
|
|
104
|
-
const result = await this.query(sql, Object.values(where));
|
|
105
|
-
return result.affectedRows;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async select(table, where = null) {
|
|
109
|
-
await this.ensureTable(table, where || {});
|
|
110
|
-
let sql = `SELECT * FROM \`${table}\``;
|
|
111
|
-
let params = [];
|
|
112
|
-
|
|
113
|
-
if (where && Object.keys(where).length > 0) {
|
|
114
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(" AND ");
|
|
115
|
-
sql += ` WHERE ${whereString}`;
|
|
116
|
-
params = Object.values(where);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return await this.query(sql, params);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async set(table, data, where) {
|
|
123
|
-
await this.ensureTable(table, { ...data, ...where });
|
|
124
|
-
const existingColumns = await this.query(`DESCRIBE \`${table}\``).catch(() => null);
|
|
125
|
-
if (!existingColumns) throw new Error(`Table ${table} does not exist.`);
|
|
126
|
-
|
|
127
|
-
const existingColumnNames = existingColumns.map(col => col.Field);
|
|
128
|
-
for (const key of Object.keys(data)) {
|
|
129
|
-
if (!existingColumnNames.includes(key)) {
|
|
130
|
-
const alterSQL = `ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`;
|
|
131
|
-
await this.query(alterSQL);
|
|
132
|
-
console.log(`Added missing column '${key}' to table '${table}'`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const existing = await this.select(table, where);
|
|
136
|
-
if (existing.length === 0) {
|
|
137
|
-
return await this.insert(table, { ...where, ...data });
|
|
138
|
-
} else {
|
|
139
|
-
return await this.update(table, data, where);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async selectOne(table, where = null) {
|
|
144
|
-
const results = await this.select(table, where);
|
|
145
|
-
return results[0] || null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async deleteOne(table, where) {
|
|
149
|
-
const row = await this.selectOne(table, where);
|
|
150
|
-
if (!row) return 0;
|
|
151
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(" AND ");
|
|
152
|
-
const sql = `DELETE FROM \`${table}\` WHERE ${whereString} LIMIT 1`;
|
|
153
|
-
const result = await this.query(sql, Object.values(where));
|
|
154
|
-
return result.affectedRows;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async updateOne(table, data, where) {
|
|
158
|
-
await this.ensureTable(table, { ...data, ...where });
|
|
159
|
-
const existingColumns = await this.query(`DESCRIBE \`${table}\``).catch(() => null);
|
|
160
|
-
if (!existingColumns) throw new Error(`Table ${table} does not exist.`);
|
|
161
|
-
|
|
162
|
-
const existingColumnNames = existingColumns.map(col => col.Field);
|
|
163
|
-
for (const key of Object.keys(data)) {
|
|
164
|
-
if (!existingColumnNames.includes(key)) {
|
|
165
|
-
const alterSQL = `ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`;
|
|
166
|
-
await this.query(alterSQL);
|
|
167
|
-
console.log(`Added missing column '${key}' to table '${table}'`);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const setString = Object.keys(data).map(k => `\`${k}\` = ?`).join(", ");
|
|
171
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(" AND ");
|
|
172
|
-
const sql = `UPDATE \`${table}\` SET ${setString} WHERE ${whereString} LIMIT 1`;
|
|
173
|
-
const result = await this.query(sql, [...Object.values(data), ...Object.values(where)]);
|
|
174
|
-
return result.affectedRows;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async bulkInsert(table, dataArray) {
|
|
178
|
-
if (!Array.isArray(dataArray) || dataArray.length === 0) return 0;
|
|
179
|
-
await this.ensureTable(table, dataArray[0]);
|
|
180
|
-
|
|
181
|
-
const existingColumns = await this.query(`DESCRIBE \`${table}\``);
|
|
182
|
-
const existingColumnNames = existingColumns.map(col => col.Field);
|
|
183
|
-
const keys = Object.keys(dataArray[0]);
|
|
184
|
-
|
|
185
|
-
// Eksik kolonları sadece ilk elemana göre kontrol et
|
|
186
|
-
for (const key of keys) {
|
|
187
|
-
if (!existingColumnNames.includes(key)) {
|
|
188
|
-
await this.query(`ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const placeholders = dataArray.map(() => `(${keys.map(() => '?').join(',')})`).join(',');
|
|
193
|
-
const values = dataArray.flatMap(obj => keys.map(k => obj[k]));
|
|
194
|
-
const sql = `INSERT INTO \`${table}\` (${keys.map(k => `\`${k}\``).join(",")}) VALUES ${placeholders}`;
|
|
195
|
-
|
|
196
|
-
const result = await this.query(sql, values);
|
|
197
|
-
return result.affectedRows;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async close() {
|
|
201
|
-
if (this.pool) await this.pool.end();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
module.exports = MySQLDatabase;
|