-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathquerier.js
67 lines (59 loc) · 2.01 KB
/
querier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.name) {
if(param.in === 'body')
parameters.push(param.name + '.data');
else
parameters.push(param.name);
}
questions.push('?');
});
return {
parameters: parameters.join(', '),
questions: questions.join(', '),
}
}
/**
* Create SQL query for the method
* @param data {object} - object with definitions & methods
* @returns {object} - same as data, but with updated methods
*/
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;
}
});
const { parameters, questions } = getProperties(method.parameters, special);
let query ='';
if(special){
query = `CALL SP_${method.methodName}(${questions})`;
}else{
query = `SELECT FN_${method.methodName}(${questions}) as Response`;
}
mutable.methods[m].query = `const results = await dal.query("${query}", `
+ `[${parameters}], { redis: ${!special} });`;
});
return mutable;
} catch (err) {
throw new Error(err.message || err);
}
}
module.exports = querier;