1 | 'use strict';
|
2 |
|
3 | const debug = require('debug')('krh:route:util');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const wrapPagingResult = exports.wrapPagingResult = function (result, options) {
|
12 | return {
|
13 | content: result,
|
14 | page: {
|
15 | size: options.limit,
|
16 | totalElements: options.count,
|
17 | totalPages: options.limit == 0 ? 0 : Math.ceil(options.count / options.limit),
|
18 | number: options.page ? options.page : 0
|
19 | },
|
20 | size: options.limit,
|
21 | totalElements: options.count,
|
22 | totalPages: options.limit == 0 ? 0 : Math.ceil(options.count / options.limit),
|
23 | number: options.page ? options.page : 0
|
24 | };
|
25 | }
|
26 |
|
27 |
|
28 | exports.halWrapper = function (entity) {
|
29 | return {
|
30 | "_links": {
|
31 | "self": {
|
32 | "href": "/api/v1/" + entity + "{?page,size,sort}",
|
33 | "templated": true
|
34 | },
|
35 | "search": {
|
36 | "href": "/api/v1/" + entity + "/search"
|
37 | }
|
38 | }
|
39 | };
|
40 | }
|
41 |
|
42 | exports.sortResolver = function () {
|
43 |
|
44 | var promise = new Promise((resolve, reject) => {
|
45 |
|
46 | if (this.query['_sort']) {
|
47 | var sortColumns = this.query['_sort'].split(',');
|
48 | var order = [];
|
49 |
|
50 | sortColumns.forEach((sortColumn) => {
|
51 | if (sortColumn.indexOf('-') === 0) {
|
52 | var actualName = sortColumn.substring(1).replace(/[;*,'"]/g, '');
|
53 | order.push(actualName + ' DESC');
|
54 | } else {
|
55 | order.push(sortColumn + ' ASC');
|
56 | }
|
57 | });
|
58 | resolve(order);
|
59 | } else {
|
60 | this.state.connection(this.state.entity).select('*').from('information_schema.columns')
|
61 | .where({'table_name': this.state.entity, 'table_schema': this.state.schema}).then((columns) => {
|
62 | debug('Use first column for sorting, %o', columns);
|
63 | if(columns.length == 0){
|
64 | debug("Sort resolved failed because cannot find column definitions of", this.state.schema, this.state.entity);
|
65 | resolve([]);
|
66 | }else{
|
67 | resolve([columns[0].COLUMN_NAME]);
|
68 | }
|
69 |
|
70 | });
|
71 | }
|
72 | });
|
73 |
|
74 | return promise;
|
75 | }
|
76 |
|
77 | exports.parsePk = function (ids, idField) {
|
78 | let pkParams = {};
|
79 | ids = ids.filter((e) => idField.filter((e1) => e1 == e[0]));
|
80 | debug('Ids %o', ids);
|
81 |
|
82 | ids.forEach((e) => {
|
83 | let value = e[1]
|
84 | let pk = e[0]
|
85 | try {
|
86 | value = decodeURIComponent(value)
|
87 | } catch (e) {
|
88 |
|
89 | }
|
90 | pkParams[pk] = value
|
91 | });
|
92 | return pkParams;
|
93 | }
|
94 |
|
95 | exports.appendClause = function (table, queryObjs) {
|
96 |
|
97 | let firstClause = true;
|
98 |
|
99 | for (let column in queryObjs) {
|
100 |
|
101 | let queryObj = queryObjs[column];
|
102 | let sqlParameter = convertToSqlParameter(queryObj);
|
103 |
|
104 | if (firstClause) {
|
105 | table[queryObj.whereType.replace('W', 'w')].apply(table, sqlParameter.clause);
|
106 | } else {
|
107 | if(!table[sqlParameter.condition]){
|
108 | sqlParameter.condition = sqlParameter.condition.replace('andW', 'w');
|
109 | }
|
110 | table[sqlParameter.condition].apply(table, sqlParameter.clause);
|
111 | }
|
112 | firstClause = false;
|
113 | }
|
114 |
|
115 | return table;
|
116 | }
|
117 |
|
118 | const convertToSqlParameter = function (queryObj) {
|
119 |
|
120 | let clause = [queryObj.columnName, queryObj.operatorAlias, queryObj.value];
|
121 | if (typeof queryObj.value === 'function') {
|
122 | clause = [queryObj.columnName];
|
123 | }
|
124 | if (queryObj.whereType.search('In') > -1) {
|
125 | clause = [queryObj.columnName, queryObj.value];
|
126 | }
|
127 | if (queryObj.whereType.search('Between') > -1) {
|
128 | clause = [queryObj.columnName, queryObj.value];
|
129 | }
|
130 |
|
131 | return {
|
132 | condition: queryObj.condition + queryObj.whereType,
|
133 | clause: clause
|
134 | }
|
135 | } |
\ | No newline at end of file |