diff --git a/.jshintignore b/.jshintignore index 3e5791b3..eb945d85 100644 --- a/.jshintignore +++ b/.jshintignore @@ -1,4 +1,5 @@ lib/expose.js lib/formatter.js +lib/querier.js lib/splitter.js zz/ diff --git a/lib/codegen.js b/lib/codegen.js index a28037d2..e0f5d5eb 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -8,6 +8,7 @@ var _ = require('lodash'); var expose = require('./expose'); var formatter = require('./formatter'); +var querier = require('./querier'); var splitter = require('./splitter'); var ts = require('./typescript'); @@ -297,14 +298,17 @@ var getCode = function(options, type) { } } - // For Swagger Specification version 2.0 value of field 'swagger' must be a string '2.0' - var swaggerView = opts.swagger.swagger === '2.0' ? getViewForSwagger2(opts, type) : getViewForSwagger1(opts, type); + // For Swagger Specification version 2.0 value of field 'swagger' must be a string '2.0' + var swaggerView = opts.swagger.swagger === '2.0' ? getViewForSwagger2(opts, type) : getViewForSwagger1(opts, type); - // format the default responses for the APIs, add objects for the load - var data = formatter.format(swaggerView); + // format the default responses for the APIs, add objects for the load + var formatted = formatter.format(swaggerView); - // expose definitions @ global scope - expose(opts.swagger.definitions, data.methods, opts.path, opts.definitionsDirName); + // create definitions + expose(opts.swagger.definitions, formatted.methods, opts.path, opts.definitionsDirName); + + // add all of the necessary query options + var data = querier(formatted); if (type === 'custom') { if (!_.isObject(opts.template) || !_.isString(opts.template.class) || !_.isString(opts.template.method)) { diff --git a/lib/querier.js b/lib/querier.js new file mode 100644 index 00000000..76a70d7d --- /dev/null +++ b/lib/querier.js @@ -0,0 +1,69 @@ +const _ = require('lodash'); + +const specialKeys = ['Add', 'Create', 'Delete', 'Disable', 'Update']; + +/** + * Get the required properties for the query + * @param params {array} - array of the method parameters + * @returns {object} - { parameters: {string}, questions: {string} } + */ +function getProperties(params) { + const parameters = []; + const questions = []; + params.forEach((param) => { + if (param.in && param.in === 'path' || param.in === 'query') { + parameters.push(param.name); + } + if (param.in && param.in === 'body') { + const { '$ref': ref } = param.schema || {}; + if (ref) { + parameters.push(ref.split('/').slice(-1)[0]); + } + } + questions.push('?'); + }); + return { + parameters: parameters.join(', '), + questions: questions.join(', '), + } +} + +/** + * Create SQL query for the method + * @param data {object} - object with definitions & methods + * @returns {*} + */ +function querier(data) { + try { + const mutable = _.cloneDeep(data); + const { methods, definitions } = mutable; + + if (!(methods && methods.length > 0 && definitions && definitions.length > 0)) { + return new Error('Methods and definitions should not be empty!'); + } + + methods.forEach((method, m) => { + let special = false; + specialKeys.forEach((key) => { + if (method.methodName.includes(key)) { + special = true; + } + }); + + if (!special) { + const { parameters, questions } = getProperties(method.parameters); + mutable.methods[m].query = { + content: `const results = await dal.query("SELECT FN_${method.methodName}(${questions})", [${parameters}], { redis: true });`, + }; + } else { + + } + }); + + return mutable; + } catch (err) { + throw new Error(err.message || err); + } +} + +module.exports = querier; diff --git a/templates/multi-method.mustache b/templates/multi-method.mustache index f0ad567c..da4ea542 100644 --- a/templates/multi-method.mustache +++ b/templates/multi-method.mustache @@ -44,9 +44,16 @@ {{/isFormParameter}} {{/parameters}} - /* - Method logic - */ + {{#query}} + /* + {{&content}} + */ + {{/query}} + {{^query}} + /* + Method logic + */ + {{/query}} {{#responses}} {{#200}}{{{code}}}{{/200}}