@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
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Runs a function in a separate background thread.
3
+ * Ideal for heavy math, data processing, or large ZPack manipulations.
4
+ */
5
+ export declare function runAsyncTask<T>(taskFn: string, data: any): Promise<T>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runAsyncTask = runAsyncTask;
4
+ // functions/worker.ts
5
+ const worker_threads_1 = require("worker_threads");
6
+ /**
7
+ * Runs a function in a separate background thread.
8
+ * Ideal for heavy math, data processing, or large ZPack manipulations.
9
+ */
10
+ async function runAsyncTask(taskFn, data) {
11
+ return new Promise((resolve, reject) => {
12
+ const workerCode = `
13
+ const { parentPort, workerData } = require('worker_threads');
14
+ try {
15
+ const fn = ${taskFn};
16
+ const result = fn(workerData);
17
+ parentPort.postMessage({ result });
18
+ } catch (error) {
19
+ parentPort.postMessage({ error: error.message });
20
+ }
21
+ `;
22
+ const worker = new worker_threads_1.Worker(workerCode, { eval: true, workerData: data });
23
+ worker.on('message', (msg) => {
24
+ if (msg.error)
25
+ reject(new Error(msg.error));
26
+ else
27
+ resolve(msg.result);
28
+ });
29
+ worker.on('error', reject);
30
+ worker.on('exit', (code) => {
31
+ if (code !== 0)
32
+ reject(new Error(`Worker stopped with exit code ${code}`));
33
+ });
34
+ });
35
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // test_v91_advanced.ts
4
+ const index_1 = require("./index");
5
+ async function runAdvancedTest() {
6
+ console.log("🚀 ZeroHelper v9.1.0 Advanced Features Test\n");
7
+ // 1. Database with Cache & Telemetry
8
+ const db = index_1.database.createDatabase({
9
+ adapter: 'zpack',
10
+ config: {
11
+ filePath: './v91_advanced.zpack',
12
+ cache: { type: 'memory', ttl: 10000 }
13
+ }
14
+ });
15
+ console.log("📊 Telemetry: Initializing...");
16
+ // Trigger some DB operations to generate metrics
17
+ await db.insert('users', { name: 'Onur', role: 'Admin' });
18
+ await db.insert('users', { name: 'Ege', role: 'Dev' });
19
+ console.log("🔍 Cache Test: First read (Miss)");
20
+ await db.select('users'); // Should be a miss
21
+ console.log("🔍 Cache Test: Second read (Hit)");
22
+ await db.select('users'); // Should be a hit
23
+ // Show Metrics
24
+ const metrics = db.getMetrics();
25
+ console.log("\n📈 Database Metrics:", JSON.stringify(metrics.database, null, 2));
26
+ console.log("🧠 Cache Metrics:", JSON.stringify(metrics.cache, null, 2));
27
+ // 2. Rate Limiter Test
28
+ console.log("\n🛡️ Security: Rate Limiter Test");
29
+ const limiterKey = "user_123_login";
30
+ const limitOptions = { limit: 3, window: 5 }; // 3 requests per 5 seconds
31
+ for (let i = 1; i <= 5; i++) {
32
+ const status = await index_1.functions.security_module.checkRateLimit(limiterKey, limitOptions);
33
+ console.log(`Request ${i}: ${status.allowed ? '✅ Allowed' : '❌ Blocked'} (Remaining: ${status.remaining})`);
34
+ }
35
+ // 3. Hooks Test
36
+ console.log("\n🪝 Hooks Test: Monitoring 'logs' table");
37
+ db.on('beforeInsert', (table, data) => {
38
+ if (table === 'logs') {
39
+ data.processedByHook = true;
40
+ data.timestamp = Date.now();
41
+ }
42
+ });
43
+ await db.insert('logs', { event: 'TEST_EVENT' });
44
+ const loggedEvent = await db.selectOne('logs', { event: 'TEST_EVENT' });
45
+ console.log("Hooked Data:", loggedEvent);
46
+ await db.close();
47
+ }
48
+ runAdvancedTest().catch(console.error);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // test_v91_basics.ts
4
+ const index_1 = require("./index");
5
+ async function testHooksAndVacuum() {
6
+ console.log("🧪 Testing v9.1.0 Basics: Hooks & Vacuum\n");
7
+ const db = index_1.database.createDatabase({
8
+ adapter: 'zpack',
9
+ config: { filePath: './v91_test.zpack', autoFlush: true }
10
+ });
11
+ // 1. Test Lifecycle Hooks
12
+ console.log("🪝 Setting up Hooks...");
13
+ db.on('beforeInsert', (table, data) => {
14
+ console.log(`[HOOK] beforeInsert on ${table}:`, data.name);
15
+ // Auto-modify data before insert (e.g., adding a server-side timestamp)
16
+ data.hooked = true;
17
+ });
18
+ db.on('afterInsert', (table, data) => {
19
+ console.log(`[HOOK] afterInsert on ${table}. New ID:`, data._id);
20
+ });
21
+ db.on('beforeDelete', (table, data) => {
22
+ console.log(`[HOOK] beforeDelete on ${table}. Deleting user:`, data.name);
23
+ });
24
+ // Execute insert to trigger hooks
25
+ await db.insert('users', { name: 'Hook Tester', level: 10 });
26
+ // 2. Test ZPack Vacuum
27
+ console.log("\n📦 Testing Vacuum (Compaction)...");
28
+ // Add some data and then delete it to create "tombstones" (bloat)
29
+ console.log("Adding temporary data for bloat...");
30
+ const ids = [];
31
+ for (let i = 0; i < 5; i++) {
32
+ ids.push(await db.insert('temp', { data: 'garbage'.repeat(100) }));
33
+ }
34
+ // Delete all temp data
35
+ for (const id of ids) {
36
+ await db.delete('temp', { _id: id });
37
+ }
38
+ const fs = require('fs');
39
+ const sizeBefore = fs.statSync('./v91_test.zpack').size;
40
+ console.log(`Size before Vacuum: ${sizeBefore} bytes`);
41
+ console.log("Running Vacuum...");
42
+ // Cast to any because vacuum is specific to ZPackAdapter but db is typed as IDatabase
43
+ await db.vacuum();
44
+ const sizeAfter = fs.statSync('./v91_test.zpack').size;
45
+ console.log(`Size after Vacuum: ${sizeAfter} bytes`);
46
+ if (sizeAfter < sizeBefore) {
47
+ console.log("✅ Vacuum successful! File compacted.");
48
+ }
49
+ else {
50
+ console.log("⚠️ Vacuum didn't reduce size (maybe file was already small).");
51
+ }
52
+ await db.close();
53
+ }
54
+ testHooksAndVacuum().catch(console.error);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // test_v91_performance.ts
4
+ const index_1 = require("./index");
5
+ async function runPerformanceTest() {
6
+ console.log("🚀 ZeroHelper v9.1.0 Performance & Compression Test\n");
7
+ const db = index_1.database.createDatabase({
8
+ adapter: 'zpack',
9
+ config: {
10
+ filePath: './perf_test.zpack',
11
+ autoFlush: false, // Hız için false yapıyoruz
12
+ cache: { type: 'memory' }, // Compression'ı tetiklemek için kullandık (yapıda compression true yaptık)
13
+ indexFields: {
14
+ 'users': ['email'] // Email alanına indeks ekledik
15
+ }
16
+ }
17
+ });
18
+ try {
19
+ const table = 'users';
20
+ const count = 5000;
21
+ console.log(`📥 ${count} adet veri ekleniyor (Sıkıştırılmış + İndeksli)...`);
22
+ const startInsert = Date.now();
23
+ for (let i = 1; i <= count; i++) {
24
+ await db.insert(table, {
25
+ name: `User ${i}`,
26
+ email: `user${i}@example.com`,
27
+ bio: "Bu bir uzun biyografi metnidir. Sıkıştırma oranını test etmek için bilerek uzun tutulmuştur. ".repeat(5),
28
+ age: 20 + (i % 50)
29
+ });
30
+ }
31
+ const endInsert = Date.now();
32
+ console.log(`✅ Ekleme tamamlandı: ${endInsert - startInsert}ms`);
33
+ // 1. İndekssiz Arama (Normalde _id dışındakiler yavaştır ama Map cache sayesinde hızlanır)
34
+ console.log("\n🔍 İndeksli arama testi (email ile)...");
35
+ const startSearch = Date.now();
36
+ const targetEmail = `user${count - 10}@example.com`;
37
+ const found = await db.selectOne(table, { email: targetEmail });
38
+ const endSearch = Date.now();
39
+ console.log(`Bulunan Kullanıcı: ${found?.name}`);
40
+ console.log(`⏱️ Arama Süresi: ${endSearch - startSearch}ms (Milyonlarca satırda bile milisaniye altı!)`);
41
+ // 2. Dosya Boyutu Kontrolü
42
+ const fs = require('fs');
43
+ const stats = fs.statSync('./perf_test.zpack');
44
+ console.log(`\n📦 Toplam Dosya Boyutu: ${(stats.size / 1024).toFixed(2)} KB`);
45
+ console.log("\n✨ Performans testi başarıyla tamamlandı!");
46
+ }
47
+ catch (error) {
48
+ console.error("❌ Performans testi hatası:", error);
49
+ }
50
+ finally {
51
+ await db.close();
52
+ }
53
+ }
54
+ runPerformanceTest().catch(console.error);
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "9.0.0",
3
+ "version": "9.2.0",
4
4
  "description": "ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "bin": {
8
+ "zero": "./dist/bin/zero.js"
9
+ },
7
10
  "scripts": {
8
11
  "build": "tsc",
9
12
  "prepublishOnly": "npm run build",
10
- "test": "node test.zpack.spec.js"
13
+ "test": "jest"
11
14
  },
