-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations.js
128 lines (113 loc) · 4.06 KB
/
operations.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
'use strict';
const { col, fn, Op, where } = require('sequelize');
const { isColumnDateField } = require('./comparer');
const { formatDate, formatValue } = require('./formatter');
const nonFormattingComparators = ['^', '$', '~'];
const comparatorMap = {
'$in': Op.in,
'$nin': Op.notIn,
'=': Op.eq,
'!': Op.ne,
'!=': Op.ne,
'<>': Op.ne,
'>': Op.gt,
'>=': Op.gte,
'<': Op.lt,
'<=': Op.lte,
'is': Op.is,
'is not': Op.not,
'^': Op.startsWith,
'$': Op.endsWith,
'~': Op.substring
};
const handleBasicComparator = (operationStr, column, value, options) => {
// check & strip if we have the comparator at the start
let strippedValue = (value && typeof(value) === 'string' && value.startsWith(operationStr))
? value.substring(operationStr.length)
: value;
// handle getting the value into the correct format
// TODO: maybe tidy this up a litte? Make it more readable?
let formattedValue = (nonFormattingComparators.some(el => el === operationStr))
? strippedValue
: (isColumnDateField(column, options) && options.dateOnlyCompare === true)
? formatDate(formatValue(strippedValue))
: formatValue(strippedValue);
// handle creating the correct operation
let sequelizeOp = comparatorMap[operationStr];
let operation = (isColumnDateField(column, options) && options.dateOnlyCompare === true)
? where(fn('date', col(column)), {
[sequelizeOp]: formattedValue
})
: { [sequelizeOp]: formattedValue };
return {
op: operationStr,
column,
value: formattedValue,
operation
};
};
const handleBetweenOperation = (operationStr, column, value, options) => {
// check & strip if we have the comparator at the start
let strippedValue = (value && value.startsWith(operationStr))
? value.substring(operationStr.length)
: value;
// split the array so we can get the two values to compare on
let splitValue = strippedValue.split('|');
let formattedMinValue = formatValue(splitValue[0]);
let formattedMaxValue = formatValue(splitValue[1]);
// handle creating the correct operation for the date fields
if (isColumnDateField(column, options) && options.dateOnlyCompare === true) {
let dateMin = formatDate(formattedMinValue);
let dateMax = formatDate(formattedMaxValue);
return {
op: operationStr,
column,
value: [dateMin, dateMax],
operation: {
[Op.and]: [
where(fn('date', col(column)), '>=', dateMin),
where(fn('date', col(column)), '<=', dateMax)
]
}
};
}
else {
return {
op: operationStr,
column,
value: [formattedMinValue, formattedMaxValue],
operation: { [Op.between]: [formattedMinValue, formattedMaxValue] }
};
}
};
const handleInComparator = (operationStr, column, value, options) => {
// check & strip if we have the comparator at the start
let strippedValue = (value && value.startsWith(operationStr))
? value.substring(operationStr.length)
: value;
// handle getting the value into the correct format
let formattedArray = strippedValue.split('|').map((e) => {
let formattedValue = formatValue(e);
return (isColumnDateField(column, options) && options.dateOnlyCompare === true)
? formatDate(formattedValue)
: formattedValue;
});
// handle creating the correct operation
let sequelizeOp = comparatorMap[operationStr];
let operation = (isColumnDateField(column, options) && options.dateOnlyCompare === true)
? where(fn('date', col(column)), {
[sequelizeOp]: formattedArray
})
: { [sequelizeOp]: formattedArray };
return {
op: operationStr,
column,
value: formattedArray,
operation
};
};
module.exports = {
handleBasicComparator,
handleBetweenOperation,
handleInComparator
};