Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lib/expose.js
lib/formatter.js
lib/querier.js
lib/splitter.js
zz/
16 changes: 10 additions & 6 deletions lib/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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)) {
Expand Down
69 changes: 69 additions & 0 deletions lib/querier.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 10 additions & 3 deletions templates/multi-method.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@
{{/isFormParameter}}
{{/parameters}}

/*
Method logic
*/
{{#query}}
/*
{{&content}}
*/
{{/query}}
{{^query}}
/*
Method logic
*/
{{/query}}

{{#responses}}
{{#200}}{{{code}}}{{/200}}
Expand Down