-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (113 loc) · 3.46 KB
/
index.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
129
130
131
132
const OPERATORS = {
eq: { op: '$eq' },
ne: { op: '$ne' },
contains: { op: '$regex', options: { $options: 'i' } },
lt: { op: '$lt' },
lte: { op: '$lte' },
gt: { op: '$gt' },
gte: { op: '$gte' },
in: { op: '$in', transform: (value) => typeof value === 'string' ? value.split(',') : value, },
exists: { op: '$exists', transform: (value) => value === 'true' },
eqInt: { op: '$eq', transform: (value) => transformToInt(value) },
};
const LOG_OPERATORS = {
and: '$and',
or: '$or',
};
const FILTERS_KEYWORDS = ['page', 'limit', 'sort', '_op', '_fields'];
const REGEX_ISO_8601 = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/;
const castValues = (value, transform) => {
value = transform ? castValues(transform(value)) : value
if (value === 'null') {
return null;
} else if (value === 'true' || value === 'false') {
return value === 'true';
} else if (typeof value === 'string' && (match = value.match(REGEX_ISO_8601))) {
var milliseconds = Date.parse(match[0]);
if (!isNaN(milliseconds)) {
return new Date(milliseconds);
}
} else if (Array.isArray(value)) {
return value.map((v) => castValues(v));
} else {
return value;
}
};
const getFilters = (query) => {
const operator = getOperator(query);
const filters = Object.keys(query)
.filter(key => !FILTERS_KEYWORDS.includes(key))
.reduce((acc, key) => {
const field = key.split('.');
const filterOperator = Object.keys(OPERATORS).includes(field[field.length - 1]) ? field.pop() : 'eq';
const { op, options, transform } = OPERATORS[filterOperator];
const values = castValues(query[key], transform);
(Array.isArray(values) && op !== '$in' ? values : [values]).forEach((value) => {
const filter = { [op]: value };
if (options) { Object.assign(filter, options) };
acc[operator].push({ [field.join('.')]: filter });
})
return acc;
}, { [operator] : [] });
return filters[operator].length ? filters : {};
};
const getPagination = (query) => {
if (query.limit === 'null') {
return undefined;
}
const page = Number(query.page || 1);
const pageSize = Number(query.limit || 10);
return {
limit: pageSize,
offset: (page - 1) * pageSize,
};
};
const getSort = (query) => (
query.sort ? transformSort(query.sort.split(',')) : undefined
);
const getOperator = (query) => {
const { _op='and' } = query;
return LOG_OPERATORS[_op] ? LOG_OPERATORS[_op] : LOG_OPERATORS.and;
};
const getFields = (query) => {
const { _fields } = query;
if(!_fields) {
return undefined
}
return _fields.split(',').reduce((acc, field) => {
acc[field.trim()] = 1;
return acc;
}, {});
}
const transformSort = (sort) => {
const regex = /^(.*?)(?:\s(ASC|DESC))?$/;
if (!sort) {
return {};
}
return sort.reduce((hash, term) => {
const re = regex.exec(term);
const field = re[1].trim();
const order = (re[2] || 'ASC').toUpperCase() === 'ASC' ? 1 : -1;
hash[field] = order;
return hash;
}, {});
};
const transformToInt = (value) => {
const integer = new Number(value) + 0;
return !isNaN(integer) ? integer : value;
};
class QueryBuilder {
static build(query) {
const filters = getFilters(query);
const pagination = getPagination(query);
const sort = getSort(query);
const fields = getFields(query);
return {
pagination,
filters,
sort,
fields,
};
}
}
module.exports = QueryBuilder;