@autofleet/sheilta 1.2.3 → 1.2.4

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.
@@ -1,11 +1,13 @@
1
1
  declare type OrderItem = string | [string, string];
2
2
  declare type SequelizeOrder = string | OrderItem[];
3
- declare const formatPayload: ({ order, page, perPage, include, query, }: {
3
+ declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
4
4
  order: any;
5
5
  page?: number;
6
6
  perPage?: number;
7
7
  include?: any[];
8
8
  query?: {};
9
+ attributes?: any;
10
+ searchTerm?: any;
9
11
  }, model?: any) => {
10
12
  query: {};
11
13
  order: SequelizeOrder[];
@@ -30,7 +30,14 @@ const formatInclude = (include, associationsMap = {}) => {
30
30
  };
31
31
  const formatQuery = (query, associationModels) => Object.keys(query).reduce((acc, queryItemKey) => (Object.assign(Object.assign({}, acc), { [utils_1.isAttributeByAssociation(queryItemKey, associationModels)
32
32
  ? utils_1.wrapAttributeWithOperator(queryItemKey) : queryItemKey]: query[queryItemKey] })), {});
33
- const formatPayload = ({ order, page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, }, model) => {
33
+ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
34
+ $or: attributesToSend.filter(attrKey => rawAttributes[attrKey].type.key === 'STRING').map(attr => ({
35
+ [attr]: {
36
+ $iLike: `%${searchTerm}%`,
37
+ },
38
+ })),
39
+ });
40
+ const formatPayload = ({ order, page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model) => {
34
41
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
35
42
  const formattedOrder = formatOrder({
36
43
  order,
@@ -39,7 +46,17 @@ const formatPayload = ({ order, page = utils_1.PAGE_DEFAULT, perPage = utils_1.P
39
46
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
40
47
  const formattedPage = formatPage(page);
41
48
  const formattedPerPage = formatPerPage(perPage);
42
- const formattedQuery = formatQuery(query, associationModels);
49
+ let formattedQuery = formatQuery(query, associationModels);
50
+ if (searchTerm) {
51
+ const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
52
+ const queryWithSearchTerm = formatSearchTerm(searchTerm, attributesToSend, model.rawAttributes);
53
+ formattedQuery = lodash_1.default.isEmpty(formattedQuery) ? queryWithSearchTerm : {
54
+ $and: [
55
+ formattedQuery,
56
+ queryWithSearchTerm,
57
+ ],
58
+ };
59
+ }
43
60
  return {
44
61
  query: formattedQuery,
45
62
  order: formattedOrder,
@@ -39,6 +39,42 @@ describe('formatting test', () => {
39
39
  });
40
40
  expect(JSON.stringify(query)).toBe(JSON.stringify({ '$status.field$': { $gt: 4 } }));
41
41
  });
42
+ it('query with searchTerm', () => {
43
+ const searchTerm = 'aviv';
44
+ const { query } = _1.default({
45
+ order: [],
46
+ query: {
47
+ 'json.field': {
48
+ $gt: 4,
49
+ },
50
+ },
51
+ searchTerm,
52
+ }, {
53
+ rawAttributes: {
54
+ startTime: {
55
+ type: {
56
+ key: 'STRING',
57
+ },
58
+ },
59
+ endTime: {
60
+ type: {
61
+ key: 'NUMBER',
62
+ },
63
+ },
64
+ },
65
+ });
66
+ expect(JSON.stringify(query)).toBe(JSON.stringify({
67
+ $and: [{ 'json.field': { $gt: 4 } }, {
68
+ $or: [
69
+ {
70
+ startTime: {
71
+ $iLike: `%${searchTerm}%`,
72
+ },
73
+ },
74
+ ],
75
+ }],
76
+ }));
77
+ });
42
78
  });
43
79
  describe('order formatting', () => {
44
80
  it('simple check', () => {
@@ -42,13 +42,15 @@ exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, n
42
42
  }
43
43
  });
44
44
  exports.newQueryFormatMiddleware = (inner = 'body') => model => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
45
- const { order, page, perPage, include, query, } = req[inner];
45
+ const { order, page, perPage, include, query, attributes, searchTerm, } = req[inner];
46
46
  const { query: formattedQuery, order: formattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, } = formatter_1.default({
47
47
  query,
48
48
  order,
49
49
  page,
50
50
  perPage,
51
51
  include,
52
+ attributes,
53
+ searchTerm,
52
54
  }, model);
53
55
  req[inner].query = formattedQuery;
54
56
  req[inner].order = formattedOrder;
@@ -13,6 +13,7 @@ exports.OPERATORS = [
13
13
  'notIn',
14
14
  'is',
15
15
  'like',
16
+ 'iLike',
16
17
  'notLike',
17
18
  'between',
18
19
  'and',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",