-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (118 loc) · 4.54 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var qs = require('qs');
let buildSQL = require('./helpers/sql_helper')
let buildKnex = require('./helpers/knex_helper')
exports.translate = function (params) {
// let params = {
// format: 'sql',
// query: '',
// tableName: 'users',
// allowedColumns: ['id','email','etc...']
// }
// params.format = 'sql', 'knex' or 'pretty'
toReturn = {};
let parsedQuery
if(typeof params.query == 'object') {
parsedQuery = params.query
} else {
parsedQuery = qs.parse(params.query);
}
// let sql_query = 'SELECT {fields} FROM '+params.tableName;
let wheres = [];
let sorts = [];
let columns = '*';
for (let key of Object.keys(parsedQuery)) {
let val = parsedQuery[key]
if (key == 'filter') { // FILTER
for (let filterKey of Object.keys(val)) {
let filterVal = val[filterKey]
if (typeof filterVal == 'string') {
// EQUAL TO
wheres.push({
'column': filterKey,
'condition': '=',
'value': filterVal
})
} else {
for (let filterSubKey of Object.keys(filterVal)) {
const filterSubVal = filterVal[filterSubKey]
if (filterSubKey == 'not') { // [gt] GREATER THAN
wheres.push({
'column': filterKey,
'condition': '<>',
'value': filterSubVal
})
} else if (filterSubKey == 'gt') { // [gt] GREATER THAN
wheres.push({
'column': filterKey,
'condition': '>',
'value': filterSubVal
})
} else if (filterSubKey == 'lt') { // [lt] LESS THAN
wheres.push({
'column': filterKey,
'condition': '<',
'value': filterSubVal
})
} else if (filterSubKey == 'gte') { // [lt] GREATER THAN OR EQUAL TO
wheres.push({
'column': filterKey,
'condition': '>=',
'value': filterSubVal
})
} else if (filterSubKey == 'lte') { // [lt] LESS THAN OR EQUAL TO
wheres.push({
'column': filterKey,
'condition': '<=',
'value': filterSubVal
})
} else if (filterSubKey == 'in') { // [in] IN
wheres.push({
'column': filterKey,
'condition': 'IN',
'value': filterSubVal
})
} else if (filterSubKey == 'or_in') { // [in] IN
wheres.push({
'column': filterKey,
'condition': 'OR IN',
'value': filterSubVal
})
}
}
}
}
} else if (key == 'sort') { // SORT
for (let sortItem of val) {
let sortItemSplitted = sortItem.split(',');
let sortType = 'DESC';
if (sortItemSplitted.length > 1) {
if (sortItemSplitted[1].toLowerCase() == 'asc') {
sortType = 'ASC';
}
}
sorts.push({
'column': sortItemSplitted[0],
'type': sortType
});
}
} else if (key == 'columns') { // COLUMNS
columns = val.split(',')
}
}
let easy = {
tableName: params.tableName,
filter: wheres,
sorts: sorts,
columns: columns
}
switch (params.format.toLowerCase()) {
case 'easy':
return easy
case 'sql':
return buildSQL(easy)
case 'knex':
easy['knex'] = params.knex
return buildKnex(easy)
}
return false;
}