godspeed-crud-api-generator
Version:
godspeed CRUD api generator will generate events and workflows based on datasource schema model
115 lines • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAndStoreWorkflow = void 0;
const generateWriteLocationForMethod_1 = require("../generateWriteLocationForMethod");
const writeFileSafely_1 = require("../writeFileSafely");
const jsYaml = require('js-yaml');
const generateSummaryBasedOnModelAndMethod = (modelName, method) => {
switch (method) {
case 'one':
return `Fetch ${modelName}`;
case 'create':
return `Create ${modelName}`;
case 'update':
return `Update ${modelName}`;
case 'delete':
return `Delete ${modelName}`;
case 'search':
return `Fetch many ${modelName}`;
default:
return '';
}
};
const generateTaskId = (dataSourceName, modelName, method) => {
return `${dataSourceName.toLowerCase()}_${modelName.toLowerCase()}_${method}`;
};
const generateDsMethod = (modelName, method) => {
switch (method) {
case 'one':
return `${modelName}.findUnique`;
case 'create':
return `${modelName}.create`;
case 'delete':
return `${modelName}.delete`;
case 'update':
return `${modelName}.update`;
case 'search':
return `${modelName}.findMany`;
default:
return '';
}
};
const generateData = (modelFields, method) => {
let indexField = modelFields.find((field) => field.isId);
switch (method) {
case 'one':
return {
data: {
where: {
[`${indexField === null || indexField === void 0 ? void 0 : indexField.name}`]: `<% ${(indexField === null || indexField === void 0 ? void 0 : indexField.type) === 'Int'
? `parseInt(inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name})`
: `inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name}`} %>`,
},
},
};
case 'create':
return {
data: { data: `<% inputs.body %>` },
};
case 'delete':
return {
data: {
where: {
[`${indexField === null || indexField === void 0 ? void 0 : indexField.name}`]: `<% ${(indexField === null || indexField === void 0 ? void 0 : indexField.type) === 'Int'
? `parseInt(inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name})`
: `inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name}`} %>`,
},
},
};
case 'update':
return {
data: {
where: {
[`${indexField === null || indexField === void 0 ? void 0 : indexField.name}`]: `<% ${(indexField === null || indexField === void 0 ? void 0 : indexField.type) === 'Int'
? `parseInt(inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name})`
: `inputs.params.${indexField === null || indexField === void 0 ? void 0 : indexField.name}`} %>`,
},
data: `<% inputs.body %>`,
},
};
case 'search':
return {
data: `<% inputs.body %>`,
};
default:
return '';
}
};
const generateAndStoreWorkflow = async (config) => {
let json;
const { basePathForGeneration, dataSourceName, modelName, method, modelFields, } = config;
const summary = generateSummaryBasedOnModelAndMethod(modelName, method);
const taskId = generateTaskId(dataSourceName, modelName, method);
const data = generateData(modelFields, method);
json = {
summary,
tasks: [
{
id: taskId,
fn: 'com.gs.datastore',
args: {
datasource: dataSourceName,
...data,
config: {
method: generateDsMethod(modelName, method),
},
},
},
],
};
const writeLocation = (0, generateWriteLocationForMethod_1.generateWriteLocationForMethod)(basePathForGeneration, '/functions/com/biz', dataSourceName, modelName, method);
await (0, writeFileSafely_1.writeFileSafely)(writeLocation, jsYaml.dump(json));
return 'generated all workflows';
};
exports.generateAndStoreWorkflow = generateAndStoreWorkflow;
//# sourceMappingURL=workflow.js.map