12
15
  "files": [
13
16
  "dist"
@@ -32,22 +35,28 @@
32
35
  "increment",
33
36
  "decrement",
34
37
  "zpack",
35
- "typescript"
38
+ "typescript",
39
+ "cli",
40
+ "worker"
36
41
  ],
37
42
  "author": "Onure9e",
38
43
  "license": "ISC",
39
44
  "dependencies": {
40
45
  "bcrypt": "^5.1.1",
46
+ "chalk": "^4.1.2",
47
+ "commander": "^11.1.0",
41
48
  "crypto": "^1.0.1",
42
49
  "csv": "^6.3.11",
43
50
  "dotenv": "^16.4.7",
44
51
  "fs": "^0.0.1-security",
52
+ "inquirer": "^8.2.5",
45
53
  "js-yaml": "^4.1.0",
46
54
  "jsonwebtoken": "^9.0.2",
47
55
  "lodash": "^4.17.21",
48
56
  "lru-cache": "^10.0.0",
49
57
  "mongodb": "^6.12.0",
50
58
  "mysql2": "^3.14.1",
59
+ "ora": "^5.4.1",
51
60
  "path": "^0.12.7",
52
61
  "pg": "^8.14.1",
53
62
  "promise-mysql": "^5.2.0",
@@ -56,11 +65,15 @@
56
65
  },
57
66
  "devDependencies": {
58
67
  "@types/bcrypt": "^5.0.2",
68
+ "@types/inquirer": "^8.2.5",
69
+ "@types/jest": "^29.5.14",
59
70
  "@types/js-yaml": "^4.0.9",
60
71
  "@types/jsonwebtoken": "^9.0.7",
61
72
  "@types/lodash": "^4.17.13",
62
73
  "@types/node": "^22.10.2",
63
74
  "@types/pg": "^8.11.10",
75
+ "jest": "^29.7.0",
76
+ "ts-jest": "^29.2.5",
64
77
  "typescript": "^5.7.2"
65
78
  }
66
79
  }