@autofleet/sheilta 1.3.8-beta.1 → 1.3.8

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,6 +24,8 @@ declare const formatPayload: ({ order, page, perPage, include, query, attributes
24
24
  page: any;
25
25
  perPage: any;
26
26
  include: any;
27
- scopes: any[];
27
+ scopes: (string | {
28
+ method: any[];
29
+ })[];
28
30
  };
29
31
  export default formatPayload;
@@ -6,11 +6,20 @@ 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");
9
10
  const DEFAULT_ORDER = 'id';
10
11
  const DESCENDING_KEY = 'DESC';
11
- const ASCENDING_KEY = 'ASC';
12
12
  const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
13
- const { CUSTOM_FIELDS_FILTER_SCOPE, CUSTOM_FIELDS_SORT_SCOPE } = common_types_1.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
+ };
14
23
  const getAttributeFromOrder = (order, options = {}) => {
15
24
  const { literalAttributes = [], DBFormatter = undefined } = options;
16
25
  const [formattedOrder, attributes] = order.reduce((acc, o) => {
@@ -27,44 +36,20 @@ const getAttributeFromOrder = (order, options = {}) => {
27
36
  }, [[], []]);
28
37
  return [formattedOrder, attributes];
29
38
  };
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
- };
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
+ });
68
53
  const formatPage = page => page || utils_1.PAGE_DEFAULT;
69
54
  const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
70
55
  const formatInclude = (include, associationsMap = {}) => {
@@ -87,7 +72,8 @@ const formatQuery = (query, associationModels) => {
87
72
  formattedScopeMap.set(CUSTOM_FIELDS_FILTER_SCOPE, {});
88
73
  }
89
74
  const scopeKey = queryItemKey.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
90
- formattedScopeMap.get(CUSTOM_FIELDS_FILTER_SCOPE)[scopeKey] = queryItemValue;
75
+ formattedScopeMap.get(CUSTOM_FIELDS_FILTER_SCOPE)[scopeKey] =
76
+ parseCustomFieldScopeQueryValue(queryItemValue);
91
77
  return;
92
78
  }
93
79
  const key = utils_1.isAttributeByAssociation(queryItemKey, associationModels)
@@ -117,18 +103,17 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
117
103
  });
118
104
  const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
119
105
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
120
- const { formattedOrders, orderScopes } = formatOrder({
106
+ const formattedOrder = formatOrder({
121
107
  order: [...order, DEFAULT_ORDER],
122
108
  associationModels,
123
109
  });
124
- const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
110
+ const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options);
125
111
  const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
126
112
  const formattedAttribute = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? allAttributes : { include: allAttributes };
127
113
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
128
114
  const formattedPage = formatPage(page);
129
115
  const formattedPerPage = formatPerPage(perPage);
130
116
  const result = formatQuery(query, associationModels);
131
- const queryScopes = result.formattedScopes;
132
117
  let { formattedQuery } = result;
133
118
  if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
134
119
  const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
@@ -140,6 +125,6 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
140
125
  ],
141
126
  };
142
127
  }
143
- return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: [...queryScopes, ...orderScopes] }, (formattedAttribute && { attributes: formattedAttribute }));
128
+ return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: result.formattedScopes }, (formattedAttribute && { attributes: formattedAttribute }));
144
129
  };
145
130
  exports.default = formatPayload;
@@ -1,3 +1,20 @@
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
+ };
3
20
  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.OPERATOR_PREFIX = exports.OPERATORS = void 0;
3
+ exports.formatOperators = exports.OPERATORS_TO_SQL = exports.OPERATOR_PREFIX = exports.OPERATORS = void 0;
4
4
  exports.OPERATORS = [
5
5
  'eq',
6
6
  'ne',
@@ -22,6 +22,23 @@ 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
+ };
25
42
  exports.formatOperators = (Sequelize) => {
26
43
  const { Op } = Sequelize;
27
44
  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.1",
3
+ "version": "1.3.8",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -11,7 +11,23 @@
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/* ../task-ms/node_modules/$npm_package_name/lib"
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
+ }
15
31
  },
16
32
  "jest": {
17
33
  "setupTestFrameworkScriptFile": "jest-extended",
@@ -28,7 +44,7 @@
28
44
  },
29
45
  "homepage": "https://github.com/Autofleet/sheilta",
30
46
  "dependencies": {
31
- "@autofleet/common-types": "^1.7.42",
47
+ "@autofleet/common-types": "^1.7.29",
32
48
  "@autofleet/errors": "^1.0.10",
33
49
  "joi": "^17.9.2",
34
50
  "lodash": "^4.17.21"