-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryHelper.js
70 lines (62 loc) · 2.56 KB
/
queryHelper.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
// sequelize - sequelize object(your var sequelize = new Sequelize();)
// request - Express request
// offsetF - name of offset field from response query. Example: 'offset'
// countF - name of count field from response query
// qF - name of q field from response query
// searchArray - array of search columns
// sortField - name of q sort field from response query. Example: 'filter'. Field data: 'name' or '-name' etc.
// sortFields - array of arrays with filter names and table columns. Example: [['name', 'name'], ['createdAt', 'createdAt']] and with relations [['date', '"Orders"."orderDate"']]
module.exports = function(sequelize, offsetF, countF, qF, searchArray, sortFieldF, sortFields) {
return {
getConfig: function(request) {
var config = {};
var q = request.query[qF];
var offset = request.query[offsetF];
var count = request.query[countF];
var sortField = request.query[sortFieldF];
config.where = {};
config.include = [];
// Supports relations or search
if (q && searchArray.length > 0) {
config.where.$or = [];
var putToInclude;
for (var i = 0; i < searchArray.length; i++) {
if (searchArray[i].indexOf('.') != -1) {
putToInclude = true;
break;
}
}
var likes = [];
for (var i = 0; i < searchArray.length; i++) {
if (searchArray[i].indexOf('.') != -1) {
var model = sequelize.model(searchArray[i].substr(0, searchArray[i].indexOf('.')));
if (model) {
config.include.push({model: model, where: {$or: []}});
}
}
var likeQuery = sequelize.where(sequelize.cast(sequelize.col(searchArray[i]), 'TEXT'), 'ILIKE', '%' + q + '%');
if (!putToInclude) config.where.$or.push(likeQuery);
else likes.push(likeQuery);
}
if (putToInclude && config.include.length > 0) {
config.include[0].where.$or = likes;
}
}
if (sortField && sortFields) {
var isRevers = sortField.indexOf('-') == 0;
if (isRevers) sortField = sortField.substr(1, sortField.length);
for (var i = 0; i < sortFields.length; i++) {
var map = sortFields[i];
if (map == sortField) {
if (!config.order) config.order = {};
config.order = [[sortField, isRevers ? 'DESC' : 'ASC']]
}
}
}
if (offset && !isNaN(offset)) config.offset = offset;
if (count && !isNaN(offset)) config.limit = count;
config.subQuery = false;
return config;
}
}
};