@onurege3467/zerohelper 10.2.6 → 11.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 +9 -704
- package/dist/index.js +19 -44
- package/package.json +21 -70
- package/dist/bin/commands/cache.d.ts +0 -2
- package/dist/bin/commands/cache.js +0 -92
- package/dist/bin/commands/db-backup.d.ts +0 -3
- package/dist/bin/commands/db-backup.js +0 -118
- package/dist/bin/commands/db.d.ts +0 -2
- package/dist/bin/commands/db.js +0 -334
- package/dist/bin/commands/import-export.d.ts +0 -3
- package/dist/bin/commands/import-export.js +0 -123
- package/dist/bin/commands/init.d.ts +0 -2
- package/dist/bin/commands/init.js +0 -85
- package/dist/bin/commands/migrate.d.ts +0 -3
- package/dist/bin/commands/migrate.js +0 -167
- package/dist/bin/commands/repl.d.ts +0 -2
- package/dist/bin/commands/repl.js +0 -96
- package/dist/bin/commands/seed.d.ts +0 -2
- package/dist/bin/commands/seed.js +0 -76
- package/dist/bin/commands/zpack.d.ts +0 -2
- package/dist/bin/commands/zpack.js +0 -36
- package/dist/bin/index.d.ts +0 -2
- package/dist/bin/index.js +0 -28
- package/dist/bin/types.d.ts +0 -22
- package/dist/bin/types.js +0 -2
- package/dist/bin/utils/config.d.ts +0 -3
- package/dist/bin/utils/config.js +0 -78
- package/dist/bin/utils/prompts.d.ts +0 -3
- package/dist/bin/utils/prompts.js +0 -115
- package/dist/bin/zero.d.ts +0 -2
- package/dist/bin/zero.js +0 -849
- package/dist/database/IDatabase.d.ts +0 -71
- package/dist/database/IDatabase.js +0 -48
- package/dist/database/cacheWrapper.d.ts +0 -34
- package/dist/database/cacheWrapper.js +0 -214
- package/dist/database/index.d.ts +0 -12
- package/dist/database/index.js +0 -100
- package/dist/database/json.d.ts +0 -32
- package/dist/database/json.js +0 -208
- package/dist/database/migration.d.ts +0 -21
- package/dist/database/migration.js +0 -97
- package/dist/database/mongodb.d.ts +0 -26
- package/dist/database/mongodb.js +0 -145
- package/dist/database/mysql.d.ts +0 -29
- package/dist/database/mysql.js +0 -282
- package/dist/database/pg.d.ts +0 -28
- package/dist/database/pg.js +0 -200
- package/dist/database/redis.d.ts +0 -31
- package/dist/database/redis.js +0 -176
- package/dist/database/seeder.d.ts +0 -20
- package/dist/database/seeder.js +0 -37
- package/dist/database/sqlite.d.ts +0 -26
- package/dist/database/sqlite.js +0 -211
- package/dist/database/telemetry.d.ts +0 -35
- package/dist/database/telemetry.js +0 -41
- package/dist/database/toon.d.ts +0 -33
- package/dist/database/toon.js +0 -244
- package/dist/database/types.d.ts +0 -71
- package/dist/database/types.js +0 -2
- package/dist/database/zpack.d.ts +0 -75
- package/dist/database/zpack.js +0 -616
- package/dist/functions/index.d.ts +0 -199
- package/dist/functions/index.js +0 -682
- package/dist/functions/security.d.ts +0 -15
- package/dist/functions/security.js +0 -46
- package/dist/functions/toon.d.ts +0 -10
- package/dist/functions/toon.js +0 -214
- package/dist/functions/worker.d.ts +0 -5
- package/dist/functions/worker.js +0 -35
- package/dist/index.d.ts +0 -8
- package/dist/migrations/1767521950635_test_migration.d.ts +0 -3
- package/dist/migrations/1767521950635_test_migration.js +0 -11
- package/dist/migrations/1767522158826_create_users_table.d.ts +0 -2
- package/dist/migrations/1767522158826_create_users_table.js +0 -11
- package/dist/package.json +0 -79
- package/dist/tests/test.d.ts +0 -1
- package/dist/tests/test.js +0 -26
- package/dist/zero.config.d.ts +0 -10
- package/dist/zero.config.js +0 -13
package/dist/bin/commands/db.js
DELETED
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.dbCommand = void 0;
|
|
7
|
-
const commander_1 = require("commander");
|
|
8
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
const ora_1 = __importDefault(require("ora"));
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
const path_1 = __importDefault(require("path"));
|
|
12
|
-
const index_1 = require("../../index");
|
|
13
|
-
const package_json_1 = require("../../package.json");
|
|
14
|
-
const config_1 = require("../utils/config");
|
|
15
|
-
const prompts_1 = require("../utils/prompts");
|
|
16
|
-
exports.dbCommand = new commander_1.Command().name('db');
|
|
17
|
-
exports.dbCommand
|
|
18
|
-
.command('test')
|
|
19
|
-
.description('Test database connection and show basic stats')
|
|
20
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
21
|
-
.action(async (options) => {
|
|
22
|
-
const spinner = (0, ora_1.default)('Testing database connection...').start();
|
|
23
|
-
try {
|
|
24
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
25
|
-
spinner.succeed(chalk_1.default.green('✅ Database connection successful'));
|
|
26
|
-
console.log(chalk_1.default.bold('\n📊 Database Stats:'));
|
|
27
|
-
console.log(chalk_1.default.cyan(' Adapter:'), chalk_1.default.white(db.constructor.name));
|
|
28
|
-
console.log(chalk_1.default.cyan(' Status:'), chalk_1.default.green('Connected'));
|
|
29
|
-
try {
|
|
30
|
-
const metrics = db.getMetrics();
|
|
31
|
-
console.log(chalk_1.default.cyan(' Operations:'), chalk_1.default.white(metrics.database.count));
|
|
32
|
-
console.log(chalk_1.default.cyan(' Avg Latency:'), chalk_1.default.white(`${metrics.database.averageDuration.toFixed(2)}ms`));
|
|
33
|
-
}
|
|
34
|
-
catch (err) {
|
|
35
|
-
console.log(chalk_1.default.yellow(' Note: Metrics not available for this adapter'));
|
|
36
|
-
}
|
|
37
|
-
await db.close();
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
spinner.fail(chalk_1.default.red('❌ Connection failed'));
|
|
41
|
-
console.error(chalk_1.default.red(error.message));
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
exports.dbCommand
|
|
46
|
-
.command('stats')
|
|
47
|
-
.description('Show detailed database performance metrics')
|
|
48
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
49
|
-
.action(async (options) => {
|
|
50
|
-
try {
|
|
51
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
52
|
-
const metrics = db.getMetrics();
|
|
53
|
-
console.log(chalk_1.default.bold('\n📊 Database Performance Dashboard'));
|
|
54
|
-
console.log(chalk_1.default.gray('─'.repeat(50)));
|
|
55
|
-
console.log(chalk_1.default.bold('\n🔹 Database Operations:'));
|
|
56
|
-
if (metrics.database) {
|
|
57
|
-
const count = metrics.database.count ?? 0;
|
|
58
|
-
const totalDuration = metrics.database.totalDuration ?? 0;
|
|
59
|
-
const avgLatency = metrics.database.averageDuration ?? 0;
|
|
60
|
-
const minDuration = metrics.database.minDuration ?? 0;
|
|
61
|
-
const maxDuration = metrics.database.maxDuration ?? 0;
|
|
62
|
-
const formatNum = (n) => typeof n === 'number' ? n.toFixed(2) : n;
|
|
63
|
-
console.log(` Total Operations: ${chalk_1.default.cyan(count)}`);
|
|
64
|
-
console.log(` Total Duration: ${chalk_1.default.cyan(`${formatNum(totalDuration)} ms`)}`);
|
|
65
|
-
console.log(` Avg Latency: ${chalk_1.default.green(`${formatNum(avgLatency)} ms`)}`);
|
|
66
|
-
console.log(` Min Duration: ${chalk_1.default.yellow(`${formatNum(minDuration)} ms`)}`);
|
|
67
|
-
console.log(` Max Duration: ${chalk_1.default.yellow(`${formatNum(maxDuration)} ms`)}`);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
console.log(chalk_1.default.gray(' No metrics available for this adapter'));
|
|
71
|
-
}
|
|
72
|
-
if (metrics.cache) {
|
|
73
|
-
console.log(chalk_1.default.bold('\n🔹 Cache Performance:'));
|
|
74
|
-
console.log(` Total Requests: ${chalk_1.default.cyan((metrics.cache.hits || 0) + (metrics.cache.misses || 0))}`);
|
|
75
|
-
console.log(` Cache Hits: ${chalk_1.default.green(metrics.cache.hits || 0)}`);
|
|
76
|
-
console.log(` Cache Misses: ${chalk_1.default.red(metrics.cache.misses || 0)}`);
|
|
77
|
-
const hits = metrics.cache.hits || 0;
|
|
78
|
-
const misses = metrics.cache.misses || 0;
|
|
79
|
-
const total = hits + misses;
|
|
80
|
-
if (total > 0) {
|
|
81
|
-
const ratio = (hits / total) * 100;
|
|
82
|
-
console.log(` Hit Ratio: ${ratio.toFixed(1)}% ${ratio > 80 ? '✅' : ratio > 50 ? '⚠️' : '❌'}`);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
console.log(chalk_1.default.gray('\n' + '─'.repeat(50)));
|
|
86
|
-
await db.close();
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
console.error(chalk_1.default.red(`Error: ${error.message}`));
|
|
90
|
-
process.exit(1);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
exports.dbCommand
|
|
94
|
-
.command('seed')
|
|
95
|
-
.description('Seed table with mock data')
|
|
96
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
97
|
-
.option('-t, --table <name>', 'Table name')
|
|
98
|
-
.option('-n, --count <number>', 'Number of records', '10')
|
|
99
|
-
.action(async (options) => {
|
|
100
|
-
if (!options.table) {
|
|
101
|
-
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
102
|
-
process.exit(1);
|
|
103
|
-
}
|
|
104
|
-
const spinner = (0, ora_1.default)(`Seeding ${options.table}...`).start();
|
|
105
|
-
try {
|
|
106
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
107
|
-
const seeder = new index_1.database.DataSeeder(db);
|
|
108
|
-
const schema = {};
|
|
109
|
-
const fieldTypes = ['string', 'number', 'email', 'boolean', 'date'];
|
|
110
|
-
for (let i = 0; i < 3; i++) {
|
|
111
|
-
const fieldType = fieldTypes[Math.floor(Math.random() * fieldTypes.length)];
|
|
112
|
-
schema[`field_${i + 1}`] = { type: fieldType };
|
|
113
|
-
}
|
|
114
|
-
const count = await seeder.seed(options.table, parseInt(options.count), schema);
|
|
115
|
-
spinner.succeed(chalk_1.default.green(`✅ Seeded ${count} records into ${options.table}`));
|
|
116
|
-
await db.close();
|
|
117
|
-
}
|
|
118
|
-
catch (error) {
|
|
119
|
-
spinner.fail(chalk_1.default.red('❌ Seeding failed'));
|
|
120
|
-
console.error(chalk_1.default.red(error.message));
|
|
121
|
-
process.exit(1);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
exports.dbCommand
|
|
125
|
-
.command('backup')
|
|
126
|
-
.description('Backup database to timestamped file')
|
|
127
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
128
|
-
.option('-o, --output <dir>', 'Output directory for backups', './backups')
|
|
129
|
-
.action(async (options) => {
|
|
130
|
-
const spinner = (0, ora_1.default)('Creating backup...').start();
|
|
131
|
-
try {
|
|
132
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
133
|
-
const config = (0, config_1.loadConfig)(options.config);
|
|
134
|
-
const backupDir = path_1.default.resolve(process.cwd(), options.output);
|
|
135
|
-
if (!fs_1.default.existsSync(backupDir)) {
|
|
136
|
-
fs_1.default.mkdirSync(backupDir, { recursive: true });
|
|
137
|
-
}
|
|
138
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
|
|
139
|
-
const backupFile = path_1.default.join(backupDir, `backup_${timestamp}.zerohelper.json`);
|
|
140
|
-
let tables = [];
|
|
141
|
-
if (config.adapter === 'json' && config.config.path) {
|
|
142
|
-
const dbPath = path_1.default.resolve(process.cwd(), config.config.path);
|
|
143
|
-
if (fs_1.default.existsSync(dbPath)) {
|
|
144
|
-
const dbContent = JSON.parse(fs_1.default.readFileSync(dbPath, 'utf-8'));
|
|
145
|
-
tables = Object.keys(dbContent);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else if (config.adapter === 'sqlite' && config.config.path) {
|
|
149
|
-
tables = await db.tables?.() || [];
|
|
150
|
-
}
|
|
151
|
-
else if (config.adapter === 'zpack' && config.config.path) {
|
|
152
|
-
tables = await db.tables?.() || [];
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
tables = ['users', 'products', 'orders', 'migrations', 'migration_test', 'test_backup'];
|
|
156
|
-
}
|
|
157
|
-
const backupData = {
|
|
158
|
-
version: package_json_1.version,
|
|
159
|
-
timestamp: new Date().toISOString(),
|
|
160
|
-
config: config,
|
|
161
|
-
data: {}
|
|
162
|
-
};
|
|
163
|
-
for (const table of tables) {
|
|
164
|
-
try {
|
|
165
|
-
const records = await db.select(table);
|
|
166
|
-
if (records.length > 0) {
|
|
167
|
-
backupData.data[table] = records;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
catch (err) {
|
|
171
|
-
// Table doesn't exist or can't be accessed
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
fs_1.default.writeFileSync(backupFile, JSON.stringify(backupData, null, 2));
|
|
175
|
-
const fileSize = fs_1.default.statSync(backupFile).size;
|
|
176
|
-
spinner.succeed(chalk_1.default.green(`✅ Backup created: ${backupFile}`));
|
|
177
|
-
console.log(chalk_1.default.gray(` Size: ${(0, config_1.formatBytes)(fileSize)}`));
|
|
178
|
-
console.log(chalk_1.default.gray(` Tables: ${Object.keys(backupData.data).join(', ') || 'none'}`));
|
|
179
|
-
await db.close();
|
|
180
|
-
}
|
|
181
|
-
catch (error) {
|
|
182
|
-
spinner.fail(chalk_1.default.red('❌ Backup failed'));
|
|
183
|
-
console.error(chalk_1.default.red(error.message));
|
|
184
|
-
process.exit(1);
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
exports.dbCommand
|
|
188
|
-
.command('restore')
|
|
189
|
-
.description('Restore database from backup file')
|
|
190
|
-
.argument('<backup-file>', 'Path to backup file')
|
|
191
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
192
|
-
.action(async (backupFile, options) => {
|
|
193
|
-
if (!fs_1.default.existsSync(backupFile)) {
|
|
194
|
-
console.error(chalk_1.default.red(`Error: Backup file not found: ${backupFile}`));
|
|
195
|
-
process.exit(1);
|
|
196
|
-
}
|
|
197
|
-
const backupData = JSON.parse(fs_1.default.readFileSync(backupFile, 'utf-8'));
|
|
198
|
-
const confirmed = await (0, prompts_1.confirmAction)(chalk_1.default.yellow(`⚠️ This will restore data from backup. Are you sure?`));
|
|
199
|
-
if (!confirmed) {
|
|
200
|
-
console.log(chalk_1.default.yellow('Restore cancelled'));
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
const spinner = (0, ora_1.default)('Restoring database...').start();
|
|
204
|
-
try {
|
|
205
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
206
|
-
for (const [table, records] of Object.entries(backupData.data)) {
|
|
207
|
-
const rows = records;
|
|
208
|
-
if (Array.isArray(rows) && rows.length > 0) {
|
|
209
|
-
try {
|
|
210
|
-
await db.bulkInsert(table, rows);
|
|
211
|
-
spinner.text = `✅ ${table}: ${rows.length} records`;
|
|
212
|
-
}
|
|
213
|
-
catch (err) {
|
|
214
|
-
spinner.text = `⚠️ ${table}: failed`;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
spinner.succeed(chalk_1.default.green(`✅ Database restored from ${backupFile}`));
|
|
219
|
-
await db.close();
|
|
220
|
-
}
|
|
221
|
-
catch (error) {
|
|
222
|
-
spinner.fail(chalk_1.default.red('❌ Restore failed'));
|
|
223
|
-
console.error(chalk_1.default.red(error.message));
|
|
224
|
-
process.exit(1);
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
exports.dbCommand
|
|
228
|
-
.command('export')
|
|
229
|
-
.description('Export table data to file')
|
|
230
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
231
|
-
.option('-t, --table <name>', 'Table name to export')
|
|
232
|
-
.option('-f, --format <format>', 'Output format (json|csv)', 'json')
|
|
233
|
-
.option('-o, --output <file>', 'Output file path')
|
|
234
|
-
.action(async (options) => {
|
|
235
|
-
if (!options.table) {
|
|
236
|
-
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
237
|
-
process.exit(1);
|
|
238
|
-
}
|
|
239
|
-
const spinner = (0, ora_1.default)(`Exporting ${options.table}...`).start();
|
|
240
|
-
try {
|
|
241
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
242
|
-
const records = await db.select(options.table);
|
|
243
|
-
if (records.length === 0) {
|
|
244
|
-
spinner.warn(chalk_1.default.yellow(`⚠️ No records found in ${options.table}`));
|
|
245
|
-
await db.close();
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
|
|
249
|
-
const ext = options.format === 'csv' ? 'csv' : 'json';
|
|
250
|
-
const defaultFile = `${options.table}_export_${timestamp}.${ext}`;
|
|
251
|
-
const outputFile = options.output || path_1.default.join(process.cwd(), 'exports', defaultFile);
|
|
252
|
-
const outputDir = path_1.default.dirname(outputFile);
|
|
253
|
-
if (!fs_1.default.existsSync(outputDir)) {
|
|
254
|
-
fs_1.default.mkdirSync(outputDir, { recursive: true });
|
|
255
|
-
}
|
|
256
|
-
let content = '';
|
|
257
|
-
if (options.format === 'csv') {
|
|
258
|
-
const headers = Object.keys(records[0]);
|
|
259
|
-
const csvRows = [
|
|
260
|
-
headers.join(','),
|
|
261
|
-
...records.map((row) => headers.map(h => {
|
|
262
|
-
const val = row[h];
|
|
263
|
-
return typeof val === 'string' && val.includes(',') ? `"${val}"` : val;
|
|
264
|
-
}).join(','))
|
|
265
|
-
];
|
|
266
|
-
content = csvRows.join('\n');
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
content = JSON.stringify(records, null, 2);
|
|
270
|
-
}
|
|
271
|
-
fs_1.default.writeFileSync(outputFile, content);
|
|
272
|
-
const fileSize = fs_1.default.statSync(outputFile).size;
|
|
273
|
-
spinner.succeed(chalk_1.default.green(`✅ Exported ${records.length} records to ${outputFile}`));
|
|
274
|
-
console.log(chalk_1.default.gray(` Size: ${(0, config_1.formatBytes)(fileSize)}`));
|
|
275
|
-
console.log(chalk_1.default.gray(` Format: ${options.format.toUpperCase()}`));
|
|
276
|
-
await db.close();
|
|
277
|
-
}
|
|
278
|
-
catch (error) {
|
|
279
|
-
spinner.fail(chalk_1.default.red('❌ Export failed'));
|
|
280
|
-
console.error(chalk_1.default.red(error.message));
|
|
281
|
-
process.exit(1);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
exports.dbCommand
|
|
285
|
-
.command('import')
|
|
286
|
-
.description('Import data from file to table')
|
|
287
|
-
.argument('<file>', 'Input file path')
|
|
288
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
289
|
-
.option('-t, --table <name>', 'Table name')
|
|
290
|
-
.option('-f, --format <format>', 'Input format (json|csv)', 'json')
|
|
291
|
-
.action(async (inputFile, options) => {
|
|
292
|
-
if (!options.table) {
|
|
293
|
-
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
294
|
-
process.exit(1);
|
|
295
|
-
}
|
|
296
|
-
if (!fs_1.default.existsSync(inputFile)) {
|
|
297
|
-
console.error(chalk_1.default.red(`Error: File not found: ${inputFile}`));
|
|
298
|
-
process.exit(1);
|
|
299
|
-
}
|
|
300
|
-
const confirmed = await (0, prompts_1.confirmAction)(chalk_1.default.yellow(`⚠️ This will import data to ${options.table}. Are you sure?`));
|
|
301
|
-
if (!confirmed) {
|
|
302
|
-
console.log(chalk_1.default.yellow('Import cancelled'));
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
const spinner = (0, ora_1.default)(`Importing to ${options.table}...`).start();
|
|
306
|
-
try {
|
|
307
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
308
|
-
const content = fs_1.default.readFileSync(inputFile, 'utf-8');
|
|
309
|
-
let data = [];
|
|
310
|
-
if (options.format === 'csv') {
|
|
311
|
-
const lines = content.trim().split('\n');
|
|
312
|
-
const headers = lines[0].split(',');
|
|
313
|
-
data = lines.slice(1).map((line) => {
|
|
314
|
-
const values = line.split(',');
|
|
315
|
-
const row = {};
|
|
316
|
-
headers.forEach((h, i) => {
|
|
317
|
-
row[h] = values[i]?.replace(/"/g, '').trim();
|
|
318
|
-
});
|
|
319
|
-
return row;
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
else {
|
|
323
|
-
data = JSON.parse(content);
|
|
324
|
-
}
|
|
325
|
-
const count = await db.bulkInsert(options.table, data);
|
|
326
|
-
spinner.succeed(chalk_1.default.green(`✅ Imported ${count} records to ${options.table}`));
|
|
327
|
-
await db.close();
|
|
328
|
-
}
|
|
329
|
-
catch (error) {
|
|
330
|
-
spinner.fail(chalk_1.default.red('❌ Import failed'));
|
|
331
|
-
console.error(chalk_1.default.red(error.message));
|
|
332
|
-
process.exit(1);
|
|
333
|
-
}
|
|
334
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.importCommand = exports.exportCommand = void 0;
|
|
7
|
-
const commander_1 = require("commander");
|
|
8
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
const ora_1 = __importDefault(require("ora"));
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
const path_1 = __importDefault(require("path"));
|
|
12
|
-
const config_1 = require("../utils/config");
|
|
13
|
-
const prompts_1 = require("../utils/prompts");
|
|
14
|
-
exports.exportCommand = new commander_1.Command().name('db');
|
|
15
|
-
exports.exportCommand
|
|
16
|
-
.command('export')
|
|
17
|
-
.description('Export table data to file')
|
|
18
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
19
|
-
.option('-t, --table <name>', 'Table name to export')
|
|
20
|
-
.option('-f, --format <format>', 'Output format (json|csv)', 'json')
|
|
21
|
-
.option('-o, --output <file>', 'Output file path')
|
|
22
|
-
.action(async (options) => {
|
|
23
|
-
if (!options.table) {
|
|
24
|
-
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
25
|
-
process.exit(1);
|
|
26
|
-
}
|
|
27
|
-
const spinner = (0, ora_1.default)(`Exporting ${options.table}...`).start();
|
|
28
|
-
try {
|
|
29
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
30
|
-
const records = await db.select(options.table);
|
|
31
|
-
if (records.length === 0) {
|
|
32
|
-
spinner.warn(chalk_1.default.yellow(`⚠️ No records found in ${options.table}`));
|
|
33
|
-
await db.close();
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
|
|
37
|
-
const ext = options.format === 'csv' ? 'csv' : 'json';
|
|
38
|
-
const defaultFile = `${options.table}_export_${timestamp}.${ext}`;
|
|
39
|
-
const outputFile = options.output || path_1.default.join(process.cwd(), 'exports', defaultFile);
|
|
40
|
-
const outputDir = path_1.default.dirname(outputFile);
|
|
41
|
-
if (!fs_1.default.existsSync(outputDir)) {
|
|
42
|
-
fs_1.default.mkdirSync(outputDir, { recursive: true });
|
|
43
|
-
}
|
|
44
|
-
let content = '';
|
|
45
|
-
if (options.format === 'csv') {
|
|
46
|
-
const headers = Object.keys(records[0]);
|
|
47
|
-
const csvRows = [
|
|
48
|
-
headers.join(','),
|
|
49
|
-
...records.map((row) => headers.map(h => {
|
|
50
|
-
const val = row[h];
|
|
51
|
-
return typeof val === 'string' && val.includes(',') ? `"${val}"` : val;
|
|
52
|
-
}).join(','))
|
|
53
|
-
];
|
|
54
|
-
content = csvRows.join('\n');
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
content = JSON.stringify(records, null, 2);
|
|
58
|
-
}
|
|
59
|
-
fs_1.default.writeFileSync(outputFile, content);
|
|
60
|
-
const fileSize = fs_1.default.statSync(outputFile).size;
|
|
61
|
-
spinner.succeed(chalk_1.default.green(`✅ Exported ${records.length} records to ${outputFile}`));
|
|
62
|
-
console.log(chalk_1.default.gray(` Size: ${(0, config_1.formatBytes)(fileSize)}`));
|
|
63
|
-
console.log(chalk_1.default.gray(` Format: ${options.format.toUpperCase()}`));
|
|
64
|
-
await db.close();
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
spinner.fail(chalk_1.default.red('❌ Export failed'));
|
|
68
|
-
console.error(chalk_1.default.red(error.message));
|
|
69
|
-
process.exit(1);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
exports.importCommand = new commander_1.Command().name('db');
|
|
73
|
-
exports.importCommand
|
|
74
|
-
.command('import')
|
|
75
|
-
.description('Import data from file to table')
|
|
76
|
-
.argument('<file>', 'Input file path')
|
|
77
|
-
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
78
|
-
.option('-t, --table <name>', 'Table name')
|
|
79
|
-
.option('-f, --format <format>', 'Input format (json|csv)', 'json')
|
|
80
|
-
.action(async (inputFile, options) => {
|
|
81
|
-
if (!options.table) {
|
|
82
|
-
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
83
|
-
process.exit(1);
|
|
84
|
-
}
|
|
85
|
-
if (!fs_1.default.existsSync(inputFile)) {
|
|
86
|
-
console.error(chalk_1.default.red(`Error: File not found: ${inputFile}`));
|
|
87
|
-
process.exit(1);
|
|
88
|
-
}
|
|
89
|
-
const confirmed = await (0, prompts_1.confirmAction)(chalk_1.default.yellow(`⚠️ This will import data to ${options.table}. Are you sure?`));
|
|
90
|
-
if (!confirmed) {
|
|
91
|
-
console.log(chalk_1.default.yellow('Import cancelled'));
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
const spinner = (0, ora_1.default)(`Importing to ${options.table}...`).start();
|
|
95
|
-
try {
|
|
96
|
-
const db = await (0, config_1.getDatabase)(options.config);
|
|
97
|
-
const content = fs_1.default.readFileSync(inputFile, 'utf-8');
|
|
98
|
-
let data = [];
|
|
99
|
-
if (options.format === 'csv') {
|
|
100
|
-
const lines = content.trim().split('\n');
|
|
101
|
-
const headers = lines[0].split(',');
|
|
102
|
-
data = lines.slice(1).map((line) => {
|
|
103
|
-
const values = line.split(',');
|
|
104
|
-
const row = {};
|
|
105
|
-
headers.forEach((h, i) => {
|
|
106
|
-
row[h] = values[i]?.replace(/"/g, '').trim();
|
|
107
|
-
});
|
|
108
|
-
return row;
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
data = JSON.parse(content);
|
|
113
|
-
}
|
|
114
|
-
const count = await db.bulkInsert(options.table, data);
|
|
115
|
-
spinner.succeed(chalk_1.default.green(`✅ Imported ${count} records to ${options.table}`));
|
|
116
|
-
await db.close();
|
|
117
|
-
}
|
|
118
|
-
catch (error) {
|
|
119
|
-
spinner.fail(chalk_1.default.red('❌ Import failed'));
|
|
120
|
-
console.error(chalk_1.default.red(error.message));
|
|
121
|
-
process.exit(1);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.initCommand = void 0;
|
|
7
|
-
const commander_1 = require("commander");
|
|
8
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
const fs_1 = __importDefault(require("fs"));
|
|
10
|
-
const path_1 = __importDefault(require("path"));
|
|
11
|
-
const package_json_1 = require("../../package.json");
|
|
12
|
-
const prompts_1 = require("../utils/prompts");
|
|
13
|
-
exports.initCommand = new commander_1.Command().name('init');
|
|
14
|
-
exports.initCommand
|
|
15
|
-
.description('Initialize ZeroHelper in your project (Interactive)')
|
|
16
|
-
.action(async () => {
|
|
17
|
-
console.log(chalk_1.default.bold.blue(`\n🚀 Welcome to ZeroHelper v${package_json_1.version} Setup\n`));
|
|
18
|
-
try {
|
|
19
|
-
const answers = await (0, prompts_1.getInitAnswers)();
|
|
20
|
-
const extraPrompts = await (0, prompts_1.getFilePathPrompts)(answers.adapter, answers.enableCache, answers.cacheType);
|
|
21
|
-
const extraAnswers = await inquirer_1.default.prompt(extraPrompts);
|
|
22
|
-
const finalAnswers = { ...answers, ...extraAnswers };
|
|
23
|
-
const configObject = buildConfig(finalAnswers);
|
|
24
|
-
const configTemplate = formatConfigTemplate(finalAnswers, configObject);
|
|
25
|
-
fs_1.default.writeFileSync(path_1.default.join(process.cwd(), 'zero.config.ts'), configTemplate);
|
|
26
|
-
console.log(chalk_1.default.green('\n✅ zero.config.ts created successfully!'));
|
|
27
|
-
console.log(chalk_1.default.gray('\n📝 Usage example:'));
|
|
28
|
-
console.log(chalk_1.default.yellow(`import { database } from '@onurege3467/zerohelper';`));
|
|
29
|
-
console.log(chalk_1.default.yellow(`import { zeroConfig } from './zero.config';`));
|
|
30
|
-
console.log(chalk_1.default.yellow(`const db = database.createDatabase(zeroConfig);`));
|
|
31
|
-
}
|
|
32
|
-
catch (error) {
|
|
33
|
-
console.error(chalk_1.default.red(error.message));
|
|
34
|
-
process.exit(1);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
function buildConfig(answers) {
|
|
38
|
-
if (['json', 'zpack', 'sqlite', 'toon'].includes(answers.adapter)) {
|
|
39
|
-
return JSON.stringify({
|
|
40
|
-
adapter: answers.adapter,
|
|
41
|
-
config: {
|
|
42
|
-
path: answers.filePath,
|
|
43
|
-
...(answers.enableCache && {
|
|
44
|
-
cache: {
|
|
45
|
-
type: answers.cacheType || 'memory',
|
|
46
|
-
...(answers.cacheType === 'memory' && answers.cacheTtl && { ttl: answers.cacheTtl * 1000 })
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
}, null, 2);
|
|
51
|
-
}
|
|
52
|
-
const config = {
|
|
53
|
-
adapter: answers.adapter,
|
|
54
|
-
config: {
|
|
55
|
-
host: answers.host,
|
|
56
|
-
port: answers.port,
|
|
57
|
-
...(answers.username && { username: answers.username }),
|
|
58
|
-
...(answers.password && { password: answers.password }),
|
|
59
|
-
database: answers.database,
|
|
60
|
-
...(answers.adapter === 'mongodb' && { url: `mongodb://${answers.host}:${answers.port}/${answers.database}` })
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
if (answers.enableCache && answers.cacheType) {
|
|
64
|
-
config.config.cache = {
|
|
65
|
-
type: answers.cacheType
|
|
66
|
-
};
|
|
67
|
-
if (answers.cacheType === 'redis') {
|
|
68
|
-
config.config.cache.host = answers.host;
|
|
69
|
-
config.config.cache.port = 6379;
|
|
70
|
-
}
|
|
71
|
-
else if (answers.cacheTtl) {
|
|
72
|
-
config.config.cache.ttl = answers.cacheTtl * 1000;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return JSON.stringify(config, null, 2);
|
|
76
|
-
}
|
|
77
|
-
function formatConfigTemplate(answers, configObject) {
|
|
78
|
-
return `/**
|
|
79
|
-
* ZeroHelper Configuration
|
|
80
|
-
* Generated on ${new Date().toLocaleDateString()}
|
|
81
|
-
*/
|
|
82
|
-
export const zeroConfig = ${configObject};
|
|
83
|
-
`;
|
|
84
|
-
}
|
|
85
|
-
const inquirer_1 = __importDefault(require("inquirer"));
|