@weverson_na/prisma-generator-nestjs-dto
Version:
Advanced Prisma Generator with Smart Merge v2: Creates DTO and Entity classes with AST-based preservation, intelligent import management, and modular architecture for NestJS
45 lines (44 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyRenderer = void 0;
const template_utilities_1 = require("./template-utilities");
class PropertyRenderer {
constructor(typeProvider, decoratorConfigPath) {
this.typeProvider = typeProvider;
this.templateUtilities = new template_utilities_1.TemplateUtilities(decoratorConfigPath);
}
addDecorator(field, forEntity = false) {
return forEntity
? this.templateUtilities.buildEntityDecorator(field)
: this.templateUtilities.buildDtoDecorator(field);
}
fieldToDtoProp(field, useInputTypes = false, forceOptional = false, addExposePropertyDecorator = false) {
const optionalMark = this.templateUtilities.unless(forceOptional ? false : field.isRequired, '?');
const decorator = this.addDecorator(field);
const decoratorWithNewline = decorator ? decorator + '\n' : '';
return (` // @generated from prisma schema\n` +
decoratorWithNewline +
(addExposePropertyDecorator ? ` @Expose()\n` : '') +
` ${field.name}${optionalMark}: ${this.getFieldType(field, useInputTypes)};`);
}
fieldToEntityProp(field, entityPrefix = '', entitySuffix = '') {
const opt = this.templateUtilities.unless(field.isRequired, '?');
const nullable = this.templateUtilities.when(field.isNullable, ' | null');
const decorator = this.addDecorator(field, true);
const decoratorWithNewline = decorator ? decorator + '\n' : '';
const type = this.getFieldType(field, false, entityPrefix, entitySuffix);
return (` // @generated from prisma schema\n` +
decoratorWithNewline +
` ${field.name}${opt}: ${type}${nullable};`);
}
fieldsToDtoProps(fields, useInputTypes = false, forceOptional = false, addExposePropertyDecorator = false) {
return this.templateUtilities.each(fields, (f) => this.fieldToDtoProp(f, useInputTypes, forceOptional, addExposePropertyDecorator), '\n');
}
fieldsToEntityProps(fields, entityPrefix = '', entitySuffix = '') {
return this.templateUtilities.each(fields, (f) => this.fieldToEntityProp(f, entityPrefix, entitySuffix), '\n');
}
getFieldType(field, toInputType = false, entityPrefix = '', entitySuffix = '') {
return this.typeProvider.fieldType(field, toInputType, entityPrefix, entitySuffix);
}
}
exports.PropertyRenderer = PropertyRenderer;