@onurege3467/zerohelper 9.0.0 → 9.2.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.
Files changed (45) hide show
  1. package/README.md +152 -254
  2. package/dist/bin/zero.d.ts +2 -0
  3. package/dist/bin/zero.js +141 -0
  4. package/dist/database/IDatabase.d.ts +25 -31
  5. package/dist/database/IDatabase.js +38 -0
  6. package/dist/database/cacheWrapper.d.ts +5 -2
  7. package/dist/database/cacheWrapper.js +36 -50
  8. package/dist/database/index.d.ts +3 -2
  9. package/dist/database/index.js +13 -9
  10. package/dist/database/json.d.ts +4 -4
  11. package/dist/database/json.js +85 -87
  12. package/dist/database/mongodb.d.ts +12 -12
  13. package/dist/database/mongodb.js +49 -82
  14. package/dist/database/mysql.d.ts +7 -9
  15. package/dist/database/mysql.js +149 -270
  16. package/dist/database/pg.d.ts +12 -14
  17. package/dist/database/pg.js +113 -222
  18. package/dist/database/redis.d.ts +5 -3
  19. package/dist/database/redis.js +81 -107
  20. package/dist/database/seeder.d.ts +20 -0
  21. package/dist/database/seeder.js +37 -0
  22. package/dist/database/sqlite.d.ts +12 -15
  23. package/dist/database/sqlite.js +108 -223
  24. package/dist/database/telemetry.d.ts +35 -0
  25. package/dist/database/telemetry.js +41 -0
  26. package/dist/database/toon.d.ts +32 -0
  27. package/dist/database/toon.js +209 -0
  28. package/dist/database/types.d.ts +28 -34
  29. package/dist/database/zpack.d.ts +10 -4
  30. package/dist/database/zpack.js +151 -71
  31. package/dist/functions/index.d.ts +16 -0
  32. package/dist/functions/index.js +49 -3
  33. package/dist/functions/security.d.ts +15 -0
  34. package/dist/functions/security.js +46 -0
  35. package/dist/functions/toon.d.ts +7 -0
  36. package/dist/functions/toon.js +118 -0
  37. package/dist/functions/worker.d.ts +5 -0
  38. package/dist/functions/worker.js +35 -0
  39. package/dist/test_v91_advanced.d.ts +1 -0
  40. package/dist/test_v91_advanced.js +48 -0
  41. package/dist/test_v91_basics.d.ts +1 -0
  42. package/dist/test_v91_basics.js +54 -0
  43. package/dist/test_v91_performance.d.ts +1 -0
  44. package/dist/test_v91_performance.js +54 -0
  45. package/package.json +16 -3
@@ -16,13 +16,19 @@ class JsonDatabase extends IDatabase_1.IDatabase {
16
16
  this.isWriting = false;
17
17
  this.writeQueue = [];
18
18
  this.saveDebounceTimeout = null;
19
- if (!config || !config.filePath)
20
- throw new Error('Yapılandırma içinde "filePath" belirtilmelidir.');
21
- this.filePath = config.filePath;
22
- this.saveInterval = 500;
19
+ if (!config || !config.path)
20
+ throw new Error('JsonDB: "path" gereklidir.');
21
+ this.filePath = config.path;
22
+ this.saveInterval = config.saveInterval || 500;
23
23
  process.on('exit', () => this.flushSync());
24
24
  this.initPromise = this._load();
25
25
  }
