UNPKG

nestjs-reverse-engineering

Version:

A powerful TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations

176 lines 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NamingUtils = void 0; /* eslint-disable prettier/prettier */ const constants_1 = require("../types/constants"); class NamingUtils { /** * Convert snake_case to PascalCase */ static toPascalCase(str) { return str .split('_') .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(''); } /** * Convert snake_case to camelCase */ static toCamelCase(str) { const pascalCase = this.toPascalCase(str); return pascalCase.charAt(0).toLowerCase() + pascalCase.slice(1); } /** * Convert PascalCase or camelCase to snake_case */ static toSnakeCase(str) { return str .replace(/([A-Z])/g, '_$1') .toLowerCase() .replace(/^_/, ''); } /** * Convert PascalCase, camelCase, or snake_case to kebab-case */ static toKebabCase(str) { return str .replace(/([A-Z])/g, '-$1') .replace(/_/g, '-') .toLowerCase() .replace(/^-/, ''); } /** * Convert kebab-case to PascalCase */ static kebabToPascalCase(str) { return str .split('-') .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(''); } /** * Convert kebab-case to camelCase */ static kebabToCamelCase(str) { const pascalCase = this.kebabToPascalCase(str); return pascalCase.charAt(0).toLowerCase() + pascalCase.slice(1); } /** * Sanitize table/column names for TypeScript */ static sanitizeName(name) { // Remove special characters and spaces let sanitized = name.replace(/[^a-zA-Z0-9_]/g, '_'); // Remove leading numbers if (/^\d/.test(sanitized)) { sanitized = '_' + sanitized; } // Handle reserved keywords if (this.isReservedKeyword(sanitized)) { sanitized = sanitized + '_'; } return sanitized; } /** * Check if a name is a reserved keyword */ static isReservedKeyword(name) { const lowerName = name.toLowerCase(); return constants_1.RESERVED_KEYWORDS.includes(lowerName) || constants_1.TYPEORM_RESERVED_WORDS.includes(lowerName); } /** * Generate a safe property name */ static toSafePropertyName(columnName, convention = 'camelCase') { const sanitized = this.sanitizeName(columnName); switch (convention) { case 'PascalCase': return this.toPascalCase(sanitized); case 'camelCase': return this.toCamelCase(sanitized); case 'snake_case': return sanitized; default: return this.toCamelCase(sanitized); } } /** * Generate a safe class name from table name */ static toSafeClassName(tableName) { const sanitized = this.sanitizeName(tableName); let className = this.toPascalCase(sanitized); // Ensure it ends with a descriptive suffix if it's too generic if (className.length < 3) { className = className + 'Entity'; } return className; } /** * Generate a safe enum name */ static toSafeEnumName(value) { let sanitized = value.replace(/[^a-zA-Z0-9_]/g, '_').toUpperCase(); // Remove leading numbers if (/^\d/.test(sanitized)) { sanitized = '_' + sanitized; } return sanitized; } /** * Extract enum values from PostgreSQL enum definition */ static parseEnumValues(enumDefinition) { const match = enumDefinition.match(/enum\((.*)\)/i); if (!match) return []; return match[1] .split(',') .map(value => value.trim().replace(/['"]/g, '')) .filter(value => value.length > 0); } /** * Generate file name from table name */ static toFileName(tableName) { return this.toSnakeCase(this.sanitizeName(tableName)); } /** * Handle composite names (tables with spaces, special chars) */ static handleCompositeName(name) { const hasSpecialChars = /[^a-zA-Z0-9_]/.test(name); const startsWithNumber = /^\d/.test(name); const isReserved = this.isReservedKeyword(name); return { original: name, safe: this.sanitizeName(name), quoted: hasSpecialChars || startsWithNumber || isReserved }; } /** * Generate relationship property name */ static toRelationshipName(tableName, isCollection = false) { const baseName = this.toCamelCase(this.sanitizeName(tableName)); return isCollection ? this.pluralize(baseName) : baseName; } /** * Simple pluralization (can be enhanced with a library like 'pluralize') */ static pluralize(word) { // Simple pluralization rules if (word.endsWith('y')) { return word.slice(0, -1) + 'ies'; } else if (word.endsWith('s') || word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) { return word + 'es'; } else { return word + 's'; } } } exports.NamingUtils = NamingUtils; //# sourceMappingURL=naming-utils.js.map