@onurege3467/zerohelper 8.0.0 → 9.0.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/README.md +292 -612
- package/dist/database/IDatabase.d.ts +77 -0
- package/dist/database/IDatabase.js +10 -0
- package/dist/database/cacheWrapper.d.ts +31 -0
- package/dist/database/cacheWrapper.js +228 -0
- package/dist/database/index.d.ts +11 -0
- package/dist/database/index.js +94 -0
- package/dist/database/json.d.ts +32 -0
- package/dist/database/json.js +210 -0
- package/dist/database/migration.d.ts +21 -0
- package/dist/database/migration.js +97 -0
- package/dist/database/mongodb.d.ts +24 -0
- package/dist/database/mongodb.js +153 -0
- package/dist/database/mysql.d.ts +31 -0
- package/dist/database/mysql.js +385 -0
- package/dist/database/pg.d.ts +30 -0
- package/dist/database/pg.js +300 -0
- package/dist/database/redis.d.ts +23 -0
- package/dist/database/redis.js +157 -0
- package/dist/database/sqlite.d.ts +25 -0
- package/dist/database/sqlite.js +273 -0
- package/dist/database/types.d.ts +76 -0
- package/dist/database/types.js +2 -0
- package/dist/database/zpack.d.ts +59 -0
- package/dist/database/zpack.js +462 -0
- package/dist/functions/index.d.ts +183 -0
- package/dist/functions/index.js +636 -0
- package/dist/functions/temp_isphone.d.ts +1 -0
- package/dist/functions/temp_isphone.js +7 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +45 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +55 -0
- package/dist/test_zpack.d.ts +1 -0
- package/dist/test_zpack.js +64 -0
- package/package.json +23 -6
- package/database/IDatabase.js +0 -92
- package/database/cacheWrapper.js +0 -585
- package/database/index.js +0 -72
- package/database/json.js +0 -281
- package/database/migration.js +0 -227
- package/database/mongodb.js +0 -203
- package/database/mysql.js +0 -526
- package/database/pg.js +0 -527
- package/database/redis.js +0 -342
- package/database/sqlite.js +0 -551
- package/functions/index.js +0 -705
- package/index.js +0 -7
package/database/redis.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
// redis.js - Redis Database Adapter (v4.x Uyumlu)
|
|
2
|
-
const { createClient } = require('redis');
|
|
3
|
-
|
|
4
|
-
class RedisDatabase {
|
|
5
|
-
constructor(config = {}) {
|
|
6
|
-
this.config = {
|
|
7
|
-
host: config.host || '127.0.0.1',
|
|
8
|
-
port: config.port || 6379,
|
|
9
|
-
password: config.password,
|
|
10
|
-
db: config.db || 0,
|
|
11
|
-
connectTimeout: config.connectTimeout || 5000,
|
|
12
|
-
commandTimeout: config.commandTimeout || 5000,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
this.keyPrefix = config.keyPrefix || 'app:';
|
|
16
|
-
this.client = null;
|
|
17
|
-
this.isConnecting = false; // Eşzamanlı bağlantı girişimlerini önlemek için
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async connect() {
|
|
21
|
-
if (this.client && this.client.isReady) {
|
|
22
|
-
return this.client;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Eğer zaten bağlantı girişimi yapılıyorsa, bekle
|
|
26
|
-
if (this.isConnecting) {
|
|
27
|
-
while (this.isConnecting) {
|
|
28
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
29
|
-
}
|
|
30
|
-
return this.client;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this.isConnecting = true;
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
this.client = createClient({
|
|
37
|
-
socket: {
|
|
38
|
-
host: this.config.host,
|
|
39
|
-
port: this.config.port,
|
|
40
|
-
connectTimeout: this.config.connectTimeout,
|
|
41
|
-
},
|
|
42
|
-
password: this.config.password,
|
|
43
|
-
database: this.config.db,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
this.client.on('error', (err) => {
|
|
47
|
-
console.error('Redis Error:', err.message);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
//this.client.on('connect', () => console.log('Redis Connected'));
|
|
51
|
-
//this.client.on('ready', () => console.log('Redis Ready'));
|
|
52
|
-
this.client.on('end', () => {
|
|
53
|
-
console.log('Redis Connection Ended');
|
|
54
|
-
this.client = null;
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// Timeout ile bağlantı kontrolü
|
|
58
|
-
await Promise.race([
|
|
59
|
-
this.client.connect(),
|
|
60
|
-
new Promise((_, reject) =>
|
|
61
|
-
setTimeout(() => reject(new Error('Redis connection timeout')), this.config.connectTimeout)
|
|
62
|
-
),
|
|
63
|
-
]);
|
|
64
|
-
|
|
65
|
-
console.log('Redis bağlantısı başarılı');
|
|
66
|
-
} catch (err) {
|
|
67
|
-
this.client = null;
|
|
68
|
-
throw new Error(`Redis bağlantısı başarısız: ${err.message}`);
|
|
69
|
-
} finally {
|
|
70
|
-
this.isConnecting = false;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return this.client;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
_getKey(table, id) {
|
|
77
|
-
return `${this.keyPrefix}${table}:${id}`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
_getTableKey(table) {
|
|
81
|
-
return `${this.keyPrefix}${table}:*`;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async select(table, where = {}) {
|
|
85
|
-
try {
|
|
86
|
-
const client = await this.connect();
|
|
87
|
-
const pattern = this._getTableKey(table);
|
|
88
|
-
const keys = await client.keys(pattern);
|
|
89
|
-
|
|
90
|
-
if (!keys.length) return [];
|
|
91
|
-
|
|
92
|
-
const values = await client.mGet(keys);
|
|
93
|
-
return values
|
|
94
|
-
.map(v => {
|
|
95
|
-
try {
|
|
96
|
-
return v ? JSON.parse(v) : null;
|
|
97
|
-
} catch (parseErr) {
|
|
98
|
-
console.error('JSON parse error:', parseErr);
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
.filter(Boolean)
|
|
103
|
-
.filter(item => Object.entries(where).every(([k, val]) => item[k] === val));
|
|
104
|
-
} catch (err) {
|
|
105
|
-
console.error('Select error:', err);
|
|
106
|
-
throw err;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async selectOne(table, where = {}) {
|
|
111
|
-
const results = await this.select(table, where);
|
|
112
|
-
return results.length ? results[0] : null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async insert(table, data) {
|
|
116
|
-
try {
|
|
117
|
-
const client = await this.connect();
|
|
118
|
-
const insertData = { ...data };
|
|
119
|
-
|
|
120
|
-
if (!insertData.id) {
|
|
121
|
-
insertData.id = Date.now().toString() + Math.random().toString(36).slice(2, 9);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const key = this._getKey(table, insertData.id);
|
|
125
|
-
await client.set(key, JSON.stringify(insertData));
|
|
126
|
-
return insertData;
|
|
127
|
-
} catch (err) {
|
|
128
|
-
console.error('Insert error:', err);
|
|
129
|
-
throw err;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async update(table, data, where) {
|
|
134
|
-
try {
|
|
135
|
-
const existing = await this.select(table, where);
|
|
136
|
-
if (!existing.length) return [];
|
|
137
|
-
|
|
138
|
-
const client = await this.connect();
|
|
139
|
-
const updated = [];
|
|
140
|
-
|
|
141
|
-
for (const item of existing) {
|
|
142
|
-
const merged = { ...item, ...data };
|
|
143
|
-
await client.set(this._getKey(table, item.id), JSON.stringify(merged));
|
|
144
|
-
updated.push(merged);
|
|
145
|
-
}
|
|
146
|
-
return updated;
|
|
147
|
-
} catch (err) {
|
|
148
|
-
console.error('Update error:', err);
|
|
149
|
-
throw err;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async updateOne(table, data, where) {
|
|
154
|
-
const results = await this.update(table, data, where);
|
|
155
|
-
return results.length ? results[0] : null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async set(table, data, where) {
|
|
159
|
-
try {
|
|
160
|
-
const existing = await this.selectOne(table, where);
|
|
161
|
-
if (existing) {
|
|
162
|
-
return await this.updateOne(table, data, where);
|
|
163
|
-
} else {
|
|
164
|
-
return await this.insert(table, { ...data, ...where });
|
|
165
|
-
}
|
|
166
|
-
} catch (err) {
|
|
167
|
-
console.error('Set error:', err);
|
|
168
|
-
throw err;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async delete(table, where) {
|
|
173
|
-
try {
|
|
174
|
-
const existing = await this.select(table, where);
|
|
175
|
-
if (!existing.length) return [];
|
|
176
|
-
|
|
177
|
-
const client = await this.connect();
|
|
178
|
-
const keys = existing.map(item => this._getKey(table, item.id));
|
|
179
|
-
await client.del(keys);
|
|
180
|
-
return existing;
|
|
181
|
-
} catch (err) {
|
|
182
|
-
console.error('Delete error:', err);
|
|
183
|
-
throw err;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async deleteOne(table, where) {
|
|
188
|
-
const results = await this.delete(table, where);
|
|
189
|
-
return results.length ? results[0] : null;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async bulkInsert(table, dataArray) {
|
|
193
|
-
if (!Array.isArray(dataArray) || !dataArray.length) {
|
|
194
|
-
return [];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
try {
|
|
198
|
-
const results = [];
|
|
199
|
-
for (const data of dataArray) {
|
|
200
|
-
results.push(await this.insert(table, data));
|
|
201
|
-
}
|
|
202
|
-
return results;
|
|
203
|
-
} catch (err) {
|
|
204
|
-
console.error('Bulk insert error:', err);
|
|
205
|
-
throw err;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async ensureTable(table) {
|
|
210
|
-
// Redis'te tablo kavramı yoktur, her zaman true döner
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async close() {
|
|
215
|
-
if (this.client) {
|
|
216
|
-
try {
|
|
217
|
-
await this.client.quit();
|
|
218
|
-
} catch (err) {
|
|
219
|
-
console.error('Redis close error:', err);
|
|
220
|
-
} finally {
|
|
221
|
-
this.client = null;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async ping() {
|
|
227
|
-
try {
|
|
228
|
-
const client = await this.connect();
|
|
229
|
-
return await client.ping();
|
|
230
|
-
} catch (err) {
|
|
231
|
-
console.error('Ping error:', err);
|
|
232
|
-
throw err;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async flushTable(table) {
|
|
237
|
-
try {
|
|
238
|
-
const client = await this.connect();
|
|
239
|
-
const pattern = this._getTableKey(table);
|
|
240
|
-
const keys = await client.keys(pattern);
|
|
241
|
-
|
|
242
|
-
if (keys.length > 0) {
|
|
243
|
-
await client.del(keys);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return keys.length;
|
|
247
|
-
} catch (err) {
|
|
248
|
-
console.error('Flush table error:', err);
|
|
249
|
-
throw err;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// Yardımcı metodlar
|
|
254
|
-
async getStats(table) {
|
|
255
|
-
try {
|
|
256
|
-
const client = await this.connect();
|
|
257
|
-
const pattern = this._getTableKey(table);
|
|
258
|
-
const keys = await client.keys(pattern);
|
|
259
|
-
return {
|
|
260
|
-
table,
|
|
261
|
-
keyCount: keys.length,
|
|
262
|
-
pattern
|
|
263
|
-
};
|
|
264
|
-
} catch (err) {
|
|
265
|
-
console.error('Get stats error:', err);
|
|
266
|
-
throw err;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
async exists(table, where) {
|
|
271
|
-
const result = await this.selectOne(table, where);
|
|
272
|
-
return result !== null;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Numerik alanları artırır (increment).
|
|
277
|
-
* Redis için hash field'ları üzerinde HINCRBY kullanır.
|
|
278
|
-
* @param {string} table - Verinin güncelleneceği tablo adı.
|
|
279
|
-
* @param {object} increments - Artırılacak alanlar ve miktarları.
|
|
280
|
-
* @param {object} where - Güncelleme koşulları.
|
|
281
|
-
* @returns {Promise<number>} Etkilenen kayıt sayısı.
|
|
282
|
-
*/
|
|
283
|
-
async increment(table, increments, where = {}) {
|
|
284
|
-
try {
|
|
285
|
-
const client = await this.connect();
|
|
286
|
-
|
|
287
|
-
// Önce mevcut kayıtları bul
|
|
288
|
-
const existingRecords = await this.select(table, where);
|
|
289
|
-
let affectedCount = 0;
|
|
290
|
-
|
|
291
|
-
for (const record of existingRecords) {
|
|
292
|
-
const key = this._getRecordKey(table, record._id);
|
|
293
|
-
|
|
294
|
-
// Her increment field için HINCRBY kullan
|
|
295
|
-
for (const [field, value] of Object.entries(increments)) {
|
|
296
|
-
await client.hIncrBy(key, field, value);
|
|
297
|
-
}
|
|
298
|
-
affectedCount++;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return affectedCount;
|
|
302
|
-
} catch (err) {
|
|
303
|
-
console.error('Increment error:', err);
|
|
304
|
-
throw err;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Numerik alanları azaltır (decrement).
|
|
310
|
-
* Redis için hash field'ları üzerinde HINCRBY ile negatif değer kullanır.
|
|
311
|
-
* @param {string} table - Verinin güncelleneceği tablo adı.
|
|
312
|
-
* @param {object} decrements - Azaltılacak alanlar ve miktarları.
|
|
313
|
-
* @param {object} where - Güncelleme koşulları.
|
|
314
|
-
* @returns {Promise<number>} Etkilenen kayıt sayısı.
|
|
315
|
-
*/
|
|
316
|
-
async decrement(table, decrements, where = {}) {
|
|
317
|
-
try {
|
|
318
|
-
const client = await this.connect();
|
|
319
|
-
|
|
320
|
-
// Önce mevcut kayıtları bul
|
|
321
|
-
const existingRecords = await this.select(table, where);
|
|
322
|
-
let affectedCount = 0;
|
|
323
|
-
|
|
324
|
-
for (const record of existingRecords) {
|
|
325
|
-
const key = this._getRecordKey(table, record._id);
|
|
326
|
-
|
|
327
|
-
// Her decrement field için HINCRBY ile negatif değer kullan
|
|
328
|
-
for (const [field, value] of Object.entries(decrements)) {
|
|
329
|
-
await client.hIncrBy(key, field, -value);
|
|
330
|
-
}
|
|
331
|
-
affectedCount++;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return affectedCount;
|
|
335
|
-
} catch (err) {
|
|
336
|
-
console.error('Decrement error:', err);
|
|
337
|
-
throw err;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
module.exports = RedisDatabase;
|