UNPKG

@onurege3467/zerohelper

Version:

ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.

168 lines (166 loc) 7.34 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeMigrationCommand = exports.migrateCommand = void 0; const commander_1 = require("commander"); const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const database = __importStar(require("../../database")); const config_1 = require("../utils/config"); const prompts_1 = require("../utils/prompts"); exports.migrateCommand = new commander_1.Command().name('migration'); exports.migrateCommand .command('migrate') .description('Run pending migrations') .option('-c, --config <path>', 'Path to config file', 'zero.config.ts') .option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations') .action(async (options) => { const spinner = (0, ora_1.default)('Loading migrations...').start(); try { const db = await (0, config_1.getDatabase)(options.config); const migration = new database.MigrationManager(db, { migrationsDir: options.migrationsDir }); const pending = await migration.getPendingMigrations(); if (pending.length === 0) { spinner.succeed(chalk_1.default.green('✅ No pending migrations')); await db.close(); return; } spinner.text = `Running ${pending.length} migration(s)...`; for (const m of pending) { await migration.runMigration(m, 'up'); spinner.text = `✅ ${m.name}`; } spinner.succeed(chalk_1.default.green(`✅ ${pending.length} migration(s) executed successfully`)); await db.close(); } catch (error) { spinner.fail(chalk_1.default.red('❌ Migration failed')); console.error(chalk_1.default.red(error.message)); process.exit(1); } }); exports.migrateCommand .command('rollback') .description('Rollback the last migration(s)') .option('-c, --config <path>', 'Path to config file', 'zero.config.ts') .option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations') .option('-s, --steps <number>', 'Number of migrations to rollback', '1') .action(async (options) => { const steps = parseInt(options.steps); const confirmed = await (0, prompts_1.confirmAction)(chalk_1.default.yellow(`⚠️ Are you sure you want to rollback ${steps} migration(s)?`)); if (!confirmed) { console.log(chalk_1.default.yellow('Rollback cancelled')); return; } const spinner = (0, ora_1.default)(`Rolling back ${steps} migration(s)...`).start(); try { const db = await (0, config_1.getDatabase)(options.config); const migration = new database.MigrationManager(db, { migrationsDir: options.migrationsDir }); await migration.rollback(steps); spinner.succeed(chalk_1.default.green(`✅ Rolled back ${steps} migration(s)`)); await db.close(); } catch (error) { spinner.fail(chalk_1.default.red('❌ Rollback failed')); console.error(chalk_1.default.red(error.message)); process.exit(1); } }); exports.migrateCommand .command('status') .description('Show migration status') .option('-c, --config <path>', 'Path to config file', 'zero.config.ts') .option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations') .action(async (options) => { try { const db = await (0, config_1.getDatabase)(options.config); const migration = new database.MigrationManager(db, { migrationsDir: options.migrationsDir }); const all = migration.getMigrationFiles(); const executed = await migration.getExecutedMigrations(); console.log(chalk_1.default.bold('\n📋 Migration Status')); console.log(chalk_1.default.gray('─'.repeat(50))); if (all.length === 0) { console.log(chalk_1.default.yellow('No migrations found')); } else { all.forEach(m => { const isExecuted = executed.includes(m.name); const status = isExecuted ? chalk_1.default.green('✅ Done') : chalk_1.default.yellow('⏳ Pending'); console.log(` ${status} ${chalk_1.default.white(m.name)}`); }); console.log(chalk_1.default.gray('\n' + '─'.repeat(50))); console.log(` Total: ${all.length} | Executed: ${chalk_1.default.green(executed.length)} | Pending: ${chalk_1.default.yellow(all.length - executed.length)}`); } await db.close(); } catch (error) { console.error(chalk_1.default.red(`Error: ${error.message}`)); process.exit(1); } }); exports.makeMigrationCommand = new commander_1.Command().name('make'); exports.makeMigrationCommand .command('migration') .description('Generate a new migration template') .argument('<name>', 'Name of the migration') .option('-d, --migrations-dir <path>', 'Migrations directory path', './migrations') .action((name, options) => { const timestamp = Date.now(); const fileName = `${timestamp}_${name}.ts`; const migrationsDir = path_1.default.join(process.cwd(), options.migrationsDir); if (!fs_1.default.existsSync(migrationsDir)) { fs_1.default.mkdirSync(migrationsDir, { recursive: true }); } const template = `export const up = async (db: any) => { // Write your migration logic here }; export const down = async (db: any) => { // Write your rollback logic here }; `; fs_1.default.writeFileSync(path_1.default.join(migrationsDir, fileName), template); console.log(chalk_1.default.green(`\n✅ Migration created: ./${options.migrationsDir}/${fileName}`)); });