From ed2388d7f679800b981d24a4444d8c1432d66ff8 Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Thu, 20 Nov 2025 07:55:45 +0100 Subject: [PATCH] externelize entity template to tpl file --- src/services/entity-generator.service.ts | 64 ++++++++++-------------- src/templates/nestjs/entity.ts.tpl | 9 ++++ 2 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 src/templates/nestjs/entity.ts.tpl diff --git a/src/services/entity-generator.service.ts b/src/services/entity-generator.service.ts index cc7ac6e..7ff29b2 100644 --- a/src/services/entity-generator.service.ts +++ b/src/services/entity-generator.service.ts @@ -2,16 +2,19 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from './config.service'; import { DataSource, DataSourceOptions, TableColumn } from 'typeorm'; +import { ModuleUpdaterService } from './module-updater.service'; +import { TemplateService } from './template.service'; // <-- 1. IMPORT TemplateService import * as path from 'path'; import * as fs from 'fs'; import { promises as fsPromises } from 'fs'; -import { ModuleUpdaterService } from './module-updater.service'; @Injectable() export class EntityGeneratorService { - constructor(private readonly configService: ConfigService, - private readonly moduleUpdaterService: ModuleUpdaterService) {} - + constructor( + private readonly configService: ConfigService, + private readonly moduleUpdaterService: ModuleUpdaterService, + private readonly templateService: TemplateService, // <-- 2. INJECT TemplateService + ) {} public async generate(tableName: string): Promise { console.log(`Generating entity for table: ${tableName}...`); @@ -35,14 +38,28 @@ export class EntityGeneratorService { const columns = table.columns; console.log(`Found ${columns.length} columns in table "${tableName}".`); - const entityContent = this.createEntityTemplate(tableName, columns); - const singularName = this.toSingular(tableName); - - // --- THIS IS THE FIX --- - // Define className here so it can be used below. const className = this.toPascalCase(singularName); - // --- END OF FIX --- + + const props = columns + .map((col) => { + const typeormDecorator = this.getDecorator(col); + const validationDecorators = this.getValidationDecorators(col); + const tsType = this.mapDbToTsType(col.type); + const nullable = col.isNullable ? ' | null' : ''; + const defaultValue = col.default ? ` = ${this.formatDefaultValue(col)}` : ''; + const allDecorators = [typeormDecorator, validationDecorators].filter(Boolean).join('\n '); + return `\n ${allDecorators}\n ${col.name}: ${tsType}${nullable}${defaultValue};\n`; + }) + .join(''); + + // 4. Render the external template + const entityContent = this.templateService.render('nestjs/entity.ts.tpl', { + tableName: tableName, + className: className, + props: props, + }); + // --- END OF REFACTOR --- const entityFileName = `${singularName}.entity.ts`; const serverRoot = path.resolve(process.cwd(), config.server.path); @@ -53,11 +70,9 @@ export class EntityGeneratorService { fs.writeFileSync(outputPath, entityContent); console.log(`✅ Entity created successfully at: ${outputPath}`); - // Now the call will work correctly await this.moduleUpdaterService.addEntityToTypeOrm(className, outputPath); return columns; - } catch (error) { console.error('❌ An error occurred:', error.message); return null; @@ -69,31 +84,6 @@ export class EntityGeneratorService { } } - private createEntityTemplate(tableName: string, columns: TableColumn[]): string { - const className = this.toPascalCase(this.toSingular(tableName)); - - const props = columns - .map((col) => { - const typeormDecorator = this.getDecorator(col); - const validationDecorators = this.getValidationDecorators(col); - const tsType = this.mapDbToTsType(col.type); - const nullable = col.isNullable ? ' | null' : ''; - const defaultValue = col.default ? ` = ${this.formatDefaultValue(col)}` : ''; - - const allDecorators = [typeormDecorator, validationDecorators].filter(Boolean).join('\n '); - - return `\n ${allDecorators}\n ${col.name}: ${tsType}${nullable}${defaultValue};\n`; - }) - .join(''); - - return `import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; -import { IsString, IsNumber, IsBoolean, IsDate, IsOptional } from 'class-validator'; - -@Entity({ name: '${tableName}' }) -export class ${className} {${props} -} -`; - } private getValidationDecorators(column: TableColumn): string { if (column.isPrimary && column.isGenerated) { diff --git a/src/templates/nestjs/entity.ts.tpl b/src/templates/nestjs/entity.ts.tpl new file mode 100644 index 0000000..a1e7f7e --- /dev/null +++ b/src/templates/nestjs/entity.ts.tpl @@ -0,0 +1,9 @@ +// dvbooking-cli/src/templates/nestjs/entity.ts.tpl + +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; +import { IsString, IsNumber, IsBoolean, IsDate, IsOptional } from 'class-validator'; + +@Entity({ name: '{{tableName}}' }) +export class {{className}} { +{{props}} +} \ No newline at end of file