@autofleet/sheilta 1.3.8-beta.0 → 1.3.8-beta.1

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.
@@ -24,8 +24,6 @@ declare const formatPayload: ({ order, page, perPage, include, query, attributes
24
24
  page: any;
25
25
  perPage: any;
26
26
  include: any;
27
- scopes: (string | {
28
- method: any[];
29
- })[];
27
+ scopes: any[];
30
28
  };
31
29
  export default formatPayload;
@@ -6,20 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const lodash_1 = __importDefault(require("lodash"));
7
7
  const common_types_1 = require("@autofleet/common-types");
8
8
  const utils_1 = require("../utils");
9
- const operators_1 = require("../operators");
10
9
  const DEFAULT_ORDER = 'id';
11
10
  const DESCENDING_KEY = 'DESC';
11
+ const ASCENDING_KEY = 'ASC';
12
12
  const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
13
- const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
14
- const parseCustomFieldScopeQueryValue = (value) => {
15
- if (typeof value === 'string') {
16
- return value;
17
- }
18
- return Object.entries(value).map(([operator, conditionValue]) => ({
19
- operator: operators_1.OPERATORS_TO_SQL[operator],
20
- value: conditionValue,
21
- }));
22
- };
13
+ const { CUSTOM_FIELDS_FILTER_SCOPE, CUSTOM_FIELDS_SORT_SCOPE } = common_types_1.customFields;
23
14
  const getAttributeFromOrder = (order, options = {}) => {
24
15
  const { literalAttributes = [], DBFormatter = undefined } = options;
25
16
  const [formattedOrder, attributes] = order.reduce((acc, o) => {
@@ -36,20 +27,44 @@ const getAttributeFromOrder = (order, options = {}) => {
36
27
  }, [[], []]);
37
28
  return [formattedOrder, attributes];
38
29
  };
39
- const formatOrder = ({ order, associationModels = [], }) => order.map((o) => {
40
- const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
41
- const isOrderDescOrder = utils_1.isOrderDesc(o);
42
- const isOrderAssociation = utils_1.isAttributeByAssociation(isOrderDescOrder
43
- ? o.split(utils_1.ORDER_PREFIX)[1]
44
- : o, associationModels);
45
- if (isOrderAssociation) {
46
- formattedOrder.push(utils_1.extractAssociatedAttributeNameFromOrder(o));
47
- }
48
- if (isOrderDescOrder) {
49
- formattedOrder.push(DESCENDING_KEY);
50
- }
51
- return formattedOrder;
52
- });
30
+ const formatOrder = ({ order, associationModels = [], }) => {
31
+ const formattedOrders = [];
32
+ const orderScopesMap = new Map();
33
+ order.forEach((o) => {
34
+ if (o.startsWith(CUSTOM_FIELDS_QUERY_PREFIX) ||
35
+ o.substring(1).startsWith(CUSTOM_FIELDS_QUERY_PREFIX)) {
36
+ if (!orderScopesMap.has(CUSTOM_FIELDS_SORT_SCOPE)) {
37
+ orderScopesMap.set(CUSTOM_FIELDS_SORT_SCOPE, {});
38
+ }
39
+ const scopeKey = o.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
40
+ orderScopesMap.get(CUSTOM_FIELDS_SORT_SCOPE)[scopeKey] = (utils_1.isOrderDesc(o) ?
41
+ DESCENDING_KEY :
42
+ ASCENDING_KEY);
43
+ return;
44
+ }
45
+ const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
46
+ const isOrderDescOrder = utils_1.isOrderDesc(o);
47
+ const isOrderAssociation = utils_1.isAttributeByAssociation(isOrderDescOrder
48
+ ? o.split(utils_1.ORDER_PREFIX)[1]
49
+ : o, associationModels);
50
+ if (isOrderAssociation) {
51
+ formattedOrder.push(utils_1.extractAssociatedAttributeNameFromOrder(o));
52
+ }
53
+ if (isOrderDescOrder) {
54
+ formattedOrder.push(DESCENDING_KEY);
55
+ }
56
+ formattedOrders.push(formattedOrder);
57
+ });
58
+ return {
59
+ formattedOrders,
60
+ orderScopes: Array.from(orderScopesMap.entries()).map(([scopeName, scopeValue]) => {
61
+ if (!scopeValue) {
62
+ return scopeName;
63
+ }
64
+ return { method: [scopeName, scopeValue] };
65
+ }),
66
+ };
67
+ };
53
68
  const formatPage = page => page || utils_1.PAGE_DEFAULT;
54
69
  const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
55
70
  const formatInclude = (include, associationsMap = {}) => {
@@ -72,8 +87,7 @@ const formatQuery = (query, associationModels) => {
72
87
  formattedScopeMap.set(CUSTOM_FIELDS_FILTER_SCOPE, {});
73
88
  }
74
89
  const scopeKey = queryItemKey.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
75
- formattedScopeMap.get(CUSTOM_FIELDS_FILTER_SCOPE)[scopeKey] =
76
- parseCustomFieldScopeQueryValue(queryItemValue);
90
+ formattedScopeMap.get(CUSTOM_FIELDS_FILTER_SCOPE)[scopeKey] = queryItemValue;
77
91
  return;
78
92
  }
79
93
  const key = utils_1.isAttributeByAssociation(queryItemKey, associationModels)
@@ -103,17 +117,18 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
103
117
  });
