@autofleet/sheilta 1.3.8 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/formatter/index.d.ts +1 -3
- package/lib/formatter/index.js +43 -18
- package/package.json +2 -2
package/lib/formatter/index.d.ts
CHANGED
package/lib/formatter/index.js
CHANGED
|
@@ -9,8 +9,9 @@ const utils_1 = require("../utils");
|
|
|
9
9
|
const operators_1 = require("../operators");
|
|
10
10
|
const DEFAULT_ORDER = 'id';
|
|
11
11
|
const DESCENDING_KEY = 'DESC';
|
|
12
|
+
const ASCENDING_KEY = 'ASC';
|
|
12
13
|
const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
|
|
13
|
-
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
14
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE, CUSTOM_FIELDS_SORT_SCOPE } = common_types_1.customFields;
|
|
14
15
|
const parseCustomFieldScopeQueryValue = (value) => {
|
|
15
16
|
if (typeof value === 'string') {
|
|
16
17
|
return value;
|
|
@@ -36,20 +37,43 @@ const getAttributeFromOrder = (order, options = {}) => {
|
|
|
36
37
|
}, [[], []]);
|
|
37
38
|
return [formattedOrder, attributes];
|
|
38
39
|
};
|
|
39
|
-
const formatOrder = ({ order, associationModels = [], }) =>
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
40
|
+
const formatOrder = ({ order, associationModels = [], }) => {
|
|
41
|
+
const formattedOrders = [];
|
|
42
|
+
const orderScopesMap = new Map();
|
|
43
|
+
order.forEach((o) => {
|
|
44
|
+
if ([o, o.substring(1)].some(t => t.startsWith(CUSTOM_FIELDS_QUERY_PREFIX))) {
|
|
45
|
+
if (!orderScopesMap.has(CUSTOM_FIELDS_SORT_SCOPE)) {
|
|
46
|
+
orderScopesMap.set(CUSTOM_FIELDS_SORT_SCOPE, {});
|
|
47
|
+
}
|
|
48
|
+
const scopeKey = o.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
|
|
49
|
+
orderScopesMap.get(CUSTOM_FIELDS_SORT_SCOPE)[scopeKey] = (utils_1.isOrderDesc(o) ?
|
|
50
|
+
DESCENDING_KEY :
|
|
51
|
+
ASCENDING_KEY);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
|
|
55
|
+
const isOrderDescOrder = utils_1.isOrderDesc(o);
|
|
56
|
+
const isOrderAssociation = utils_1.isAttributeByAssociation(isOrderDescOrder
|
|
57
|
+
? o.split(utils_1.ORDER_PREFIX)[1]
|
|
58
|
+
: o, associationModels);
|
|
59
|
+
if (isOrderAssociation) {
|
|
60
|
+
formattedOrder.push(utils_1.extractAssociatedAttributeNameFromOrder(o));
|
|
61
|
+
}
|
|
62
|
+
if (isOrderDescOrder) {
|
|
63
|
+
formattedOrder.push(DESCENDING_KEY);
|
|
64
|
+
}
|
|
65
|
+
formattedOrders.push(formattedOrder);
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
formattedOrders,
|
|
69
|
+
orderScopes: Array.from(orderScopesMap.entries()).map(([scopeName, scopeValue]) => {
|
|
70
|
+
if (!scopeValue) {
|
|
71
|
+
return scopeName;
|
|
72
|
+
}
|
|
73
|
+
return { method: [scopeName, scopeValue] };
|
|
74
|
+
}),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
53
77
|
const formatPage = page => page || utils_1.PAGE_DEFAULT;
|
|
54
78
|
const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
|
|
55
79
|
const formatInclude = (include, associationsMap = {}) => {
|
|
@@ -103,17 +127,18 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
|
|
|
103
127
|
});
|
|
104
128
|
const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
|
|
105
129
|
const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
|
|
106
|
-
const
|
|
130
|
+
const { formattedOrders, orderScopes } = formatOrder({
|
|
107
131
|
order: [...order, DEFAULT_ORDER],
|
|
108
132
|
associationModels,
|
|
109
133
|
});
|
|
110
|
-
const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(
|
|
134
|
+
const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
|
|
111
135
|
const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
|
|
112
136
|
const formattedAttribute = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? allAttributes : { include: allAttributes };
|
|
113
137
|
const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
|
|
114
138
|
const formattedPage = formatPage(page);
|
|
115
139
|
const formattedPerPage = formatPerPage(perPage);
|
|
116
140
|
const result = formatQuery(query, associationModels);
|
|
141
|
+
const queryScopes = result.formattedScopes;
|
|
117
142
|
let { formattedQuery } = result;
|
|
118
143
|
if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
|
|
119
144
|
const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
|
|
@@ -125,6 +150,6 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
|
|
|
125
150
|
],
|
|
126
151
|
};
|
|
127
152
|
}
|
|
128
|
-
return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes:
|
|
153
|
+
return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: [...queryScopes, ...orderScopes] }, (formattedAttribute && { attributes: formattedAttribute }));
|
|
129
154
|
};
|
|
130
155
|
exports.default = formatPayload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sheilta",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.9",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/Autofleet/sheilta",
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@autofleet/common-types": "^1.7.
|
|
47
|
+
"@autofleet/common-types": "^1.7.42",
|
|
48
48
|
"@autofleet/errors": "^1.0.10",
|
|
49
49
|
"joi": "^17.9.2",
|
|
50
50
|
"lodash": "^4.17.21"
|