@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
@@ -4,40 +4,36 @@ exports.RedisDatabase = void 0;
4
4
  const IDatabase_1 = require("./IDatabase");
5
5
  const redis_1 = require("redis");
6
6
  class RedisDatabase extends IDatabase_1.IDatabase {
7
- constructor(config = {}) {
7
+ constructor(config) {
8
8
  super();
9
9
  this.client = null;
10
10
  this.isConnecting = false;
11
- this.config = {
12
- host: config.host || '127.0.0.1',
13
- port: config.port || 6379,
14
- password: config.password,
15
- db: config.db || 0,
16
- connectTimeout: config.connectTimeout || 5000,
17
- };
11
+ this.config = config;
18
12
  this.keyPrefix = config.keyPrefix || 'app:';
19
13
  }
14
+ async _execute(op, table, fn) {
15
+ const start = Date.now();
16
+ const res = await fn();
17
+ this.recordMetric(op, table, Date.now() - start);
18
+ return res;
19
+ }
20
+ ;
20
21
  async connect() {
21
22
  if (this.client && this.client.isReady)
22
23
  return this.client;
23
24
  if (this.isConnecting) {
24
- while (this.isConnecting) {
25
- await new Promise(resolve => setTimeout(resolve, 100));
26
- }
25
+ while (this.isConnecting)
26
+ await new Promise(r => setTimeout(r, 100));
27
27
  return this.client;
28
28
  }
29
29
  this.isConnecting = true;
30
30
  try {
31
31
  this.client = (0, redis_1.createClient)({
32
- socket: {
33
- host: this.config.host,
34
- port: this.config.port,
35
- connectTimeout: this.config.connectTimeout,
36
- },
32
+ url: this.config.url,
33
+ socket: { host: this.config.host || '127.0.0.1', port: this.config.port || 6379 },
37
34
  password: this.config.password,
38
- database: this.config.db,
35
+ database: Number(this.config.database) || 0,
39
36
  });
40
- this.client.on('error', (err) => console.error('Redis Error:', err.message));
41
37
  await this.client.connect();
42
38
  return this.client;
43
39
  }
@@ -45,113 +41,91 @@ class RedisDatabase extends IDatabase_1.IDatabase {
45
41
  this.isConnecting = false;
46
42
  }
47
43
  }
48
- _getKey(table, id) {
49
- return `${this.keyPrefix}${table}:${id}`;
50
- }
51
- _getTableKey(table) {
52
- return `${this.keyPrefix}${table}:*`;
53
- }
44
+ _getKey(table, id) { return `${this.keyPrefix}${table}:${id}`; }
45
+ _getTableKey(table) { return `${this.keyPrefix}${table}:*`; }
54
46
  async select(table, where = {}) {
55
- const client = await this.connect();
56
- const pattern = this._getTableKey(table);
57
- const keys = await client.keys(pattern);
58
- if (!keys.length)
59
- return [];
60
- const values = await client.mGet(keys);
61
- return values
62
- .map(v => {
63
- try {
64
- return v ? JSON.parse(v) : null;
65
- }
66
- catch (e) {
67
- return null;
68
- }
69
- })
70
- .filter(Boolean)
71
- .filter(item => Object.entries(where).every(([k, val]) => item[k] === val));
47
+ return this._execute('select', table, async () => {
48
+ const client = await this.connect();
49
+ const keys = await client.keys(this._getTableKey(table));
50
+ if (!keys.length)
51
+ return [];
52
+ const vals = await client.mGet(keys);
53
+ return vals.map(v => v ? JSON.parse(v) : null).filter(Boolean)
54
+ .filter(item => Object.entries(where).every(([k, v]) => String(item[k]) === String(v)));
55
+ });
72
56
  }
73
57
  async selectOne(table, where = {}) {
74
- const results = await this.select(table, where);
75
- return results.length ? results[0] : null;
58
+ const res = await this.select(table, where);
59
+ return res[0] || null;
76
60
  }
77
61
  async insert(table, data) {
78
- const client = await this.connect();
79
- const insertData = { ...data };
80
- if (!insertData._id && !insertData.id) {
81
- insertData._id = Date.now().toString() + Math.random().toString(36).slice(2, 9);
82
- }
83
- const id = insertData._id || insertData.id;
84
- const key = this._getKey(table, id);
85
- await client.set(key, JSON.stringify(insertData));
86
- return insertData;
62
+ await this.runHooks('beforeInsert', table, data);
63
+ return this._execute('insert', table, async () => {
64
+ const client = await this.connect();
65
+ const d = { ...data };
66
+ if (!d._id && !d.id)
67
+ d._id = Date.now().toString() + Math.random().toString(36).slice(2, 9);
68
+ const id = String(d._id || d.id);
69
+ await client.set(this._getKey(table, id), JSON.stringify(d));
70
+ await this.runHooks('afterInsert', table, d);
71
+ return d._id || d.id;
72
+ });
87
73
  }
88
74
  async update(table, data, where) {
89
- const existing = await this.select(table, where);
90
- if (!existing.length)
91
- return 0;
92
- const client = await this.connect();
93
- for (const item of existing) {
94
- const merged = { ...item, ...data };
95
- const id = item._id || item.id;
96
- await client.set(this._getKey(table, id), JSON.stringify(merged));
97
- }
98
- return existing.length;
75
+ await this.runHooks('beforeUpdate', table, { data, where });
76
+ return this._execute('update', table, async () => {
77
+ const existing = await this.select(table, where);
78
+ const client = await this.connect();
79
+ for (const item of existing) {
80
+ const merged = { ...item, ...data };
81
+ await client.set(this._getKey(table, item._id || item.id), JSON.stringify(merged));
82
+ }
83
+ return existing.length;
84
+ });
99
85
  }
100
86
  async delete(table, where) {
101
- const existing = await this.select(table, where);
102
- if (!existing.length)
103
- return 0;
104
- const client = await this.connect();
105
- const keys = existing.map(item => this._getKey(table, item._id || item.id));
106
- await client.del(keys);
107
- return existing.length;
87
+ await this.runHooks('beforeDelete', table, where);
88
+ return this._execute('delete', table, async () => {
89
+ const existing = await this.select(table, where);
90
+ const client = await this.connect();
91
+ if (existing.length) {
92
+ const keys = existing.map(i => this._getKey(table, String(i._id || i.id)));
93
+ await client.del(keys);
94
+ }
95
+ return existing.length;
96
+ });
108
97
  }
109
98
  async set(table, data, where) {
110
- const existing = await this.selectOne(table, where);
111
- if (existing) {
112
- await this.update(table, data, where);
113
- return existing;
114
- }
115
- else {
116
- return await this.insert(table, { ...data, ...where });
117
- }
99
+ const ex = await this.selectOne(table, where);
100
+ return ex ? this.update(table, data, where) : this.insert(table, { ...data, ...where });
118
101
  }
119
102
  async bulkInsert(table, dataArray) {
120
- for (const data of dataArray) {
121
- await this.insert(table, data);
122
- }
103
+ for (const d of dataArray)
104
+ await this.insert(table, d);
123
105
  return dataArray.length;
124
106
  }
125
- async increment(table, increments, where = {}) {
126
- const records = await this.select(table, where);
127
- const client = await this.connect();
128
- for (const record of records) {
129
- for (const [field, value] of Object.entries(increments)) {
130
- record[field] = (Number(record[field]) || 0) + value;
107
+ async increment(table, incs, where = {}) {
108
+ return this._execute('increment', table, async () => {
109
+ const recs = await this.select(table, where);
110
+ const client = await this.connect();
111
+ for (const r of recs) {
112
+ for (const [f, v] of Object.entries(incs))
113
+ r[f] = (Number(r[f]) || 0) + v;
114
+ await client.set(this._getKey(table, r._id || r.id), JSON.stringify(r));
131
115
  }
132
- const id = record._id || record.id;
133
- await client.set(this._getKey(table, id), JSON.stringify(record));
134
- }
135
- return records.length;
116
+ return recs.length;
117
+ });
136
118
  }
137
- async decrement(table, decrements, where = {}) {
138
- const records = await this.select(table, where);
139
- const client = await this.connect();
140
- for (const record of records) {
141
- for (const [field, value] of Object.entries(decrements)) {
142
- record[field] = (Number(record[field]) || 0) - value;
143
- }
144
- const id = record._id || record.id;
145
- await client.set(this._getKey(table, id), JSON.stringify(record));
146
- }
147
- return records.length;
148
- }
149
- async close() {
150
- if (this.client) {
151
- await this.client.quit();
152
- this.client = null;
153
- }
119
+ async decrement(table, decs, where = {}) {
120
+ const incs = {};
121
+ for (const k in decs)
122
+ incs[k] = -decs[k];
123
+ return this.increment(table, incs, where);
154
124
  }
125
+ async close() { if (this.client) {
126
+ await this.client.quit();
127
+ this.client = null;
128
+ } }
155
129
  }
156
130
  exports.RedisDatabase = RedisDatabase;
157
131
  exports.default = RedisDatabase;
@@ -0,0 +1,20 @@
1
+ import { IDatabase } from './IDatabase';
2
+ export interface SeederField {
3
+ type: 'string' | 'number' | 'email' | 'id' | 'date' | 'boolean' | 'pick';
4
+ values?: any[];
5
+ min?: number;
6
+ max?: number;
7
+ length?: number;
8
+ }
9
+ export interface SeederSchema {
10
+ [key: string]: SeederField;
11
+ }
12
+ export declare class DataSeeder {
13
+ private db;
14
+ constructor(database: IDatabase);
15
+ private generateValue;
16
+ /**
17
+ * Seeds a table with mock data based on a schema.
18
+ */
19
+ seed(table: string, count: number, schema: SeederSchema): Promise<number>;
20
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataSeeder = void 0;
4
+ const index_1 = require("../functions/index");
5
+ class DataSeeder {
6
+ constructor(database) {
7
+ this.db = database;
8
+ }
9
+ generateValue(field) {
10
+ switch (field.type) {
11
+ case 'id': return Date.now().toString(36) + Math.random().toString(36).substr(2);
12
+ case 'email': return `${(0, index_1.randomText)(5)}@${(0, index_1.randomArray)(['gmail.com', 'outlook.com', 'zero.io'])}`;
13
+ case 'number': return (0, index_1.randomNumber)(field.min ?? 0, field.max ?? 1000);
14
+ case 'string': return (0, index_1.randomText)(field.length ?? 8);
15
+ case 'boolean': return Math.random() > 0.5;
16
+ case 'date': return new Date(Date.now() - Math.random() * 10000000000);
17
+ case 'pick': return (0, index_1.randomArray)(field.values ?? []);
18
+ default: return null;
19
+ }
20
+ }
21
+ /**
22
+ * Seeds a table with mock data based on a schema.
23
+ */
24
+ async seed(table, count, schema) {
25
+ const dataArray = [];
26
+ for (let i = 0; i < count; i++) {
27
+ const row = {};
28
+ for (const [key, field] of Object.entries(schema)) {
29
+ row[key] = this.generateValue(field);
30
+ }
31
+ dataArray.push(row);
32
+ }
33
+ await this.db.bulkInsert(table, dataArray);
34
+ return count;
35
+ }
36
+ }
37
+ exports.DataSeeder = DataSeeder;
@@ -3,23 +3,20 @@ import { SQLiteConfig } from './types';
3
3
  export declare class SQLiteDatabase extends IDatabase {
4
4
  private db;
5
5
  constructor(config: SQLiteConfig);
6
- private _detectColumnType;
7
- private _determineBestColumnType;
8
- private _ensureMissingColumns;
6
+ private _execute;
9
7
  query(sql: string, params?: any[]): Promise<any>;
10
- ensureTable(table: string, data?: Record<string, any>): Promise<void>;
8
+ ensureTable(table: string, data?: any): Promise<void>;
9
+ insert(table: string, data: any): Promise<number>;
10
+ update(table: string, data: any, where: any): Promise<number>;
11
+ delete(table: string, where: any): Promise<number>;
12
+ select<T = any>(table: string, where?: any): Promise<T[]>;
13
+ selectOne<T = any>(table: string, where?: any): Promise<T | null>;
14
+ set(table: string, data: any, where: any): Promise<any>;
15
+ bulkInsert(table: string, dataArray: any[]): Promise<number>;
16
+ increment(table: string, incs: any, where: any): Promise<number>;
17
+ decrement(table: string, decs: any, where: any): Promise<number>;
18
+ close(): Promise<void>;
11
19
  private _serializeValue;
12
- private _deserializeValue;
13
- insert(table: string, data: Record<string, any>): Promise<number>;
14
- update(table: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
15
- delete(table: string, where: Record<string, any>): Promise<number>;
16
- select<T = any>(table: string, where?: Record<string, any> | null): Promise<T[]>;
17
- set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
18
- selectOne<T = any>(table: string, where?: Record<string, any> | null): Promise<T | null>;
19
- bulkInsert(table: string, dataArray: Record<string, any>[]): Promise<number>;
20
- increment(table: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
21
- decrement(table: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
22
20
  private _buildWhereClause;
23
- close(): Promise<void>;
24
21
  }
25
22
  export default SQLiteDatabase;