@onurege3467/zerohelper 10.2.5 → 10.2.6
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 +432 -54
- package/dist/bin/commands/cache.d.ts +2 -0
- package/dist/bin/commands/cache.js +92 -0
- package/dist/bin/commands/db-backup.d.ts +3 -0
- package/dist/bin/commands/db-backup.js +118 -0
- package/dist/bin/commands/db.d.ts +2 -0
- package/dist/bin/commands/db.js +334 -0
- package/dist/bin/commands/import-export.d.ts +3 -0
- package/dist/bin/commands/import-export.js +123 -0
- package/dist/bin/commands/init.d.ts +2 -0
- package/dist/bin/commands/init.js +85 -0
- package/dist/bin/commands/migrate.d.ts +3 -0
- package/dist/bin/commands/migrate.js +167 -0
- package/dist/bin/commands/repl.d.ts +2 -0
- package/dist/bin/commands/repl.js +96 -0
- package/dist/bin/commands/seed.d.ts +2 -0
- package/dist/bin/commands/seed.js +76 -0
- package/dist/bin/commands/zpack.d.ts +2 -0
- package/dist/bin/commands/zpack.js +36 -0
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +28 -0
- package/dist/bin/types.d.ts +22 -0
- package/dist/bin/types.js +2 -0
- package/dist/bin/utils/config.d.ts +3 -0
- package/dist/bin/utils/config.js +78 -0
- package/dist/bin/utils/prompts.d.ts +3 -0
- package/dist/bin/utils/prompts.js +115 -0
- package/dist/bin/zero.js +789 -81
- package/dist/migrations/1767521950635_test_migration.d.ts +3 -0
- package/dist/migrations/1767521950635_test_migration.js +11 -0
- package/dist/migrations/1767522158826_create_users_table.d.ts +2 -0
- package/dist/migrations/1767522158826_create_users_table.js +11 -0
- package/dist/package.json +79 -0
- package/dist/zero.config.d.ts +10 -0
- package/dist/zero.config.js +13 -0
- package/package.json +2 -2
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
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"));
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.makeMigrationCommand = exports.migrateCommand = void 0;
|
|
40
|
+
const commander_1 = require("commander");
|
|
41
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
42
|
+
const ora_1 = __importDefault(require("ora"));
|
|
43
|
+
const fs_1 = __importDefault(require("fs"));
|
|
44
|
+
const path_1 = __importDefault(require("path"));
|
|
45
|
+
const database = __importStar(require("../../database"));
|
|
46
|
+
const config_1 = require("../utils/config");
|
|
47
|
+
const prompts_1 = require("../utils/prompts");
|
|
48
|
+
exports.migrateCommand = new commander_1.Command().name('migration');
|
|
49
|
+
exports.migrateCommand
|
|
50
|
+
.command('migrate')
|
|
51
|
+
.description('Run pending migrations')
|
|
52
|
+
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
53
|
+
.option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations')
|
|
54
|
+
.action(async (options) => {
|
|
55
|
+
const spinner = (0, ora_1.default)('Loading migrations...').start();
|
|
56
|
+
try {
|
|
57
|
+
const db = await (0, config_1.getDatabase)(options.config);
|
|
58
|
+
const migration = new database.MigrationManager(db, {
|
|
59
|
+
migrationsDir: options.migrationsDir
|
|
60
|
+
});
|
|
61
|
+
const pending = await migration.getPendingMigrations();
|
|
62
|
+
if (pending.length === 0) {
|
|
63
|
+
spinner.succeed(chalk_1.default.green('✅ No pending migrations'));
|
|
64
|
+
await db.close();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
spinner.text = `Running ${pending.length} migration(s)...`;
|
|
68
|
+
for (const m of pending) {
|
|
69
|
+
await migration.runMigration(m, 'up');
|
|
70
|
+
spinner.text = `✅ ${m.name}`;
|
|
71
|
+
}
|
|
72
|
+
spinner.succeed(chalk_1.default.green(`✅ ${pending.length} migration(s) executed successfully`));
|
|
73
|
+
await db.close();
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
spinner.fail(chalk_1.default.red('❌ Migration failed'));
|
|
77
|
+
console.error(chalk_1.default.red(error.message));
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
exports.migrateCommand
|
|
82
|
+
.command('rollback')
|
|
83
|
+
.description('Rollback the last migration(s)')
|
|
84
|
+
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
85
|
+
.option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations')
|
|
86
|
+
.option('-s, --steps <number>', 'Number of migrations to rollback', '1')
|
|
87
|
+
.action(async (options) => {
|
|
88
|
+
const steps = parseInt(options.steps);
|
|
89
|
+
const confirmed = await (0, prompts_1.confirmAction)(chalk_1.default.yellow(`⚠️ Are you sure you want to rollback ${steps} migration(s)?`));
|
|
90
|
+
if (!confirmed) {
|
|
91
|
+
console.log(chalk_1.default.yellow('Rollback cancelled'));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const spinner = (0, ora_1.default)(`Rolling back ${steps} migration(s)...`).start();
|
|
95
|
+
try {
|
|
96
|
+
const db = await (0, config_1.getDatabase)(options.config);
|
|
97
|
+
const migration = new database.MigrationManager(db, {
|
|
98
|
+
migrationsDir: options.migrationsDir
|
|
99
|
+
});
|
|
100
|
+
await migration.rollback(steps);
|
|
101
|
+
spinner.succeed(chalk_1.default.green(`✅ Rolled back ${steps} migration(s)`));
|
|
102
|
+
await db.close();
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
spinner.fail(chalk_1.default.red('❌ Rollback failed'));
|
|
106
|
+
console.error(chalk_1.default.red(error.message));
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
exports.migrateCommand
|
|
111
|
+
.command('status')
|
|
112
|
+
.description('Show migration status')
|
|
113
|
+
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
114
|
+
.option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations')
|
|
115
|
+
.action(async (options) => {
|
|
116
|
+
try {
|
|
117
|
+
const db = await (0, config_1.getDatabase)(options.config);
|
|
118
|
+
const migration = new database.MigrationManager(db, {
|
|
119
|
+
migrationsDir: options.migrationsDir
|
|
120
|
+
});
|
|
121
|
+
const all = migration.getMigrationFiles();
|
|
122
|
+
const executed = await migration.getExecutedMigrations();
|
|
123
|
+
console.log(chalk_1.default.bold('\n📋 Migration Status'));
|
|
124
|
+
console.log(chalk_1.default.gray('─'.repeat(50)));
|
|
125
|
+
if (all.length === 0) {
|
|
126
|
+
console.log(chalk_1.default.yellow('No migrations found'));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
all.forEach(m => {
|
|
130
|
+
const isExecuted = executed.includes(m.name);
|
|
131
|
+
const status = isExecuted ? chalk_1.default.green('✅ Done') : chalk_1.default.yellow('⏳ Pending');
|
|
132
|
+
console.log(` ${status} ${chalk_1.default.white(m.name)}`);
|
|
133
|
+
});
|
|
134
|
+
console.log(chalk_1.default.gray('\n' + '─'.repeat(50)));
|
|
135
|
+
console.log(` Total: ${all.length} | Executed: ${chalk_1.default.green(executed.length)} | Pending: ${chalk_1.default.yellow(all.length - executed.length)}`);
|
|
136
|
+
}
|
|
137
|
+
await db.close();
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.error(chalk_1.default.red(`Error: ${error.message}`));
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
exports.makeMigrationCommand = new commander_1.Command().name('make');
|
|
145
|
+
exports.makeMigrationCommand
|
|
146
|
+
.command('migration')
|
|
147
|
+
.description('Generate a new migration template')
|
|
148
|
+
.argument('<name>', 'Name of the migration')
|
|
149
|
+
.option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations')
|
|
150
|
+
.action((name, options) => {
|
|
151
|
+
const timestamp = Date.now();
|
|
152
|
+
const fileName = `${timestamp}_${name}.ts`;
|
|
153
|
+
const migrationsDir = path_1.default.join(process.cwd(), options.migrationsDir);
|
|
154
|
+
if (!fs_1.default.existsSync(migrationsDir)) {
|
|
155
|
+
fs_1.default.mkdirSync(migrationsDir, { recursive: true });
|
|
156
|
+
}
|
|
157
|
+
const template = `export const up = async (db: any) => {
|
|
158
|
+
// Write your migration logic here
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export const down = async (db: any) => {
|
|
162
|
+
// Write your rollback logic here
|
|
163
|
+
};
|
|
164
|
+
`;
|
|
165
|
+
fs_1.default.writeFileSync(path_1.default.join(migrationsDir, fileName), template);
|
|
166
|
+
console.log(chalk_1.default.green(`\n✅ Migration created: ./${options.migrationsDir}/${fileName}`));
|
|
167
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
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.replCommand = void 0;
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
const config_1 = require("../utils/config");
|
|
10
|
+
exports.replCommand = new commander_1.Command().name('repl');
|
|
11
|
+
exports.replCommand
|
|
12
|
+
.description('Interactive ZeroHelper REPL mode')
|
|
13
|
+
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
14
|
+
.action(async (options) => {
|
|
15
|
+
console.log(chalk_1.default.bold.cyan('\n🔧 ZeroHelper REPL Mode\n'));
|
|
16
|
+
console.log(chalk_1.default.gray('Type .exit to quit, .help for commands\n'));
|
|
17
|
+
try {
|
|
18
|
+
const db = await (0, config_1.getDatabase)(options.config);
|
|
19
|
+
const readline = require('readline');
|
|
20
|
+
const rl = readline.createInterface({
|
|
21
|
+
input: process.stdin,
|
|
22
|
+
output: process.stdout,
|
|
23
|
+
prompt: chalk_1.default.blue('zero> ')
|
|
24
|
+
});
|
|
25
|
+
rl.prompt();
|
|
26
|
+
rl.on('line', async (line) => {
|
|
27
|
+
const cmd = line.trim();
|
|
28
|
+
if (cmd === '.exit') {
|
|
29
|
+
await db.close();
|
|
30
|
+
rl.close();
|
|
31
|
+
console.log(chalk_1.default.green('\n👋 Goodbye!\n'));
|
|
32
|
+
process.exit(0);
|
|
33
|
+
}
|
|
34
|
+
else if (cmd === '.help') {
|
|
35
|
+
console.log(chalk_1.default.bold('\nAvailable REPL Commands:'));
|
|
36
|
+
console.log(' .exit Exit REPL');
|
|
37
|
+
console.log(' .help Show this help');
|
|
38
|
+
console.log(' .stats Show database stats');
|
|
39
|
+
console.log(' .metrics Show performance metrics');
|
|
40
|
+
console.log(' select <table> Select all from table');
|
|
41
|
+
console.log(' count <table> Count records in table');
|
|
42
|
+
console.log(' clear Clear screen\n');
|
|
43
|
+
}
|
|
44
|
+
else if (cmd === '.stats') {
|
|
45
|
+
const metrics = db.getMetrics();
|
|
46
|
+
console.log(chalk_1.default.bold('\n📊 Database Stats:'));
|
|
47
|
+
console.log(` Operations: ${metrics.database.count || 0}`);
|
|
48
|
+
console.log(` Avg Latency: ${(metrics.database.averageDuration || 0).toFixed(2)}ms\n`);
|
|
49
|
+
}
|
|
50
|
+
else if (cmd === '.metrics') {
|
|
51
|
+
const metrics = db.getMetrics();
|
|
52
|
+
console.log(chalk_1.default.bold('\n📊 Full Metrics:'));
|
|
53
|
+
console.log(JSON.stringify(metrics, null, 2));
|
|
54
|
+
console.log('');
|
|
55
|
+
}
|
|
56
|
+
else if (cmd === '.clear') {
|
|
57
|
+
console.clear();
|
|
58
|
+
}
|
|
59
|
+
else if (cmd.startsWith('select ')) {
|
|
60
|
+
const table = cmd.replace('select ', '').trim();
|
|
61
|
+
try {
|
|
62
|
+
const records = await db.select(table);
|
|
63
|
+
console.log(chalk_1.default.bold(`\nFound ${records.length} records in ${table}:`));
|
|
64
|
+
console.log(JSON.stringify(records, null, 2));
|
|
65
|
+
console.log('');
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
console.error(chalk_1.default.red(`Error: ${err.message}\n`));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (cmd.startsWith('count ')) {
|
|
72
|
+
const table = cmd.replace('count ', '').trim();
|
|
73
|
+
try {
|
|
74
|
+
const records = await db.select(table);
|
|
75
|
+
console.log(chalk_1.default.bold(`\n${table}: ${records.length} records\n`));
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
console.error(chalk_1.default.red(`Error: ${err.message}\n`));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (cmd.length > 0) {
|
|
82
|
+
console.log(chalk_1.default.yellow('Unknown command. Type .help for available commands\n'));
|
|
83
|
+
}
|
|
84
|
+
rl.prompt();
|
|
85
|
+
});
|
|
86
|
+
rl.on('close', () => {
|
|
87
|
+
db.close();
|
|
88
|
+
console.log(chalk_1.default.green('\n👋 Goodbye!\n'));
|
|
89
|
+
process.exit(0);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.error(chalk_1.default.red(`Error: ${error.message}`));
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.seedCommand = void 0;
|
|
40
|
+
const commander_1 = require("commander");
|
|
41
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
42
|
+
const ora_1 = __importDefault(require("ora"));
|
|
43
|
+
const database = __importStar(require("../../database"));
|
|
44
|
+
const config_1 = require("../utils/config");
|
|
45
|
+
exports.seedCommand = new commander_1.Command().name('db');
|
|
46
|
+
exports.seedCommand
|
|
47
|
+
.command('seed')
|
|
48
|
+
.description('Seed table with mock data')
|
|
49
|
+
.option('-c, --config <path>', 'Path to config file', 'zero.config.ts')
|
|
50
|
+
.option('-t, --table <name>', 'Table name')
|
|
51
|
+
.option('-n, --count <number>', 'Number of records', '10')
|
|
52
|
+
.action(async (options) => {
|
|
53
|
+
if (!options.table) {
|
|
54
|
+
console.error(chalk_1.default.red('Error: --table option is required'));
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
const spinner = (0, ora_1.default)(`Seeding ${options.table}...`).start();
|
|
58
|
+
try {
|
|
59
|
+
const db = await (0, config_1.getDatabase)(options.config);
|
|
60
|
+
const seeder = new database.DataSeeder(db);
|
|
61
|
+
const schema = {};
|
|
62
|
+
const fieldTypes = ['string', 'number', 'email', 'boolean', 'date'];
|
|
63
|
+
for (let i = 0; i < 3; i++) {
|
|
64
|
+
const fieldType = fieldTypes[Math.floor(Math.random() * fieldTypes.length)];
|
|
65
|
+
schema[`field_${i + 1}`] = { type: fieldType };
|
|
66
|
+
}
|
|
67
|
+
const count = await seeder.seed(options.table, parseInt(options.count), schema);
|
|
68
|
+
spinner.succeed(chalk_1.default.green(`✅ Seeded ${count} records into ${options.table}`));
|
|
69
|
+
await db.close();
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
spinner.fail(chalk_1.default.red('❌ Seeding failed'));
|
|
73
|
+
console.error(chalk_1.default.red(error.message));
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
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.zpackCommand = 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 config_1 = require("../utils/config");
|
|
12
|
+
const config_2 = require("../utils/config");
|
|
13
|
+
exports.zpackCommand = new commander_1.Command().name('zpack');
|
|
14
|
+
exports.zpackCommand
|
|
15
|
+
.command('vacuum')
|
|
16
|
+
.description('Compact a ZPack binary file to save disk space')
|
|
17
|
+
.argument('<file>', 'ZPack file path')
|
|
18
|
+
.action(async (file) => {
|
|
19
|
+
const spinner = (0, ora_1.default)(`Vacuuming ${file}...`).start();
|
|
20
|
+
const startSize = fs_1.default.existsSync(file) ? fs_1.default.statSync(file).size : 0;
|
|
21
|
+
try {
|
|
22
|
+
const db = await (0, config_1.getDatabase)('zero.config.ts');
|
|
23
|
+
await db.vacuum();
|
|
24
|
+
await db.close();
|
|
25
|
+
const endSize = fs_1.default.statSync(file).size;
|
|
26
|
+
const reduction = startSize > 0 ? ((1 - endSize / startSize) * 100) : 0;
|
|
27
|
+
spinner.succeed(chalk_1.default.green(`✅ Vacuum complete for ${file}`));
|
|
28
|
+
console.log(chalk_1.default.gray(` Original Size: ${(0, config_2.formatBytes)(startSize)}`));
|
|
29
|
+
console.log(chalk_1.default.gray(` New Size: ${(0, config_2.formatBytes)(endSize)}`));
|
|
30
|
+
console.log(chalk_1.default.bold.blue(` Efficiency: ${reduction.toFixed(1)}% reduction`));
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
spinner.fail(chalk_1.default.red(`❌ Vacuum failed: ${error.message}`));
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
const package_json_1 = require("../package.json");
|
|
10
|
+
const init_1 = require("./commands/init");
|
|
11
|
+
const db_1 = require("./commands/db");
|
|
12
|
+
const cache_1 = require("./commands/cache");
|
|
13
|
+
const migrate_1 = require("./commands/migrate");
|
|
14
|
+
const zpack_1 = require("./commands/zpack");
|
|
15
|
+
const repl_1 = require("./commands/repl");
|
|
16
|
+
const program = new commander_1.Command();
|
|
17
|
+
program
|
|
18
|
+
.name('zero')
|
|
19
|
+
.description(chalk_1.default.cyan('ZeroHelper CLI - Elite Database Management Tool'))
|
|
20
|
+
.version(package_json_1.version);
|
|
21
|
+
program.addCommand(init_1.initCommand);
|
|
22
|
+
program.addCommand(db_1.dbCommand);
|
|
23
|
+
program.addCommand(cache_1.cacheCommand);
|
|
24
|
+
program.addCommand(migrate_1.migrateCommand);
|
|
25
|
+
program.addCommand(migrate_1.makeMigrationCommand);
|
|
26
|
+
program.addCommand(zpack_1.zpackCommand);
|
|
27
|
+
program.addCommand(repl_1.replCommand);
|
|
28
|
+
program.parse();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface CLIOptions {
|
|
2
|
+
config: string;
|
|
3
|
+
}
|
|
4
|
+
export interface DBOptions extends CLIOptions {
|
|
5
|
+
table?: string;
|
|
6
|
+
count?: number;
|
|
7
|
+
format?: string;
|
|
8
|
+
output?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface MigrationOptions extends CLIOptions {
|
|
11
|
+
migrationsDir: string;
|
|
12
|
+
steps?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ImportOptions extends CLIOptions {
|
|
15
|
+
table: string;
|
|
16
|
+
format: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ExportOptions extends CLIOptions {
|
|
19
|
+
table: string;
|
|
20
|
+
format: string;
|
|
21
|
+
output: string;
|
|
22
|
+
}
|