104
118
  const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
105
119
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
106
- const formattedOrder = formatOrder({
120
+ const { formattedOrders, orderScopes } = formatOrder({
107
121
  order: [...order, DEFAULT_ORDER],
108
122
  associationModels,
109
123
  });
110
- const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options);
124
+ const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
111
125
  const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
112
126
  const formattedAttribute = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? allAttributes : { include: allAttributes };
113
127
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
114
128
  const formattedPage = formatPage(page);
115
129
  const formattedPerPage = formatPerPage(perPage);
116
130
  const result = formatQuery(query, associationModels);
131
+ const queryScopes = result.formattedScopes;
117
132
  let { formattedQuery } = result;
118
133
  if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
119
134
  const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
@@ -125,6 +140,6 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
125
140
  ],
126
141
  };
127
142
  }
128
- return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: result.formattedScopes }, (formattedAttribute && { attributes: formattedAttribute }));
143
+ return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: [...queryScopes, ...orderScopes] }, (formattedAttribute && { attributes: formattedAttribute }));
129
144
  };
130
145
  exports.default = formatPayload;
@@ -1,20 +1,3 @@
1
1
  export declare const OPERATORS: string[];
2
2
  export declare const OPERATOR_PREFIX = "$";
3
- export declare const OPERATORS_TO_SQL: {
4
- $eq: string;
5
- $ne: string;
6
- $gte: string;
7
- $gt: string;
8
- $lte: string;
9
- $lt: string;
10
- $not: string;
11
- $in: string;
12
- $notIn: string;
13
- $is: string;
14
- $like: string;
15
- $iLike: string;
16
- $notLike: string;
17
- $and: string;
18
- $or: string;
19
- };
20
3
  export declare const formatOperators: (Sequelize: any) => {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatOperators = exports.OPERATORS_TO_SQL = exports.OPERATOR_PREFIX = exports.OPERATORS = void 0;
3
+ exports.formatOperators = exports.OPERATOR_PREFIX = exports.OPERATORS = void 0;
4
4
  exports.OPERATORS = [
5
5
  'eq',
6
6
  'ne',
@@ -22,23 +22,6 @@ exports.OPERATORS = [
22
22
  'contains',
23
23
  ];
24
24
  exports.OPERATOR_PREFIX = '$';
25
- exports.OPERATORS_TO_SQL = {
26
- $eq: '=',
27
- $ne: '!=',
28
- $gte: '>=',
29
- $gt: '>',
30
- $lte: '<=',
31
- $lt: '<',
32
- $not: 'NOT',
33
- $in: 'IN',
34
- $notIn: 'NOT IN',
35
- $is: 'IS',
36
- $like: 'LIKE',
37
- $iLike: 'ILIKE',
38
- $notLike: 'NOT LIKE',
39
- $and: 'AND',
40
- $or: 'OR',
41
- };
42
25
  exports.formatOperators = (Sequelize) => {
43
26
  const { Op } = Sequelize;
44
27
  return exports.OPERATORS.reduce((map, o) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.3.8-beta.0",
3
+ "version": "1.3.8-beta.1",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -11,23 +11,7 @@
11
11
  "test": "jest --forceExit --runInBand",
12
12
  "test-auto": "jest --watch --runInBand",
13
13
  "linter": "./node_modules/.bin/eslint src/**/*.ts",
14
- "build-to-local-repo": "npm run build && cp -r lib/* ../$REPO/node_modules/$npm_package_name/lib",
15
- "watch": "npm-watch build-to-local-repo"
16
- },
17
- "watch": {
18
- "build-to-local-repo": {
19
- "extensions": [
20
- "js",
21
- "jsx",
22
- "ts",
23
- "tsx",
24
- "css"
25
- ],
26
- "patterns": [
27
- "src"
28
- ],
29
- "quiet": false
30
- }
14
+ "build-to-local-repo": "npm run build && cp -r lib/* ../task-ms/node_modules/$npm_package_name/lib"
31
15
  },
32
16
  "jest": {
33
17
  "setupTestFrameworkScriptFile": "jest-extended",
@@ -44,7 +28,7 @@
44
28
  },
45
29
  "homepage": "https://github.com/Autofleet/sheilta",
46
30
  "dependencies": {
47
- "@autofleet/common-types": "^1.7.29",
31
+ "@autofleet/common-types": "^1.7.42",
48
32
  "@autofleet/errors": "^1.0.10",
49
33
  "joi": "^17.9.2",
50
34
  "lodash": "^4.17.21"