UNPKG

@arianaprojects/prisma-nestjs-crud-generator

Version:
93 lines 5.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const templates_1 = require("../templates"); const util_1 = require("../util"); const CRUD_1 = __importDefault(require("./CRUD")); const FileGenerator_1 = __importDefault(require("./FileGenerator")); const Types_1 = __importDefault(require("./Types")); class Model extends FileGenerator_1.default { constructor(model, option) { super(); this.blackList = ['deletedAt', 'updatedAt', 'createdAt']; this.excludeList = ['deletedAt']; this.userList = ['userId', 'employeeId']; this.model = model; this.option = option; this.fullPath = option.fullPath && option.fullPath; this.configPath = option.configPath; this.preGenerator(); this.types = new Types_1.default(this); this.crud = new CRUD_1.default(this); } preGenerator() { this.idFields = this.model.fields.filter((x) => x.isId); this.createFieldsAdmin = this.model.fields.filter((x) => !x.isId && (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.updateFieldsAdmin = this.model.fields.filter((x) => !x.isId && (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.findFieldsAdmin = this.model.fields.filter((x) => (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.connectFieldsAdmin = this.model.fields.filter((x) => x.isId || x.isUnique); this.deletedField = this.model.fields.filter((x) => x.name == 'deletedAt'); this.accessField = this.model.fields.filter((x) => this.isInUserList(x.name)); this.createFields = this.model.fields.filter((x) => !x.isId && (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.updateFields = this.model.fields.filter((x) => !x.isId && (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.findFields = this.model.fields.filter((x) => (x.kind == 'scalar' || x.kind == 'enum' || x.kind == 'unsupported') && !this.isInBlackList(x.name)); this.connectFields = this.model.fields.filter((x) => x.isId || x.isUnique); this.returnFields = this.model.fields.filter((x) => !this.isInBlackList(x.name)); this.uniqFields = this.model.fields.filter((x) => x.isUnique); this.entityFields = this.model.fields; this.enumFields = this.model.fields.filter((x) => x.kind == 'enum'); this.objectFields = this.model.fields.filter((x) => x.kind == 'object'); this.idsNoDefault = this.idFields.filter((x) => !x.default); this.nameCamel = this.toCamelCase(this.model.name); this.namePascal = this.toPascalCase(this.model.name); this.module = ` import { Module } from '@nestjs/common'; ${(0, templates_1.importGenerator)('{' + this.namePascal + 'Controller }', '"./' + this.nameCamel + '.controller"')} ${(0, templates_1.importGenerator)('{' + this.namePascal + 'Service }', '"./' + this.nameCamel + '.service"')} ${(0, templates_1.moduleGenerator)(this.namePascal + 'Module', '', this.namePascal + 'Service', this.namePascal + 'Controller', '')}`; } generator() { } toString() { console.log(this.types.dto); console.log(this.types.entity); console.log(this.crud.controller); console.log(this.crud.service); } postGenerate() { console.log(this.fullPath + 'writeTSFile'); (0, util_1.writeTSFile)(this.fullPath + `/${this.toCamelCase(this.model.name)}/${this.toCamelCase(this.model.name)}.dto.ts`, this.types.dto, false); (0, util_1.writeTSFile)(this.fullPath + `/${this.toCamelCase(this.model.name)}/${this.toCamelCase(this.model.name)}.entity.ts`, this.types.entity, false); (0, util_1.writeTSFile)(this.fullPath + `/${this.toCamelCase(this.model.name)}/${this.toCamelCase(this.model.name)}.controller.ts`, this.crud.controller, false); (0, util_1.writeTSFile)(this.fullPath + `/${this.toCamelCase(this.model.name)}/${this.toCamelCase(this.model.name)}.service.ts`, this.crud.service, false); (0, util_1.writeTSFile)(this.fullPath + `/${this.toCamelCase(this.model.name)}/${this.toCamelCase(this.model.name)}.module.ts`, this.module, false); } isInBlackList(name) { return this.blackList.includes(name); } isExcludeList(name) { return this.excludeList.includes(name); } isInUserList(name) { return this.userList.includes(name); } replace(s) { if (typeof s == 'string') return s .replace('NAME_PASCAL', this.namePascal) .replace('NAME_CAMEL', this.nameCamel) .replace('ENTITY', this.namePascal) .replace('CREATE_DTO', `Create${this.namePascal}Dto`) .replace('CONNECT_DTO', `Connect${this.namePascal}Dto`) .replace('FIND_DTO', `Find${this.namePascal}Dto`) .replace('UPDATE_DTO', `Update${this.namePascal}Dto`) .replace('CREATE_ADMIN_DTO', `Create${this.namePascal}AdminDto`) .replace('CONNECT_ADMIN_DTO', `Connect${this.namePascal}AdminDto`) .replace('FIND_ADMIN_DTO', `Find${this.namePascal}AdminDto`) .replace('UPDATE_ADMIN_DTO', `Update${this.namePascal}AdminDto`); return s; } } exports.default = Model; //# sourceMappingURL=Model.js.map