@arianaprojects/prisma-nestjs-crud-generator
Version:
code generator from Prisma schema
186 lines • 7.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const templates_1 = require("../templates");
const util_1 = require("../util");
class Types {
constructor(parent) {
this.parent = parent;
this.generateDTO();
this.generateEntity();
}
get entityDTO() {
return this.dto;
}
get entityGetter() {
return this.entity;
}
generateDTO() {
let res = [];
res.push(this.dtoImport());
res.push(this.classGenerator(`Create${this.parent.namePascal}Dto`, this.parent.createFields, true, false, true, false));
res.push(this.classGenerator(`Update${this.parent.namePascal}Dto`, this.parent.updateFields, true, false, true, true));
res.push(this.classGenerator(`Find${this.parent.namePascal}Dto`, this.parent.findFields, true, false, true, true));
res.push(this.classGenerator(`Connect${this.parent.namePascal}Dto`, this.parent.connectFields, true, false, true, false));
res.push(this.classGenerator(`Access${this.parent.namePascal}Dto`, this.parent.accessField, false, false, false, false));
res.push(this.classGenerator(`Deleted${this.parent.namePascal}Dto`, this.parent.deletedField, false, false, false, false));
res.push(this.classGenerator(`Pseudonymisation${this.parent.namePascal}Dto`, this.parent.updateFieldsAdmin, false, false, false, true));
res.push(this.classGenerator(`Create${this.parent.namePascal}AdminDto`, this.parent.createFieldsAdmin, true, false, true, false));
res.push(this.classGenerator(`Update${this.parent.namePascal}AdminDto`, this.parent.updateFieldsAdmin, true, false, true, true));
res.push(this.classGenerator(`Find${this.parent.namePascal}AdminDto`, this.parent.findFieldsAdmin, true, false, true, true));
res.push(this.classGenerator(`Connect${this.parent.namePascal}AdminDto`, this.parent.connectFieldsAdmin, true, false, true, false));
this.dto = res.join('\n\n');
}
generateEntity() {
let res = [];
res.push(this.entityImport());
res.push(this.classGenerator(this.parent.namePascal, this.parent.entityFields, true, true, true, false, true));
this.entity = res.join('\n');
}
dtoImport() {
let res = [
`import { Exclude, Expose, Type } from 'class-transformer';`,
`import {
IsArray,
IsBoolean,
IsDate,
IsDecimal,
IsEnum,
IsNotEmpty,
IsNumber,
IsObject,
IsOptional,
IsJSON,IsInt,
ValidateNested,IsString
} from 'class-validator';`,
"import { Decimal } from '@prisma/client/runtime';",
"import { ApiProperty } from '@nestjs/swagger';",
`import { Prisma, ${this.parent.enumFields
.map((f) => {
return f.type;
})
.join(',')} } from '@prisma/client';`,
];
return res.join('\n');
}
entityImport() {
let res = [
`import { Exclude, Expose, Type } from 'class-transformer';`,
`import {
IsArray,
IsBoolean,
IsDate,
IsDecimal,
IsEnum,
IsNotEmpty,
IsNumber,
IsObject,
IsOptional,
IsJSON,IsInt,
ValidateNested,IsString
} from 'class-validator';`,
"import { ApiProperty } from '@nestjs/swagger';",
"import { Decimal } from '@prisma/client/runtime';",
];
const importPrisma = '{Prisma,' +
this.parent.enumFields
.map((f) => {
return f.type;
})
.join(',') +
'}';
res.push((0, templates_1.importGenerator)(importPrisma, '"@prisma/client"'));
this.parent.objectFields.map((f) => {
res.push((0, templates_1.importGenerator)('{' + this.parent.toPascalCase((0, util_1.tsTypes)(f.type)) + '}', `"../${this.parent.toCamelCase((0, util_1.tsTypes)(f.type))}/${this.parent.toCamelCase((0, util_1.tsTypes)(f.type))}.entity"`));
});
return res.join('\n');
}
classGenerator(name, fields, classValidator = true, classTransformer = true, apiProperty = true, isOptional = true, entity = false) {
const body = fields.map((f) => this.fieldDecoratorGenerator(f, classValidator, classTransformer, apiProperty, isOptional, entity)).join('\n\n');
return (0, templates_1.classGenerator)(name, body);
}
fieldDecoratorGenerator(f, classValidator = true, classTransformer = true, apiProperty = true, isOptional, entity = false) {
let res = [];
if (classTransformer)
res.push(this.classTransformerDecorator(f, entity));
if (apiProperty)
res.push(this.apiPropertyDecorator(f, isOptional, entity));
if (classValidator)
res.push(this.classValidatorDecorator(f, isOptional, entity));
res.push(this.fieldGenerator(f, isOptional, entity));
return res.join('\n');
}
fieldGenerator(f, isOptional, entity = false) {
let res = [];
res.push();
res.push(f.name);
if (isOptional || !f.isRequired || f.kind == 'object')
res.push('?');
res.push(':');
res.push((0, util_1.tsTypes)(f.type, entity));
if (f.isList)
res.push('[]');
return res.join(' ');
}
classTransformerDecorator(f, entity = false) {
let res = [];
if (this.parent.isExcludeList(f.name))
res.push('@Exclude()');
return res.join('\n');
}
apiPropertyDecorator(f, isOptional, entity = false) {
let res = [];
if (this.parent.isInBlackList(f.name))
return '';
res.push('@ApiProperty({');
if (f.isList)
res.push('isArray: true,');
if (f.kind == 'object')
res.push(`type: ()=>${f.type},`);
else if (f.kind == 'enum')
res.push(`enum: ${f.type},`);
else if (f.kind == 'unsupported')
res.push(`// TODO`);
if (f.type == 'Decimal' || f.type == 'Float')
res.push('type: ()=>Decimal,');
if (isOptional || !f.isRequired)
res.push(`required:false,`);
const d = (0, util_1.defaultGenerator)(f.name, (0, util_1.tsTypes)(f.type, entity));
if (!!d)
res.push(`default:${d}`);
res.push('})');
return res.join('\n');
}
classValidatorDecorator(f, isOptional, entity = false) {
let res = [];
if (f.isList)
res.push('@IsArray()');
if (!isOptional && f.isRequired && f.kind != 'object')
res.push('@IsNotEmpty()');
else
res.push('@IsOptional()');
if (f.kind == 'enum')
res.push(``);
else if (f.kind == 'object')
res.push('@ValidateNested()');
if (f.type == 'Int' || f.type == 'BigInt') {
res.push('@IsInt()');
if (f.isId)
res.push('@Type(() => Number)');
}
else if (f.type == 'String' || f.type == 'Text')
res.push('@IsString()');
else if (f.type == 'DateTime')
res.push('@IsDate()');
else if (f.type == 'Boolean')
res.push('@IsBoolean()');
else if (f.type == 'Decimal' || f.type == 'Float')
res.push('@IsDecimal()');
else if (f.type == 'Json')
res.push('@IsJSON()');
else
res.push('@IsObject()');
return res.join('\n');
}
}
exports.default = Types;
//# sourceMappingURL=Types.js.map