UNPKG

@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

101 lines (100 loc) 4.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateHelpers = void 0; const import_generator_1 = require("./template-helpers/import-generator"); const naming_strategy_1 = require("./template-helpers/naming-strategy"); const property_renderer_1 = require("./template-helpers/property-renderer"); const template_utilities_1 = require("./template-helpers/template-utilities"); const type_converter_1 = require("./template-helpers/type-converter"); class TemplateHelpers { constructor(options) { this.options = options; this.namingStrategy = new naming_strategy_1.DefaultNamingStrategy(options.transformClassNameCase || ((s) => s), options.transformFileNameCase || ((s) => s)); this.typeConverter = new type_converter_1.PrismaTypeConverter(); this.propertyRenderer = new property_renderer_1.PropertyRenderer(this, options.decoratorConfigPath); this.templateUtilities = new template_utilities_1.TemplateUtilities(options.decoratorConfigPath); } entityName(name, ignorePrefixAndSufix = false) { return this.namingStrategy.transformClassName(name, ignorePrefixAndSufix ? '' : this.options.entityPrefix, ignorePrefixAndSufix ? '' : this.options.entitySuffix); } connectDtoName(name) { return this.namingStrategy.transformClassName(name, this.options.connectDtoPrefix, this.options.dtoSuffix); } createDtoName(name) { return this.namingStrategy.transformClassName(name, this.options.createDtoPrefix, this.options.dtoSuffix); } updateDtoName(name) { return this.namingStrategy.transformClassName(name, this.options.updateDtoPrefix, this.options.dtoSuffix); } connectDtoFilename(name, withExt = false) { return this.namingStrategy.transformFileName(name, 'connect-', '.dto', withExt); } createDtoFilename(name, withExt = false) { return this.namingStrategy.transformFileName(name, 'create-', '.dto', withExt); } updateDtoFilename(name, withExt = false) { return this.namingStrategy.transformFileName(name, 'update-', '.dto', withExt); } entityFilename(name, withExt = false) { return this.namingStrategy.transformFileName(name, undefined, '.entity', withExt); } fieldType(field, toInputType = false, entityPrefix = '', entitySuffix = '') { return this.typeConverter.fieldType(field, toInputType, entityPrefix, entitySuffix); } fieldsToDtoProps(fields, useInputTypes = false, forceOptional = false, addExposePropertyDecorator = false) { return this.propertyRenderer.fieldsToDtoProps(fields, useInputTypes, forceOptional, addExposePropertyDecorator); } fieldsToEntityProps(fields, entityPrefix = '', entitySuffix = '') { return this.propertyRenderer.fieldsToEntityProps(fields, entityPrefix, entitySuffix); } static echo(input) { return input; } static when(condition, thenTpl, elseTpl = '') { return condition ? thenTpl : elseTpl; } static unless(condition, thenTpl, elseTpl = '') { return !condition ? thenTpl : elseTpl; } static each(arr, fn, joinWith = '') { return arr.map(fn).join(joinWith); } static hasSomeApiPropertyDoc(fields, decoratorConfigPath) { const tempUtils = new template_utilities_1.TemplateUtilities(decoratorConfigPath); return tempUtils.hasSomeApiPropertyDoc(fields); } static hasApiPropertyDoc(field, decoratorConfigPath) { const tempUtils = new template_utilities_1.TemplateUtilities(decoratorConfigPath); return tempUtils.hasApiPropertyDoc(field); } static scalarToTS(scalar, useInputTypes = false) { const converter = new type_converter_1.PrismaTypeConverter(); return converter.scalarToTS(scalar, useInputTypes); } apiExtraModels(names, ignorePrefixAndSufix = false) { const list = names .map((n) => this.entityName(n, ignorePrefixAndSufix)) .join(', '); return `@ApiExtraModels(${list})`; } get transformClassNameCase() { return this.options.transformClassNameCase || ((s) => s); } get transformFileNameCase() { return this.options.transformFileNameCase || ((s) => s); } get config() { const { connectDtoPrefix, createDtoPrefix, updateDtoPrefix, dtoSuffix, entityPrefix, entitySuffix, } = this.options; return { connectDtoPrefix, createDtoPrefix, updateDtoPrefix, dtoSuffix, entityPrefix, entitySuffix, }; } } exports.TemplateHelpers = TemplateHelpers; TemplateHelpers.importStatement = import_generator_1.ImportStatementGenerator.importStatement; TemplateHelpers.importStatements = import_generator_1.ImportStatementGenerator.importStatements;