UNPKG

@iarayan/ch-orm

Version:

A Developer-First ClickHouse ORM with Powerful CLI Tools

226 lines 8.31 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MakeMigrationCommand = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const helpers_1 = require("../../utils/helpers"); /** * Command for creating a new migration file */ class MakeMigrationCommand { /** * Create a new MakeMigrationCommand instance * @param migrationsDir - Path to migrations directory * @param config - Migration configuration * @param templatesDir - Path to templates directory */ constructor(migrationsDir = "./migrations", config = {}, templatesDir = path.join(__dirname, "../templates/migrations")) { this.migrationsDir = migrationsDir; this.config = config; this.templatesDir = templatesDir; } /** * Set the path to the migrations directory * @param dir - Migrations directory path */ setMigrationsDir(dir) { this.migrationsDir = dir; } /** * Set the migration configuration * @param config - Migration configuration */ setConfig(config) { this.config = config; } /** * Set the templates directory * @param dir - Templates directory path */ setTemplatesDir(dir) { this.templatesDir = dir; } /** * Execute the command to create a migration file * @param name - Migration name * @returns Path to the created migration file */ execute(name) { // Ensure migrations directory exists this.ensureMigrationsDir(); // Generate class name from migration name const className = this.generateClassName(name); // Generate table name from migration name const tableName = this.generateTableName(name); // Determine migration type const type = this.determineMigrationType(name); // Determine module system const moduleSystem = this.detectModuleSystem(); // Generate filename for the migration const filename = (0, helpers_1.generateMigrationFilename)(name); // Get template content const templateContent = this.getTemplateContent(moduleSystem, type); // Create migration content from template const content = templateContent .replace("{name}", className) .replace(/\{tableName\}/g, tableName); // Write migration file const filePath = path.join(this.migrationsDir, filename); fs.writeFileSync(filePath, content); return filePath; } /** * Get template content from a file * @param moduleSystem - Module system (esm or commonjs) * @param type - Migration type (create, alter, or drop) * @returns Template content */ getTemplateContent(moduleSystem, type) { const templatePath = path.join(this.templatesDir, moduleSystem, `${type}.txt`); try { return fs.readFileSync(templatePath, "utf-8"); } catch (error) { throw new Error(`Template not found: ${templatePath}`); } } /** * Detect the module system being used in the project * @returns The detected module system */ detectModuleSystem() { var _a, _b, _c; // First check if module system is explicitly configured if (this.config.moduleSystem) { return this.config.moduleSystem; } // Then try to detect from project configuration // Check for package.json in the project root const packageJsonPath = path.join(process.cwd(), "package.json"); if (fs.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); if (packageJson.type === "module") { return "esm"; } } // Check for tsconfig.json in the project root const tsConfigPath = path.join(process.cwd(), "tsconfig.json"); if (fs.existsSync(tsConfigPath)) { const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, "utf-8")); if (((_a = tsConfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.module) === "ESNext" || ((_b = tsConfig.compilerOptions) === null || _b === void 0 ? void 0 : _b.module) === "ES2022" || ((_c = tsConfig.compilerOptions) === null || _c === void 0 ? void 0 : _c.module) === "ES2020") { return "esm"; } } // Default to ESM if no configuration is found return "esm"; } /** * Ensure migrations directory exists */ ensureMigrationsDir() { if (!fs.existsSync(this.migrationsDir)) { fs.mkdirSync(this.migrationsDir, { recursive: true }); } } /** * Generate a class name from migration name * @param name - Migration name * @returns Class name in PascalCase */ generateClassName(name) { // Convert snake_case or kebab-case to PascalCase return name .split(/[_\-]/) .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()) .join(""); } /** * Generate a table name from migration name * @param name - Migration name * @returns Table name */ generateTableName(name) { // Split by common separators and remove common prefixes/suffixes const parts = name .split(/[_\-]/) .filter((part) => ![ "create", "table", "add", "remove", "drop", "alter", "modify", "update", "delete", "to", "from", "in", ].includes(part)); // Return the last meaningful part (usually the table name) return parts[parts.length - 1] || name; } /** * Determine the type of migration from the name * @param name - Migration name * @returns Migration type */ determineMigrationType(name) { const lowerName = name.toLowerCase(); if (lowerName.startsWith("create_") || lowerName.startsWith("create-")) { return "create"; } else if (lowerName.startsWith("drop_") || lowerName.startsWith("drop-")) { return "drop"; } else if (lowerName.startsWith("alter_") || lowerName.startsWith("alter-") || lowerName.startsWith("add_") || lowerName.startsWith("add-") || lowerName.startsWith("remove_") || lowerName.startsWith("remove-") || lowerName.startsWith("modify_") || lowerName.startsWith("modify-")) { return "alter"; } // Default to create if type cannot be determined return "create"; } } exports.MakeMigrationCommand = MakeMigrationCommand; //# sourceMappingURL=MakeMigrationCommand.js.map