26
+ async _execute(op, table, fn) {
27
+ const start = Date.now();
28
+ const res = await fn();
29
+ this.recordMetric(op, table, Date.now() - start);
30
+ return res;
31
+ }
26
32
  async _load() {
27
33
  try {
28
34
  const dir = path_1.default.dirname(this.filePath);
@@ -83,7 +89,7 @@ class JsonDatabase extends IDatabase_1.IDatabase {
83
89
  this.isDirty = false;
84
90
  }
85
91
  catch (error) {
86
- console.error("Veritabanı dosyasına yazılırken hata:", error);
92
+ console.error("JsonDB save error:", error);
87
93
  }
88
94
  }
89
95
  flushSync() {
@@ -95,116 +101,108 @@ class JsonDatabase extends IDatabase_1.IDatabase {
95
101
  catch (error) { }
96
102
  }
97
103
  }
98
- _matches(row, where) {
99
- return Object.keys(where).every(key => row[key] === where[key]);
100
- }
101
104
  async ensureTable(table) {
102
105
  await this.initPromise;
103
106
  if (!this.db[table]) {
104
- return this._queueRequest(() => {
105
- this.db[table] = [];
106
- });
107
+ return this._queueRequest(() => { this.db[table] = []; });
107
108
  }
108
109
  }
109
110
  async insert(table, data) {
110
- await this.ensureTable(table);
111
- return this._queueRequest(() => {
112
- const maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
113
- const newId = maxId + 1;
114
- const newRow = { _id: newId, ...data };
115
- this.db[table].push(newRow);
116
- return newId;
111
+ await this.runHooks('beforeInsert', table, data);
112
+ return this._execute('insert', table, async () => {
113
+ await this.ensureTable(table);
114
+ return this._queueRequest(() => {
115
+ const maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
116
+ const newId = maxId + 1;
117
+ const newRow = { _id: newId, ...data };
118
+ this.db[table].push(newRow);
119
+ this.runHooks('afterInsert', table, newRow);
120
+ return newId;
121
+ });
117
122
  });
118
123
  }
119
124
  async update(table, data, where) {
120
- await this.ensureTable(table);
121
- return this._queueRequest(() => {
122
- let affectedRows = 0;
123
- this.db[table].forEach(row => {
124
- if (this._matches(row, where)) {
125
- Object.assign(row, data);
126
- affectedRows++;
127
- }
125
+ await this.runHooks('beforeUpdate', table, { data, where });
126
+ return this._execute('update', table, async () => {
127
+ await this.ensureTable(table);
128
+ return this._queueRequest(() => {
129
+ let affected = 0;
130
+ this.db[table].forEach(row => {
131
+ if (Object.keys(where).every(k => String(row[k]) === String(where[k]))) {
132
+ Object.assign(row, data);
133
+ affected++;
134
+ }
135
+ });
136
+ this.runHooks('afterUpdate', table, { affected });
137
+ return affected;
128
138
  });
129
- return affectedRows;
130
139
  });
131
140
  }
132
141
  async delete(table, where) {
133
- await this.ensureTable(table);
134
- return this._queueRequest(() => {
135
- const initialLength = this.db[table].length;
136
- this.db[table] = this.db[table].filter(row => !this._matches(row, where));
137
- return initialLength - this.db[table].length;
142
+ await this.runHooks('beforeDelete', table, where);
143
+ return this._execute('delete', table, async () => {
144
+ await this.ensureTable(table);
145
+ return this._queueRequest(() => {
146
+ const initial = this.db[table].length;
147
+ this.db[table] = this.db[table].filter(row => !Object.keys(where).every(k => String(row[k]) === String(where[k])));
148
+ const affected = initial - this.db[table].length;
149
+ this.runHooks('afterDelete', table, { affected });
150
+ return affected;
151
+ });
138
152
  });
139
153
  }
140
154
  async select(table, where = null) {
141
- await this.initPromise;
142
- const tableData = this.db[table] || [];
143
- let results = tableData;
144
- if (where && Object.keys(where).length > 0) {
145
- results = results.filter(row => this._matches(row, where));
146
- }
147
- return JSON.parse(JSON.stringify(results));
148
- }
149
- async set(table, data, where) {
150
- const existing = await this.select(table, where);
151
- if (existing.length === 0) {
152
- return this.insert(table, { ...where, ...data });
153
- }
154
- else {
155
- return this.update(table, data, where);
156
- }
155
+ return this._execute('select', table, async () => {
156
+ await this.initPromise;
157
+ const results = where && Object.keys(where).length > 0
158
+ ? (this.db[table] || []).filter(row => Object.keys(where).every(k => String(row[k]) === String(where[k])))
159
+ : (this.db[table] || []);
160
+ return JSON.parse(JSON.stringify(results));
161
+ });
157
162
  }
158
163
  async selectOne(table, where = null) {
159
- const results = await this.select(table, where);
160
- return results[0] || null;
164
+ const res = await this.select(table, where);
165
+ return res[0] || null;
161
166
  }
162
- async bulkInsert(table, dataArray) {
163
- if (!Array.isArray(dataArray) || dataArray.length === 0)
164
- return 0;
165
- await this.ensureTable(table);
166
- return this._queueRequest(() => {
167
- let maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
168
- dataArray.forEach(data => {
169
- maxId++;
170
- this.db[table].push({ _id: maxId, ...data });
171
- });
172
- return dataArray.length;
173
- });
167
+ async set(table, data, where) {
168
+ const ex = await this.selectOne(table, where);
169
+ return ex ? this.update(table, data, where) : this.insert(table, { ...where, ...data });
174
170
  }
175
- async increment(table, increments, where = {}) {
176
- await this.ensureTable(table);
177
- return this._queueRequest(() => {
178
- let affectedCount = 0;
179
- this.db[table].forEach(row => {
180
- if (this._matches(row, where)) {
181
- for (const [field, value] of Object.entries(increments)) {
182
- row[field] = (Number(row[field]) || 0) + value;
183
- }
184
- affectedCount++;
185
- }
171
+ async bulkInsert(table, dataArray) {
172
+ return this._execute('bulkInsert', table, async () => {
173
+ if (!dataArray.length)
174
+ return 0;
175
+ await this.ensureTable(table);
176
+ return this._queueRequest(() => {
177
+ let maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
178
+ dataArray.forEach(data => { maxId++; this.db[table].push({ _id: maxId, ...data }); });
179
+ return dataArray.length;
186
180
  });
187
- return affectedCount;
188
181
  });
189
182
  }
190
- async decrement(table, decrements, where = {}) {
191
- await this.ensureTable(table);
192
- return this._queueRequest(() => {
193
- let affectedCount = 0;
194
- this.db[table].forEach(row => {
195
- if (this._matches(row, where)) {
196
- for (const [field, value] of Object.entries(decrements)) {
197
- row[field] = (Number(row[field]) || 0) - value;
183
+ async increment(table, incs, where = {}) {
184
+ return this._execute('increment', table, async () => {
185
+ await this.ensureTable(table);
186
+ return this._queueRequest(() => {
187
+ let affected = 0;
188
+ this.db[table].forEach(row => {
189
+ if (Object.keys(where).every(k => String(row[k]) === String(where[k]))) {
190
+ for (const [f, v] of Object.entries(incs))
191
+ row[f] = (Number(row[f]) || 0) + v;
192
+ affected++;
198
193
  }
199
- affectedCount++;
200
- }
194
+ });
195
+ return affected;
201
196
  });
202
- return affectedCount;
203
197
  });
204
198
  }
205
- async close() {
206
- await this._saveNow();
199
+ async decrement(table, decs, where = {}) {
200
+ const incs = {};
201
+ for (const k in decs)
202
+ incs[k] = -decs[k];
203
+ return this.increment(table, incs, where);
207
204
  }
205
+ async close() { await this._saveNow(); }
208
206
  }
209
207
  exports.JsonDatabase = JsonDatabase;
210
208
  exports.default = JsonDatabase;
@@ -1,4 +1,5 @@
1
1
  import { IDatabase } from './IDatabase';
2
+ import { MongoDBConfig } from './types';
2
3
  export declare class MongoDBDatabase extends IDatabase {
3
4
  private config;
4
5
  private client;
@@ -6,19 +7,18 @@ export declare class MongoDBDatabase extends IDatabase {
6
7
  private _queue;
7
8
  private _connected;
8
9
  private _connectionPromise;
9
- constructor(config: any);
10
- private _queueRequest;
10
+ constructor(config: MongoDBConfig);
11
+ private _execute;
11
12
  private _processQueue;
12
- ensureCollection(collection: string): Promise<void>;
13
- insert(collection: string, data: Record<string, any>): Promise<any>;
14
- bulkInsert(collection: string, dataArray: Record<string, any>[]): Promise<number>;
15
- update(collection: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
16
- delete(collection: string, where: Record<string, any>): Promise<number>;
17
- select<T = any>(collection: string, where?: Record<string, any>): Promise<T[]>;
18
- selectOne<T = any>(collection: string, where?: Record<string, any>): Promise<T | null>;
19
- set(collection: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
20
- increment(collection: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
21
- decrement(collection: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
13
+ insert(collection: string, data: any): Promise<any>;
14
+ update(collection: string, data: any, where: any): Promise<number>;
15
+ delete(collection: string, where: any): Promise<number>;
16
+ select<T = any>(collection: string, where?: any): Promise<T[]>;
17
+ selectOne<T = any>(collection: string, where?: any): Promise<T | null>;
18
+ set(collection: string, data: any, where: any): Promise<any>;
19
+ bulkInsert(collection: string, dataArray: any[]): Promise<number>;
20
+ increment(collection: string, incs: Record<string, number>, where?: any): Promise<number>;
21
+ decrement(collection: string, decs: Record<string, number>, where?: any): Promise<number>;
22
22
  close(): Promise<void>;
23
23
  }
24
24
  export default MongoDBDatabase;
@@ -10,30 +10,30 @@ class MongoDBDatabase extends IDatabase_1.IDatabase {
10
10
  this._queue = [];
11
11
  this._connected = false;
12
12
  this.config = config;
13
- this.client = new mongodb_1.MongoClient(config.url || config.uri);
13
+ this.client = new mongodb_1.MongoClient(config.url || `mongodb://${config.host || 'localhost'}:${config.port || 27017}`);
14
14
  this._connectionPromise = new Promise(async (resolve, reject) => {
15
15
  try {
16
16
  await this.client.connect();
17
- this.db = this.client.db(config.database || config.dbName);
17
+ this.db = this.client.db(config.database || 'test');
18
18
  this._connected = true;
19
19
  resolve(this.db);
20
20
  this._processQueue();
21
21
  }
22
22
  catch (error) {
23
- console.error("MongoDB connection error:", error);
24
23
  reject(error);
25
24
  }
26
25
  });
27
26
  }
28
- async _queueRequest(operation) {
29
- if (this._connected) {
30
- return operation();
31
- }
32
- else {
33
- return new Promise((resolve, reject) => {
34
- this._queue.push({ operation, resolve, reject });
35
- });
36
- }
27
+ async _execute(op, table, fn) {
28
+ const start = Date.now();
29
+ const execute = async () => {
30
+ const res = await fn();
31
+ this.recordMetric(op, table, Date.now() - start);
32
+ return res;
33
+ };
34
+ if (this._connected)
35
+ return execute();
36
+ return new Promise((resolve, reject) => this._queue.push({ operation: execute, resolve, reject }));
37
37
  }
38
38
  async _processQueue() {
39
39
  if (!this._connected)
@@ -42,8 +42,7 @@ class MongoDBDatabase extends IDatabase_1.IDatabase {
42
42
  const item = this._queue.shift();
43
43
  if (item) {
44
44
  try {
45
- const result = await item.operation();
46
- item.resolve(result);
45
+ item.resolve(await item.operation());
47
46
  }
48
47
  catch (error) {
49
48
  item.reject(error);
@@ -51,103 +50,71 @@ class MongoDBDatabase extends IDatabase_1.IDatabase {
51
50
  }
52
51
  }
53
52
  }
54
- async ensureCollection(collection) {
55
- return this._queueRequest(async () => {
56
- const db = await this._connectionPromise;
57
- const collections = await db.listCollections({ name: collection }).toArray();
58
- if (collections.length === 0) {
59
- await db.createCollection(collection);
60
- }
61
- });
62
- }
63
53
  async insert(collection, data) {
64
- return this._queueRequest(async () => {
65
- await this.ensureCollection(collection);
66
- const db = await this._connectionPromise;
67
- const result = await db.collection(collection).insertOne(data);
68
- return result.insertedId;
69
- });
70
- }
71
- async bulkInsert(collection, dataArray) {
72
- return this._queueRequest(async () => {
73
- if (!Array.isArray(dataArray) || dataArray.length === 0)
74
- return 0;
75
- await this.ensureCollection(collection);
54
+ await this.runHooks('beforeInsert', collection, data);
55
+ return this._execute('insert', collection, async () => {
76
56
  const db = await this._connectionPromise;
77
- const result = await db.collection(collection).insertMany(dataArray);
78
- return result.insertedCount;
57
+ const res = await db.collection(collection).insertOne(data);
58
+ const finalData = { _id: res.insertedId, ...data };
59
+ await this.runHooks('afterInsert', collection, finalData);
60
+ return res.insertedId;
79
61
  });
80
62
  }
81
63
  async update(collection, data, where) {
82
- return this._queueRequest(async () => {
83
- await this.ensureCollection(collection);
64
+ await this.runHooks('beforeUpdate', collection, { data, where });
65
+ return this._execute('update', collection, async () => {
84
66
  const db = await this._connectionPromise;
85
- const result = await db.collection(collection).updateMany(where, { $set: data });
86
- return Number(result.modifiedCount);
67
+ const res = await db.collection(collection).updateMany(where, { $set: data });
68
+ return Number(res.modifiedCount);
87
69
  });
88
70
  }
89
71
  async delete(collection, where) {
90
- return this._queueRequest(async () => {
91
- await this.ensureCollection(collection);
72
+ await this.runHooks('beforeDelete', collection, where);
73
+ return this._execute('delete', collection, async () => {
92
74
  const db = await this._connectionPromise;
93
- const result = await db.collection(collection).deleteMany(where);
94
- return result.deletedCount;
75
+ const res = await db.collection(collection).deleteMany(where);
76
+ return res.deletedCount;
95
77
  });
96
78
  }
97
79
  async select(collection, where = {}) {
98
- return this._queueRequest(async () => {
99
- await this.ensureCollection(collection);
80
+ return this._execute('select', collection, async () => {
100
81
  const db = await this._connectionPromise;
101
- const results = await db.collection(collection).find(where).toArray();
102
- return results;
82
+ return await db.collection(collection).find(where).toArray();
103
83
  });
104
84
  }
105
85
  async selectOne(collection, where = {}) {
106
- return this._queueRequest(async () => {
107
- await this.ensureCollection(collection);
86
+ return this._execute('selectOne', collection, async () => {
108
87
  const db = await this._connectionPromise;
109
- const result = await db.collection(collection).findOne(where);
110
- return result;
88
+ return await db.collection(collection).findOne(where);
111
89
  });
112
90
  }
113
91
  async set(collection, data, where) {
114
- return this._queueRequest(async () => {
115
- await this.ensureCollection(collection);
116
- const db = await this._connectionPromise;
117
- const existing = await db.collection(collection).findOne(where);
118
- if (!existing) {
119
- const result = await db.collection(collection).insertOne({ ...where, ...data });
120
- return result.insertedId;
121
- }
122
- else {
123
- const result = await db.collection(collection).updateOne(where, { $set: data });
124
- return result.modifiedCount;
125
- }
126
- });
92
+ const ex = await this.selectOne(collection, where);
93
+ return ex ? this.update(collection, data, where) : this.insert(collection, { ...where, ...data });
127
94
  }
128
- async increment(collection, increments, where = {}) {
129
- return this._queueRequest(async () => {
130
- await this.ensureCollection(collection);
95
+ async bulkInsert(collection, dataArray) {
96
+ return this._execute('bulkInsert', collection, async () => {
97
+ if (!dataArray.length)
98
+ return 0;
131
99
  const db = await this._connectionPromise;
132
- const result = await db.collection(collection).updateMany(where, { $inc: increments });
133
- return Number(result.modifiedCount);
100
+ const res = await db.collection(collection).insertMany(dataArray);
101
+ return res.insertedCount;
134
102
  });
135
103
  }
136
- async decrement(collection, decrements, where = {}) {
137
- return this._queueRequest(async () => {
138
- await this.ensureCollection(collection);
104
+ async increment(collection, incs, where = {}) {
105
+ return this._execute('increment', collection, async () => {
139
106
  const db = await this._connectionPromise;
140
- const negativeIncrements = {};
141
- for (const [key, value] of Object.entries(decrements)) {
142
- negativeIncrements[key] = -value;
143
- }
144
- const result = await db.collection(collection).updateMany(where, { $inc: negativeIncrements });
145
- return Number(result.modifiedCount);
107
+ const res = await db.collection(collection).updateMany(where, { $inc: incs });
108
+ return Number(res.modifiedCount);
146
109
  });
147
110
  }
148
- async close() {
149
- await this.client.close();
111
+ async decrement(collection, decs, where = {}) {
112
+ const incs = {};
113
+ for (const k in decs)
114
+ incs[k] = -decs[k];
115
+ return this.increment(collection, incs, where);
150
116
  }
117
+ async close() { await this.client.close(); }
151
118
  }
152
119
  exports.MongoDBDatabase = MongoDBDatabase;
153
120
  exports.default = MongoDBDatabase;
@@ -7,25 +7,23 @@ export declare class MySQLDatabase extends IDatabase {
7
7
  private _connected;
8
8
  private _connectionPromise;
9
9
  constructor(config: MySQLConfig);
10
- private _getColumnType;
11
- private _getBestColumnType;
12
- private _ensureMissingColumns;
13
- private _queueRequest;
10
+ private _execute;
14
11
  private _processQueue;
15
12
  query(sql: string, params?: any[]): Promise<any>;
16
- ensureTable(table: string, data?: Record<string, any>): Promise<void>;
13
+ private _ensureMissingColumns;
14
+ ensureTable(table: string, data?: any): Promise<void>;
17
15
  insert(table: string, data: Record<string, any>): Promise<number>;
18
16
  update(table: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
19
17
  delete(table: string, where: Record<string, any>): Promise<number>;
20
18
  select<T = any>(table: string, where?: Record<string, any> | null): Promise<T[]>;
21
- set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
22
19
  selectOne<T = any>(table: string, where?: Record<string, any> | null): Promise<T | null>;
20
+ set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
23
21
  bulkInsert(table: string, dataArray: Record<string, any>[]): Promise<number>;
22
+ increment(table: string, incs: Record<string, number>, where: Record<string, any>): Promise<number>;
23
+ decrement(table: string, decs: Record<string, number>, where: Record<string, any>): Promise<number>;
24
24
  close(): Promise<void>;
25
+ private _getColumnType;
25
26
  private _serializeValue;
26
- private _deserializeValue;
27
- increment(table: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
28
- decrement(table: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
29
27
  private _buildWhereClause;
30
28
  }
31
29
  export default MySQLDatabase;