@onurege3467/zerohelper 4.0.1 → 4.1.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.
@@ -0,0 +1,158 @@
1
+ const mysql = require("mysql2/promise");
2
+
3
+ class MySQLDatabase {
4
+ /**
5
+ *
6
+ * @param {Object} config
7
+ * @param {string} config.host
8
+ * @param {number} config.port
9
+ * @param {string} config.user
10
+ * @param {string} config.password
11
+ * @param {string} config.database
12
+ * @param {number} [config.connectionLimit]
13
+ */
14
+ constructor(config) {
15
+ this.pool = mysql.createPool({
16
+ host: config.host,
17
+ port: config.port || 3306,
18
+ user: config.user,
19
+ password: config.password,
20
+ database: config.database,
21
+ waitForConnections: true,
22
+ connectionLimit: config.connectionLimit || 10,
23
+ queueLimit: 0,
24
+ });
25
+ }
26
+
27
+ // Genel sorgu çalıştırma (select, insert, update, delete, vs.)
28
+ async query(sql, params = []) {
29
+ const [rows, fields] = await this.pool.execute(sql, params);
30
+ return rows;
31
+ }
32
+
33
+ async ensureTable(table, data) {
34
+ // Tablo adını escape et
35
+ const escapedTable = mysql.escape(table); // string olarak tırnaklı, örn: 'users'
36
+
37
+ // SHOW TABLES LIKE 'tableName' şeklinde sorgu yaz, parametre yerine direkt string kullan
38
+ const sql = `SHOW TABLES LIKE ${escapedTable}`;
39
+ const tables = await this.query(sql);
40
+
41
+ if (tables.length === 0) {
42
+ const columns = Object.keys(data).map(col => `\`${col}\` VARCHAR(255)`).join(", ");
43
+
44
+ const createTableSQL = `
45
+ CREATE TABLE \`${table}\` (
46
+ id INT PRIMARY KEY AUTO_INCREMENT,
47
+ ${columns}
48
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
49
+ `;
50
+
51
+ await this.query(createTableSQL);
52
+ }
53
+ }
54
+
55
+ // Yeni kayıt ekleme (tablo, objede kolonlar ve değerler)
56
+ async insert(table, data) {
57
+ await this.ensureTable(table, {...data});
58
+ // Önce tablo kolonlarını öğren
59
+ const existingColumns = await this.query(`DESCRIBE \`${table}\``).catch(() => null);
60
+
61
+ if (!existingColumns) {
62
+ throw new Error(`Table ${table} does not exist.`);
63
+ }
64
+
65
+ // Varolan kolon isimlerini al
66
+ const existingColumnNames = existingColumns.map(col => col.Field);
67
+
68
+ // data'daki kolonlar varsa, olmayanları ekle
69
+ for (const key of Object.keys(data)) {
70
+ if (!existingColumnNames.includes(key)) {
71
+ // Burada kolonu VARCHAR(255) olarak ekliyoruz, istersen bunu özelleştirebilirsin
72
+ const alterSQL = `ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` VARCHAR(255)`;
73
+ await this.query(alterSQL);
74
+ console.log(`Added missing column '${key}' to table '${table}'`);
75
+ }
76
+ }
77
+
78
+ // Şimdi insert işlemini yap
79
+ const keys = Object.keys(data);
80
+ const placeholders = keys.map(() => "?").join(",");
81
+ const values = Object.values(data);
82
+
83
+ const sql = `INSERT INTO \`${table}\` (${keys.map(k => `\`${k}\``).join(",")}) VALUES (${placeholders})`;
84
+
85
+ const result = await this.query(sql, values);
86
+ return result.insertId;
87
+ }
88
+
89
+ // Kayıt güncelleme (tablo, data, koşul)
90
+ // where şartı basit objeyle: {id: 1, status: 'active'}
91
+ async update(table, data, where) {
92
+ await this.ensureTable(table, {...data, ...where});
93
+ const setKeys = Object.keys(data);
94
+ const setValues = Object.values(data);
95
+ const whereKeys = Object.keys(where);
96
+ const whereValues = Object.values(where);
97
+
98
+ const setString = setKeys.map(k => `\`${k}\` = ?`).join(", ");
99
+ const whereString = whereKeys.map(k => `\`${k}\` = ?`).join(" AND ");
100
+
101
+ const sql = `UPDATE \`${table}\` SET ${setString} WHERE ${whereString}`;
102
+
103
+ const result = await this.query(sql, [...setValues, ...whereValues]);
104
+ return result.affectedRows;
105
+ }
106
+
107
+ // Kayıt silme (tablo, koşul)
108
+ async delete(table, where) {
109
+ await this.ensureTable(table, {...where});
110
+ const whereKeys = Object.keys(where);
111
+ const whereValues = Object.values(where);
112
+ const whereString = whereKeys.map(k => `\`${k}\` = ?`).join(" AND ");
113
+
114
+ const sql = `DELETE FROM \`${table}\` WHERE ${whereString}`;
115
+ const result = await this.query(sql, whereValues);
116
+ return result.affectedRows;
117
+ }
118
+
119
+ // Kayıtları çekme (tablo, koşul veya null)
120
+ async select(table, where = null) {
121
+ await this.ensureTable(table, {...where});
122
+ let sql = `SELECT * FROM \`${table}\``;
123
+ let params = [];
124
+
125
+ if (where && Object.keys(where).length > 0) {
126
+ const whereKeys = Object.keys(where);
127
+ const whereValues = Object.values(where);
128
+ const whereString = whereKeys.map(k => `\`${k}\` = ?`).join(" AND ");
129
+ sql += ` WHERE ${whereString}`;
130
+ params = whereValues;
131
+ }
132
+
133
+ return await this.query(sql, params);
134
+ }
135
+
136
+ // Set (insert veya update)
137
+ async set(table, data, where) {
138
+ await this.ensureTable(table, {...data, ...where});
139
+ // where ile arama yap, kayıt var mı kontrol et
140
+ const existing = await this.select(table, where);
141
+
142
+ if (existing.length === 0) {
143
+ // Kayıt yoksa: insert et
144
+ // insert fonksiyonumuz otomatik kolon eklediği için onu kullanabiliriz
145
+ return await this.insert(table, {...where, ...data});
146
+ } else {
147
+ // Kayıt varsa: update et
148
+ return await this.update(table, data, where);
149
+ }
150
+ }
151
+
152
+ // Bağlantıyı kapatma
153
+ async close() {
154
+ await this.pool.end();
155
+ }
156
+ }
157
+
158
+ module.exports = MySQLDatabase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "4.0.1",
3
+ "version": "4.1.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"