@agatee/cli
Version:
CLI for Agatee App
42 lines (41 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.serviceStarterCode = void 0;
var text_1 = require("../../../utils/text");
var prettier_1 = require("prettier");
var serviceStarterCode = function (params, pathName) {
var _a;
var name = (0, text_1.camelCase)(pathName);
var capitalizedComponentName = (0, text_1.capitalize)(name);
var addMongoose = params.additionalParams['mongoose-model'] || params.additionalParams['mm'];
var endpoints = { create: false, read: false, update: false, delete: false };
if (params.additionalParams['endpoints'] || params.additionalParams['e']) {
var endpointOption = params.additionalParams['endpoints'] || params.additionalParams['e'];
var endpointValue = ((_a = endpointOption.values[0]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
endpoints.create = endpointValue.includes('c');
endpoints.read = endpointValue.includes('r');
endpoints.update = endpointValue.includes('u');
endpoints.delete = endpointValue.includes('d');
}
var contentHeader = "import { Injectable } from '@agatee/core';\n\n";
if (addMongoose) {
contentHeader += "import { " + capitalizedComponentName + "Model, " + capitalizedComponentName + "Document } from './" + pathName + ".model';\n\n";
}
var content = "@Injectable()\nexport class " + capitalizedComponentName + "Service {\n";
if (addMongoose && endpoints.create) {
content += ("\nasync create(input: Partial<" + capitalizedComponentName + "Document>) {\n return await " + capitalizedComponentName + "Model.create(input);\n }\n");
}
if (addMongoose && (endpoints.read || endpoints.update)) {
content += ("\nasync getAll() {\n return await " + capitalizedComponentName + "Model.find({});\n }\n");
content += ("\nasync getById(input: {id: string}) {\n return await " + capitalizedComponentName + "Model.findOne({_id: input.id})\n }\n");
}
if (addMongoose && endpoints.update) {
content += ("\nasync updateById(input: {id: string, update: Partial<" + capitalizedComponentName + "Document>}) {\n return await " + capitalizedComponentName + "Model.updateOne({_id: input.id}, input.update);\n }\n");
}
if (addMongoose && endpoints.delete) {
content += ("\nasync deleteById(input: {id: string}) {\n return await " + capitalizedComponentName + "Model.deleteOne({_id: input.id})\n }\n");
}
content += "\n}\n";
return contentHeader + (0, prettier_1.format)(content, { parser: 'typescript' });
};
exports.serviceStarterCode = serviceStarterCode;