forked from alwint3r/sequelize-datatable-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_builder.js
More file actions
111 lines (89 loc) · 2.59 KB
/
search_builder.js
File metadata and controls
111 lines (89 loc) · 2.59 KB
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
'use strict';
const _ = require(`lodash`);
const helper = require(`./helper`);
const possibleNumericTypes = [
`INTEGER`,
`DECIMAL`,
`FLOAT`,
`DOUBLE`,
`INT`,
`TINYINT`,
`BIGINT`,
`NUMBER`,
`REAL`,
];
const possibleStringTypes = [
`CHARACTER VARYING`,
`VARCHAR`,
`TEXT`,
`CHAR`,
`STRING`,
`TINYTEXT`,
`MEDIUMTEXT`,
`LONGTEXT`,
];
function isTypeExists(typesList, item) {
return _.filter(typesList, type => item.indexOf(type) > -1).length > 0;
}
function filterColumns(modelName, config) {
return _.filter(config.columns, (col) => {
const modelAndColumn = _.takeRight(helper.getModelAndColumn(col.data), 2);
return modelName === modelAndColumn[0];
});
}
function createNameMaps(columns) {
return _.reduce(columns, (acc, col) => _.merge(acc, {
[helper.getModelAndColumn(col.data).pop()]: col,
}), {});
}
function charSearch(modelName, modelDesc, config, opt, dialect) {
const columns = filterColumns(modelName, config);
const nameMaps = createNameMaps(columns);
const matchNames = _(modelDesc)
.keys()
.filter((item) => {
const isChar = isTypeExists(possibleStringTypes, modelDesc[item].type);
return isChar && nameMaps[item] && config.search.value;
})
.value();
let likeOp = opt.caseInsensitive ? `$ilike` : `$like`;
if (dialect === `mysql`) {
likeOp = `$like`;
}
return _.map(matchNames, name => ({
[helper.searchify(nameMaps[name].data)]: { [likeOp]: `%${config.search.value}%` },
}));
}
function numericSearch(modelName, modelDesc, config) {
const columns = filterColumns(modelName, config);
const nameMaps = createNameMaps(columns);
const matchNames = _(modelDesc)
.keys()
.filter((item) => {
const isNumeric = isTypeExists(possibleNumericTypes, modelDesc[item].type);
return isNumeric && nameMaps[item] && !_.isNaN(Number(config.search.value));
})
.value();
return _.map(matchNames, name => ({
[helper.searchify(nameMaps[name].data)]: Number(config.search.value),
}));
}
function booleanSearch(modelName, modelDesc, config) {
const columns = filterColumns(modelName, config);
const nameMaps = createNameMaps(columns);
const matchNames = _(modelDesc)
.keys()
.filter((item) => {
const isNumeric = possibleNumericTypes.indexOf(modelDesc[item].type) > -1;
return isNumeric && nameMaps[item] && helper.boolAlike(config.search.value);
})
.value();
return _.map(matchNames, name => ({
[helper.searchify(nameMaps[name].data)]: helper.boolify(config.search.value),
}));
}
module.exports = {
numericSearch,
charSearch,
booleanSearch,
};