forked from alwint3r/sequelize-datatable-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (134 loc) · 3.94 KB
/
index.js
File metadata and controls
169 lines (134 loc) · 3.94 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const _ = require(`lodash`);
const searchBuilder = require(`./search_builder`);
const helper = require(`./helper`);
const Promise = require(`bluebird`);
function describe(model) {
return model.describe();
}
function paginate(config) {
if (_.isUndefined(config.start) || _.isUndefined(config.length)) {
return {};
}
const start = Number(config.start);
const length = Number(config.length);
if (_.isNaN(start) || _.isNaN(length)) {
return {};
}
return {
offset: start,
limit: length,
};
}
function search(model, config, modelName, opt) {
if (_.isUndefined(config.search) || !config.search.value) {
return Promise.resolve({});
}
const dialect = helper.getDialectFromModel(model);
return describe(model).then(description => _.concat(
searchBuilder.charSearch(modelName, description, config, opt, dialect),
searchBuilder.numericSearch(modelName, description, config, opt),
searchBuilder.booleanSearch(modelName, description, config, opt)
));
}
function buildSearch(model, config, params, opt) {
const leaves = helper.dfs(params, [], []);
if (_.isUndefined(config.search) || !config.search.value) {
return Promise.resolve({});
}
return Promise.map(leaves, leaf =>
search(leaf.model || model, config, leaf.as || ``, opt)
).then((result) => {
const objects = _.filter(result, res => _.isObject(res) && !_.isArray(res) && !_.isEmpty(res));
const arrays = _.filter(result, res => _.isArray(res) && !_.isEmpty(res));
const reducedArrays = _.reduce(arrays, (acc, val) => _.concat(acc, val), []);
const reducedObject = _.reduce(objects, (acc, val) => _.merge(acc, val), {});
const concatenated = {
$or: _.filter(_.concat(reducedArrays, reducedObject), res => !_.isEmpty(res)),
};
return concatenated;
});
}
function buildOrder(model, config, params) {
if (!config.order) {
return [];
}
const order = config.order[0];
const col = config.columns[order.column].data;
const leaves = helper.dfs(params, [], []);
if (col.indexOf(`.`) > -1) {
const splitted = col.split(`.`);
const colName = splitted.pop();
const orders = _.compact(_.map(splitted, (modelName) => {
const found = _.filter(leaves, leaf =>
leaf.as === modelName
)[0];
if (!found) {
return false;
}
return {
model: found.model,
as: found.as,
};
}));
if (orders.length < 1) {
return [];
}
orders.push(colName);
orders.push(order.dir.toUpperCase());
return orders;
}
return [helper.getColumnName(col), order.dir.toUpperCase()];
}
function getResult(model, config, modelParams, opt) {
const params = modelParams;
/* mutate params */
return buildSearch(model, config, params, opt)
.then((result) => {
if (_.isEmpty(result)) {
return params;
}
if (params.where) {
params.where = {
$and: [
params.where,
result,
],
};
} else {
params.where = result;
}
return params;
})
.then(() => {
const order = buildOrder(model, config, params);
return order;
})
.then((orderResult) => {
if (orderResult.length > 0) {
params.order = [orderResult];
}
_.assign(params, paginate(config));
return params;
})
.then(newParams =>
Promise.all([
model.count({}),
model.findAndCountAll(newParams),
]))
.then(result => ({
draw: Number(config.draw),
data: _.map(result[1].rows, row => row.toJSON()),
recordsFiltered: result[1].count,
recordsTotal: result[0],
_: config._,
}));
}
function dataTable(model, config, modelParams, options) {
const opt = options || {};
if (!model || !config) {
return Promise.reject(new Error(`Model and config should be provided`));
}
return getResult(model, config, modelParams || {}, opt);
}
module.exports = dataTable;