@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,240 +0,0 @@
|
|
|
1
|
-
// adapters/sqlite.js
|
|
2
|
-
|
|
3
|
-
const Database = require('better-sqlite3');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
|
|
7
|
-
class SQLiteDatabase {
|
|
8
|
-
/**
|
|
9
|
-
* @param {object} config - Yapılandırma nesnesi.
|
|
10
|
-
* @param {string} config.filePath - SQLite veritabanı dosyasının yolu.
|
|
11
|
-
*/
|
|
12
|
-
constructor(config) {
|
|
13
|
-
if (!config || !config.filePath) {
|
|
14
|
-
throw new Error('SQLite yapılandırması için "filePath" gereklidir.');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Veritabanı dosyasının bulunacağı klasörün var olduğundan emin ol
|
|
18
|
-
const dir = path.dirname(config.filePath);
|
|
19
|
-
if (!fs.existsSync(dir)) {
|
|
20
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// better-sqlite3 senkron çalıştığı için bağlantı anında kurulur.
|
|
24
|
-
this.db = new Database(config.filePath);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* SQL sorgusu çalıştırır. SELECT için satırları, diğerleri için bilgiyi döndürür.
|
|
29
|
-
* @param {string} sql - Çalıştırılacak SQL sorgusu.
|
|
30
|
-
* @param {Array} params - Sorgu parametreleri.
|
|
31
|
-
*/
|
|
32
|
-
query(sql, params = []) {
|
|
33
|
-
try {
|
|
34
|
-
// SELECT sorguları için .all() kullanılır ve bir dizi döndürür.
|
|
35
|
-
return this.db.prepare(sql).all(params);
|
|
36
|
-
} catch (error) {
|
|
37
|
-
// INSERT, UPDATE, DELETE gibi sorgular .all() ile çalışmaz, .run() kullanılır.
|
|
38
|
-
// Bu sorgular bir bilgi nesnesi döndürür (örn: { changes: 1, lastInsertRowid: 5 })
|
|
39
|
-
if (error.message.includes('This statement does not return data')) {
|
|
40
|
-
return this.db.prepare(sql).run(params);
|
|
41
|
-
}
|
|
42
|
-
// Başka bir hata varsa fırlat
|
|
43
|
-
throw error;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Bir tablonun var olup olmadığını kontrol eder, yoksa oluşturur.
|
|
49
|
-
* @param {string} table - Tablo adı.
|
|
50
|
-
* @param {object} data - Tablo oluşturulurken sütunları belirlemek için örnek veri.
|
|
51
|
-
*/
|
|
52
|
-
ensureTable(table, data = {}) {
|
|
53
|
-
if (!data || Object.keys(data).length === 0) return;
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
// Tablonun varlığını kontrol etmenin en hızlı yolu bir sorgu denemektir.
|
|
57
|
-
this.db.prepare(`SELECT 1 FROM \`${table}\` LIMIT 1`).get();
|
|
58
|
-
} catch (error) {
|
|
59
|
-
// "no such table" hatası alırsak, tabloyu oluştur.
|
|
60
|
-
if (error.message.includes('no such table')) {
|
|
61
|
-
// SQLite tipleri esnek olduğu için TEXT çoğu veri tipi için yeterlidir.
|
|
62
|
-
const columns = Object.keys(data).map(col => `\`${col}\` TEXT`).join(', ');
|
|
63
|
-
const createTableSQL = `
|
|
64
|
-
CREATE TABLE \`${table}\` (
|
|
65
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
66
|
-
${columns}
|
|
67
|
-
)
|
|
68
|
-
`;
|
|
69
|
-
this.query(createTableSQL);
|
|
70
|
-
} else {
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Verilen verideki anahtarların tabloda sütun olarak var olduğundan emin olur.
|
|
78
|
-
* @private
|
|
79
|
-
*/
|
|
80
|
-
_ensureColumns(table, data) {
|
|
81
|
-
const columnsInfo = this.db.prepare(`PRAGMA table_info(\`${table}\`)`).all();
|
|
82
|
-
const existingNames = columnsInfo.map(col => col.name);
|
|
83
|
-
|
|
84
|
-
for (const key of Object.keys(data)) {
|
|
85
|
-
if (!existingNames.includes(key)) {
|
|
86
|
-
this.query(`ALTER TABLE \`${table}\` ADD COLUMN \`${key}\` TEXT`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Bir tabloya yeni veri ekler.
|
|
93
|
-
* @returns {number} Eklenen satırın ID'si.
|
|
94
|
-
*/
|
|
95
|
-
insert(table, data) {
|
|
96
|
-
const copy = { ...data };
|
|
97
|
-
this.ensureTable(table, copy);
|
|
98
|
-
this._ensureColumns(table, copy);
|
|
99
|
-
|
|
100
|
-
const keys = Object.keys(copy);
|
|
101
|
-
const placeholders = keys.map(() => '?').join(',');
|
|
102
|
-
const values = Object.values(copy);
|
|
103
|
-
const sql = `INSERT INTO \`${table}\` (${keys.map(k => `\`${k}\``).join(',')}) VALUES (${placeholders})`;
|
|
104
|
-
|
|
105
|
-
const result = this.query(sql, values);
|
|
106
|
-
return result.lastInsertRowid; // SQLite'ta `insertId` yerine `lastInsertRowid` kullanılır.
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Tablodaki verileri günceller.
|
|
111
|
-
* @returns {number} Etkilenen satır sayısı.
|
|
112
|
-
*/
|
|
113
|
-
update(table, data, where) {
|
|
114
|
-
this.ensureTable(table, { ...data, ...where });
|
|
115
|
-
this._ensureColumns(table, data);
|
|
116
|
-
|
|
117
|
-
const setString = Object.keys(data).map(k => `\`${k}\` = ?`).join(', ');
|
|
118
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(' AND ');
|
|
119
|
-
const sql = `UPDATE \`${table}\` SET ${setString} WHERE ${whereString}`;
|
|
120
|
-
const result = this.query(sql, [...Object.values(data), ...Object.values(where)]);
|
|
121
|
-
return result.changes; // SQLite'ta `affectedRows` yerine `changes` kullanılır.
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Tablodan veri siler.
|
|
126
|
-
* @returns {number} Etkilenen satır sayısı.
|
|
127
|
-
*/
|
|
128
|
-
delete(table, where) {
|
|
129
|
-
if (!where || Object.keys(where).length === 0) return 0;
|
|
130
|
-
this.ensureTable(table, { ...where });
|
|
131
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(' AND ');
|
|
132
|
-
const sql = `DELETE FROM \`${table}\` WHERE ${whereString}`;
|
|
133
|
-
const result = this.query(sql, Object.values(where));
|
|
134
|
-
return result.changes;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Tablodan veri seçer.
|
|
139
|
-
* @returns {Array<object>} Sonuç satırları.
|
|
140
|
-
*/
|
|
141
|
-
select(table, where = null) {
|
|
142
|
-
this.ensureTable(table, where || {});
|
|
143
|
-
let sql = `SELECT * FROM \`${table}\``;
|
|
144
|
-
let params = [];
|
|
145
|
-
|
|
146
|
-
if (where && Object.keys(where).length > 0) {
|
|
147
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(' AND ');
|
|
148
|
-
sql += ` WHERE ${whereString}`;
|
|
149
|
-
params = Object.values(where);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return this.query(sql, params);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Veri varsa günceller, yoksa ekler (upsert).
|
|
157
|
-
*/
|
|
158
|
-
set(table, data, where) {
|
|
159
|
-
this.ensureTable(table, { ...data, ...where });
|
|
160
|
-
this._ensureColumns(table, data);
|
|
161
|
-
const existing = this.select(table, where);
|
|
162
|
-
if (existing.length === 0) {
|
|
163
|
-
return this.insert(table, { ...where, ...data });
|
|
164
|
-
} else {
|
|
165
|
-
return this.update(table, data, where);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Koşula uyan ilk veriyi seçer.
|
|
171
|
-
* @returns {object|null} Bulunan satır veya null.
|
|
172
|
-
*/
|
|
173
|
-
selectOne(table, where = null) {
|
|
174
|
-
const results = this.select(table, where);
|
|
175
|
-
return results[0] || null;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Koşula uyan ilk veriyi siler.
|
|
180
|
-
* @returns {number} Etkilenen satır sayısı (0 veya 1).
|
|
181
|
-
*/
|
|
182
|
-
deleteOne(table, where) {
|
|
183
|
-
if (!where || Object.keys(where).length === 0) return 0;
|
|
184
|
-
this.ensureTable(table, where);
|
|
185
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(' AND ');
|
|
186
|
-
const sql = `DELETE FROM \`${table}\` WHERE rowid IN (SELECT rowid FROM \`${table}\` WHERE ${whereString} LIMIT 1)`;
|
|
187
|
-
const result = this.query(sql, Object.values(where));
|
|
188
|
-
return result.changes;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Koşula uyan ilk veriyi günceller.
|
|
193
|
-
* @returns {number} Etkilenen satır sayısı (0 veya 1).
|
|
194
|
-
*/
|
|
195
|
-
updateOne(table, data, where) {
|
|
196
|
-
this.ensureTable(table, { ...data, ...where });
|
|
197
|
-
this._ensureColumns(table, data);
|
|
198
|
-
const setString = Object.keys(data).map(k => `\`${k}\` = ?`).join(', ');
|
|
199
|
-
const whereString = Object.keys(where).map(k => `\`${k}\` = ?`).join(' AND ');
|
|
200
|
-
const sql = `UPDATE \`${table}\` SET ${setString} WHERE rowid IN (SELECT rowid FROM \`${table}\` WHERE ${whereString} LIMIT 1)`;
|
|
201
|
-
const result = this.query(sql, [...Object.values(data), ...Object.values(where)]);
|
|
202
|
-
return result.changes;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Toplu veri ekleme.
|
|
207
|
-
* @returns {number} Eklenen satır sayısı.
|
|
208
|
-
*/
|
|
209
|
-
bulkInsert(table, dataArray) {
|
|
210
|
-
if (!Array.isArray(dataArray) || dataArray.length === 0) return 0;
|
|
211
|
-
this.ensureTable(table, dataArray[0]);
|
|
212
|
-
this._ensureColumns(table, dataArray[0]);
|
|
213
|
-
|
|
214
|
-
const keys = Object.keys(dataArray[0]);
|
|
215
|
-
const placeholders = keys.map(() => '?').join(',');
|
|
216
|
-
const sql = `INSERT INTO \`${table}\` (${keys.map(k => `\`${k}\``).join(',')}) VALUES (${placeholders})`;
|
|
217
|
-
|
|
218
|
-
// better-sqlite3'nin transaction özelliği toplu işlemlerde çok yüksek performans sağlar.
|
|
219
|
-
const insertMany = this.db.transaction((items) => {
|
|
220
|
-
const stmt = this.db.prepare(sql);
|
|
221
|
-
for (const item of items) {
|
|
222
|
-
stmt.run(Object.values(item));
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
insertMany(dataArray);
|
|
227
|
-
return dataArray.length;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Veritabanı bağlantısını kapatır.
|
|
232
|
-
*/
|
|
233
|
-
close() {
|
|
234
|
-
if (this.db) {
|
|
235
|
-
this.db.close();
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
module.exports = SQLiteDatabase;
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
const { Pool } = require("pg");
|
|
2
|
-
|
|
3
|
-
class PostgreSQL {
|
|
4
|
-
constructor(config) {
|
|
5
|
-
this.pool = new Pool(config);
|
|
6
|
-
|
|
7
|
-
// Ensure the table exists when the class is instantiated
|
|
8
|
-
this.ensureTableExists(config).catch((error) => {
|
|
9
|
-
console.error("Error ensuring table exists:", error);
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async ensureTableExists(config) {
|
|
14
|
-
try {
|
|
15
|
-
await this.pool.query(`
|
|
16
|
-
CREATE TABLE IF NOT EXISTS key_value_store (
|
|
17
|
-
key TEXT PRIMARY KEY,
|
|
18
|
-
value TEXT NOT NULL
|
|
19
|
-
)
|
|
20
|
-
`);
|
|
21
|
-
} catch (error) {
|
|
22
|
-
console.error("Failed to create table:", error);
|
|
23
|
-
throw error;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async set(key, value) {
|
|
28
|
-
const keys = key.split(".");
|
|
29
|
-
const rootKey = keys.shift();
|
|
30
|
-
const currentValue = (await this.get(rootKey)) || {};
|
|
31
|
-
|
|
32
|
-
let target = currentValue;
|
|
33
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
34
|
-
if (!target[keys[i]]) target[keys[i]] = {};
|
|
35
|
-
target = target[keys[i]];
|
|
36
|
-
}
|
|
37
|
-
target[keys[keys.length - 1]] = value;
|
|
38
|
-
|
|
39
|
-
await this.pool.query(
|
|
40
|
-
"INSERT INTO key_value_store (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = $2",
|
|
41
|
-
[rootKey, JSON.stringify(currentValue)]
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async get(key) {
|
|
46
|
-
const keys = key.split(".");
|
|
47
|
-
const rootKey = keys.shift();
|
|
48
|
-
const res = await this.pool.query(
|
|
49
|
-
"SELECT value FROM key_value_store WHERE key = $1",
|
|
50
|
-
[rootKey]
|
|
51
|
-
);
|
|
52
|
-
const rootValue = res.rows[0] ? JSON.parse(res.rows[0].value) : null;
|
|
53
|
-
|
|
54
|
-
if (!rootValue) return null;
|
|
55
|
-
|
|
56
|
-
let target = rootValue;
|
|
57
|
-
for (const k of keys) {
|
|
58
|
-
if (target[k] === undefined) return null;
|
|
59
|
-
target = target[k];
|
|
60
|
-
}
|
|
61
|
-
return target;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async has(key) {
|
|
65
|
-
return (await this.get(key)) !== null;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async delete(key) {
|
|
69
|
-
const keys = key.split(".");
|
|
70
|
-
const rootKey = keys.shift();
|
|
71
|
-
const currentValue = (await this.get(rootKey)) || {};
|
|
72
|
-
|
|
73
|
-
let target = currentValue;
|
|
74
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
75
|
-
if (!target[keys[i]]) return; // Key path does not exist
|
|
76
|
-
target = target[keys[i]];
|
|
77
|
-
}
|
|
78
|
-
delete target[keys[keys.length - 1]];
|
|
79
|
-
|
|
80
|
-
await this.pool.query(
|
|
81
|
-
"INSERT INTO key_value_store (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = $2",
|
|
82
|
-
[rootKey, JSON.stringify(currentValue)]
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async add(key, amount) {
|
|
87
|
-
const currentValue = (await this.get(key)) || 0;
|
|
88
|
-
if (typeof currentValue !== "number") {
|
|
89
|
-
throw new TypeError("The value is not a number.");
|
|
90
|
-
}
|
|
91
|
-
await this.set(key, currentValue + amount);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async sub(key, amount) {
|
|
95
|
-
const currentValue = (await this.get(key)) || 0;
|
|
96
|
-
if (typeof currentValue !== "number") {
|
|
97
|
-
throw new TypeError("The value is not a number.");
|
|
98
|
-
}
|
|
99
|
-
await this.set(key, currentValue - amount);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async push(key, value) {
|
|
103
|
-
const currentValue = (await this.get(key)) || [];
|
|
104
|
-
if (!Array.isArray(currentValue)) {
|
|
105
|
-
throw new TypeError("The value is not an array.");
|
|
106
|
-
}
|
|
107
|
-
currentValue.push(value);
|
|
108
|
-
await this.set(key, currentValue);
|
|
109
|
-
}
|
|
110
|
-
async getAllData() {
|
|
111
|
-
try {
|
|
112
|
-
const res = await this.pool.query(
|
|
113
|
-
"SELECT key, value FROM key_value_store"
|
|
114
|
-
);
|
|
115
|
-
const result = {};
|
|
116
|
-
|
|
117
|
-
res.rows.forEach((row) => {
|
|
118
|
-
let value = row.value;
|
|
119
|
-
|
|
120
|
-
// JSON parse işlemi
|
|
121
|
-
try {
|
|
122
|
-
value = JSON.parse(value);
|
|
123
|
-
} catch (e) {
|
|
124
|
-
// JSON değilse olduğu gibi bırak
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
result[row.key] = value;
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
return result;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
console.error("Error fetching all data:", error);
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
async ping() {
|
|
137
|
-
try {
|
|
138
|
-
await this.pool.query("SELECT 1");
|
|
139
|
-
return true;
|
|
140
|
-
} catch {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async close() {
|
|
146
|
-
await this.pool.end();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
module.exports = PostgreSQL;
|
package/database/redis/index.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
const { createClient } = require("redis");
|
|
2
|
-
|
|
3
|
-
class RedisDatabase {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
this.client = createClient(config);
|
|
6
|
-
this.client.on("error", (err) => console.error("Redis Client Error", err));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async connect() {
|
|
10
|
-
await this.client.connect();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async set(key, value) {
|
|
14
|
-
const keys = key.split(".");
|
|
15
|
-
const rootKey = keys.shift();
|
|
16
|
-
const currentValue = (await this.get(rootKey)) || {};
|
|
17
|
-
|
|
18
|
-
let target = currentValue;
|
|
19
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
20
|
-
if (!target[keys[i]]) target[keys[i]] = {};
|
|
21
|
-
target = target[keys[i]];
|
|
22
|
-
}
|
|
23
|
-
target[keys[keys.length - 1]] = value;
|
|
24
|
-
|
|
25
|
-
await this.client.set(rootKey, JSON.stringify(currentValue));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async get(key) {
|
|
29
|
-
const keys = key.split(".");
|
|
30
|
-
const rootKey = keys.shift();
|
|
31
|
-
const value = await this.client.get(rootKey);
|
|
32
|
-
const rootValue = value ? JSON.parse(value) : null;
|
|
33
|
-
|
|
34
|
-
if (!rootValue) return null;
|
|
35
|
-
|
|
36
|
-
let target = rootValue;
|
|
37
|
-
for (const k of keys) {
|
|
38
|
-
if (target[k] === undefined) return null;
|
|
39
|
-
target = target[k];
|
|
40
|
-
}
|
|
41
|
-
return target;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async has(key) {
|
|
45
|
-
return (await this.get(key)) !== null;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async delete(key) {
|
|
49
|
-
const keys = key.split(".");
|
|
50
|
-
const rootKey = keys.shift();
|
|
51
|
-
const currentValue = (await this.get(rootKey)) || {};
|
|
52
|
-
|
|
53
|
-
let target = currentValue;
|
|
54
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
55
|
-
if (!target[keys[i]]) return; // Key path does not exist
|
|
56
|
-
target = target[keys[i]];
|
|
57
|
-
}
|
|
58
|
-
delete target[keys[keys.length - 1]];
|
|
59
|
-
|
|
60
|
-
await this.client.set(rootKey, JSON.stringify(currentValue));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async add(key, amount) {
|
|
64
|
-
const currentValue = (await this.get(key)) || 0;
|
|
65
|
-
if (typeof currentValue !== "number") {
|
|
66
|
-
throw new TypeError("The value is not a number.");
|
|
67
|
-
}
|
|
68
|
-
await this.set(key, currentValue + amount);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async sub(key, amount) {
|
|
72
|
-
const currentValue = (await this.get(key)) || 0;
|
|
73
|
-
if (typeof currentValue !== "number") {
|
|
74
|
-
throw new TypeError("The value is not a number.");
|
|
75
|
-
}
|
|
76
|
-
await this.set(key, currentValue - amount);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async push(key, value) {
|
|
80
|
-
const currentValue = (await this.get(key)) || [];
|
|
81
|
-
if (!Array.isArray(currentValue)) {
|
|
82
|
-
throw new TypeError("The value is not an array.");
|
|
83
|
-
}
|
|
84
|
-
currentValue.push(value);
|
|
85
|
-
await this.set(key, currentValue);
|
|
86
|
-
}
|
|
87
|
-
async getAllData() {
|
|
88
|
-
try {
|
|
89
|
-
const keys = await this.client.keys("*"); // Tüm anahtarları al
|
|
90
|
-
const result = {};
|
|
91
|
-
|
|
92
|
-
for (const key of keys) {
|
|
93
|
-
let value = await this.client.get(key);
|
|
94
|
-
|
|
95
|
-
// JSON parse işlemi
|
|
96
|
-
try {
|
|
97
|
-
value = JSON.parse(value);
|
|
98
|
-
} catch (e) {
|
|
99
|
-
// JSON değilse olduğu gibi bırak
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
result[key] = value;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return result;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
console.error("Error fetching all data:", error);
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
async ping() {
|
|
112
|
-
try {
|
|
113
|
-
const pong = await this.client.ping();
|
|
114
|
-
return pong === "PONG";
|
|
115
|
-
} catch {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async close() {
|
|
121
|
-
await this.client.quit();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = RedisDatabase;
|