@restorecommerce/resource-base-interface 0.0.9 → 0.2.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.
package/lib/index.js CHANGED
@@ -1,83 +1,183 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GraphResourcesServiceBase = exports.ServiceBase = exports.ResourcesAPIBase = exports.toObject = exports.toStruct = void 0;
3
+ exports.OperatorType = exports.FilterValueType = exports.FilterOperation = exports.GraphResourcesServiceBase = exports.ServiceBase = exports.ResourcesAPIBase = exports.toObject = void 0;
4
4
  const _ = require("lodash");
5
- const toStruct = (obj, fromArray = false) => {
6
- const decode = (value) => {
7
- let decodedVal;
8
- if (_.isNumber(value)) {
9
- decodedVal = { number_value: value };
10
- }
11
- else if (_.isString(value)) {
12
- decodedVal = { string_value: value };
5
+ const filterOperationMap = new Map([
6
+ [0, 'eq'],
7
+ [1, 'lt'],
8
+ [2, 'lte'],
9
+ [3, 'gt'],
10
+ [4, 'gte'],
11
+ [5, 'isEmpty'],
12
+ [6, 'iLike'],
13
+ [7, 'in'],
14
+ [8, 'neq']
15
+ ]);
16
+ const filterOperatorMap = new Map([
17
+ [0, 'and'],
18
+ [1, 'or']
19
+ ]);
20
+ /**
21
+ * Takes filter object containing field, operation and value and inserts it
22
+ * to the obj using the operatorList for finding the last operator position and updates obj
23
+ * @param {filter} filter object containing field, operation, value and type
24
+ * @param {obj} obj converted filter object
25
+ * @param {operatorList} operatorList list of operators from original filter object
26
+ */
27
+ const convertFilterToObject = (filter, obj, operatorList) => {
28
+ let temp = _.clone(obj);
29
+ let value;
30
+ if (!filter.type || filter.type === 'STRING' || filter.type === 0) {
31
+ value = filter.value;
32
+ }
33
+ else if ((filter.type === 'NUMBER' || filter.type === 1) && !isNaN(filter.value)) {
34
+ value = Number(filter.value);
35
+ }
36
+ else if (filter.type === 'BOOLEAN' || filter.type === 2) {
37
+ if (filter.value === 'true') {
38
+ value = true;
13
39
  }
14
- else if (_.isBoolean(value)) {
15
- decodedVal = { bool_value: value };
40
+ else if (filter.value === 'false') {
41
+ value = false;
16
42
  }
17
- else if (_.isArray(value)) {
18
- decodedVal = {
19
- list_value: {
20
- values: _.map(value, (v) => {
21
- return exports.toStruct(v, true);
22
- })
23
- }
24
- };
43
+ }
44
+ else if (filter.type === 'ARRAY' || filter.type === 4) {
45
+ try {
46
+ value = JSON.parse(filter.value);
25
47
  }
26
- else if (_.isObject(value)) {
27
- decodedVal = { struct_value: exports.toStruct(value) };
48
+ catch (err) {
49
+ // to handle JSON string parse error
50
+ if (err.message.indexOf('Unexpected token') > -1) {
51
+ value = JSON.parse(JSON.stringify(filter.value));
52
+ }
53
+ else {
54
+ throw err;
55
+ }
28
56
  }
29
- return decodedVal;
30
- };
31
- let struct;
32
- // fromArray flag is true when iterating
33
- // objects inside a JSON array
34
- if (!fromArray) {
35
- struct = {
36
- fields: {},
37
- };
38
- _.forEach(obj, (value, key) => {
39
- struct.fields[key] = decode(value);
40
- });
41
57
  }
42
- else {
43
- struct = decode(obj);
58
+ else if (filter.type === 'DATE' || filter.type === 3) {
59
+ value = (new Date(filter.value)).getTime();
60
+ }
61
+ for (let i = 0; i < operatorList.length; i++) {
62
+ if (_.isArray(temp)) {
63
+ temp = _.find(temp, operatorList[i]);
64
+ }
65
+ else {
66
+ temp = temp[operatorList[i]];
67
+ }
68
+ if (i === (operatorList.length - 1)) {
69
+ // push for final element in the operatorList array
70
+ if (filter.operation === 'eq' || filter.operation === 0) {
71
+ if (_.isArray(temp)) {
72
+ temp.push({ [filter.field]: value });
73
+ }
74
+ else {
75
+ temp[operatorList[i]].push({ [filter.field]: value });
76
+ }
77
+ }
78
+ else if (filter.operation === 'neq' || filter.operation === 8) {
79
+ if (_.isArray(temp)) {
80
+ temp.push({ [filter.field]: { $not: { $eq: value } } });
81
+ }
82
+ else {
83
+ temp[operatorList[i]].push({ [filter.field]: { $not: { $eq: value } } });
84
+ }
85
+ }
86
+ else {
87
+ let op, opValue;
88
+ if (typeof filter.operation === 'string' || filter.operation instanceof String) {
89
+ opValue = filter.operation;
90
+ }
91
+ else if (Number.isInteger(filter.operation)) {
92
+ opValue = filterOperationMap.get(filter.operation);
93
+ }
94
+ op = `$${opValue}`;
95
+ if (_.isArray(temp)) {
96
+ temp.push({ [filter.field]: { [op]: value } });
97
+ }
98
+ else {
99
+ temp[operatorList[i]].push({ [filter.field]: { [op]: value } });
100
+ }
101
+ }
102
+ }
44
103
  }
45
- return struct;
104
+ return obj;
46
105
  };
47
- exports.toStruct = toStruct;
48
- const decodeValue = (value) => {
49
- let ret = {};
50
- if (!value) {
51
- value = {};
106
+ /**
107
+ * Inserts the new operator into obj iterating throught the operator list and updates obj
108
+ * @param {obj} obj Converted filter object
109
+ * @param {operatorList} operatorList operator list
110
+ * @param {operatorNew} operatorNew new operator
111
+ */
112
+ const insertNewOpAndUpdateObj = (obj, operatorList, operatorNew) => {
113
+ let pos = _.clone(obj);
114
+ for (let i = 0; i < operatorList.length; i++) {
115
+ if (_.isArray(pos)) {
116
+ pos = _.find(pos, operatorList[i]);
117
+ }
118
+ else {
119
+ pos = pos[operatorList[i]];
120
+ }
121
+ // push new operator after iterating to the last element in operatorList
122
+ if (i === (operatorList.length - 1)) {
123
+ pos.push({ [operatorNew]: [] });
124
+ }
52
125
  }
53
- if (value.number_value) {
54
- ret = value.number_value;
126
+ return obj;
127
+ };
128
+ /**
129
+ * toObject takes input contained in the proto structure defined in resource_base proto
130
+ * and converts it into Object understandable by the underlying DB implementation in chassis-srv
131
+ * @param {*} input Original filter input object
132
+ * @param {*} obj converted filter objected passed recursively
133
+ * @param {*} operatorList operatorlist updated and passed recursively
134
+ */
135
+ const toObject = (input, obj, operatorList) => {
136
+ // since toObject method is called recursively we are not adding the typing to input parameter
137
+ let filters;
138
+ if (input && !_.isEmpty(input.filters)) {
139
+ filters = input.filters;
55
140
  }
56
- else if (value.string_value) {
57
- ret = value.string_value;
141
+ else {
142
+ filters = input;
58
143
  }
59
- else if (value.list_value) {
60
- ret = _.map(value.list_value.values, (v) => {
61
- return exports.toObject(v, true); // eslint-disable-line
62
- });
144
+ // const filters = _.cloneDeep( (input.filters && input.filters.length > 0) ? input.filters : input);
145
+ // by default use 'and' operator if no operator is specified
146
+ if (filters && _.isArray(filters.filter) && !filters.operator) {
147
+ filters.operator = 'and';
63
148
  }
64
- else if (value.struct_value) {
65
- ret = exports.toObject(value.struct_value); // eslint-disable-line
149
+ if (!obj) {
150
+ obj = {};
66
151
  }
67
- else if (!_.isNil(value.bool_value)) {
68
- ret = value.bool_value;
152
+ if (_.isArray(filters.filter)) {
153
+ let operatorValue;
154
+ if (typeof filters.operator === 'string' || filters.operator instanceof String) {
155
+ operatorValue = filters.operator;
156
+ }
157
+ else if (Number.isInteger(filters.operator)) {
158
+ operatorValue = filterOperatorMap.get(filters.operator);
159
+ }
160
+ const newOperator = `$${operatorValue}`;
161
+ if (operatorList && newOperator) {
162
+ // insert obj with new operator
163
+ obj = insertNewOpAndUpdateObj(obj, operatorList, newOperator);
164
+ operatorList.push(newOperator);
165
+ }
166
+ else {
167
+ operatorList = [newOperator];
168
+ obj[newOperator] = [];
169
+ }
170
+ // pass operatorList and obj recursively
171
+ (0, exports.toObject)(filters.filter, obj, operatorList);
69
172
  }
70
- return ret;
71
- };
72
- const toObject = (struct, fromArray = false) => {
73
- let obj = {};
74
- if (!fromArray) {
75
- _.forEach(struct.fields, (value, key) => {
76
- obj[key] = decodeValue(value);
77
- });
173
+ else if (_.isArray(filters)) {
174
+ for (let filterObj of filters) {
175
+ (0, exports.toObject)(filterObj, obj, operatorList);
176
+ }
78
177
  }
79
- else {
80
- obj = decodeValue(struct);
178
+ else if (filters.field && (filters.operation || filters.operation === 0) && filters.value) {
179
+ // object contains field, operation and value, update it on obj using convertFilterToObject()
180
+ obj = convertFilterToObject(filters, obj, operatorList);
81
181
  }
82
182
  return obj;
83
183
  };
@@ -88,3 +188,8 @@ const ServiceBase_1 = require("./core/ServiceBase");
88
188
  Object.defineProperty(exports, "ServiceBase", { enumerable: true, get: function () { return ServiceBase_1.ServiceBase; } });
89
189
  const GraphResourcesServiceBase_1 = require("./core/GraphResourcesServiceBase");
90
190
  Object.defineProperty(exports, "GraphResourcesServiceBase", { enumerable: true, get: function () { return GraphResourcesServiceBase_1.GraphResourcesServiceBase; } });
191
+ var interfaces_1 = require("./core/interfaces");
192
+ Object.defineProperty(exports, "FilterOperation", { enumerable: true, get: function () { return interfaces_1.FilterOperation; } });
193
+ Object.defineProperty(exports, "FilterValueType", { enumerable: true, get: function () { return interfaces_1.FilterValueType; } });
194
+ Object.defineProperty(exports, "OperatorType", { enumerable: true, get: function () { return interfaces_1.OperatorType; } });
195
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4BAA4B;AAE5B,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IACjC,CAAC,CAAC,EAAE,IAAI,CAAC;IACT,CAAC,CAAC,EAAE,IAAI,CAAC;IACT,CAAC,CAAC,EAAE,KAAK,CAAC;IACV,CAAC,CAAC,EAAE,IAAI,CAAC;IACT,CAAC,CAAC,EAAE,KAAK,CAAC;IACV,CAAC,CAAC,EAAE,SAAS,CAAC;IACd,CAAC,CAAC,EAAE,OAAO,CAAC;IACZ,CAAC,CAAC,EAAE,IAAI,CAAC;IACT,CAAC,CAAC,EAAE,KAAK,CAAC;CACX,CAAC,CAAC;AAEH,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAChC,CAAC,CAAC,EAAE,KAAK,CAAC;IACV,CAAC,CAAC,EAAE,IAAI,CAAC;CACV,CAAC,CAAC;AAEH;;;;;;GAMG;AACH,MAAM,qBAAqB,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,EAAE;IAC1D,IAAI,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC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,0GAAA,YAAY,OAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restorecommerce/resource-base-interface",
3
- "version": "0.0.9",
3
+ "version": "0.2.1",
4
4
  "description": "Restorecommerce Resource Base Interface",
5
5
  "main": "lib/index.js",
6
6
  "author": "n-fuse GmbH",
@@ -17,34 +17,33 @@
17
17
  "interface"
18
18
  ],
19
19
  "dependencies": {
20
- "@restorecommerce/chassis-srv": "^0.1.12",
21
- "@restorecommerce/grpc-client": "0.0.9",
22
- "@restorecommerce/kafka-client": "^0.1.13",
23
- "@restorecommerce/protos": "^0.0.22",
24
- "@restorecommerce/service-config": "^0.4.1",
20
+ "@restorecommerce/chassis-srv": "^0.3.4",
21
+ "@restorecommerce/grpc-client": "^0.2.12",
22
+ "@restorecommerce/kafka-client": "^0.2.25",
23
+ "@restorecommerce/protos": "^0.4.11",
24
+ "@restorecommerce/service-config": "^0.4.21",
25
25
  "bluebird": "^3.7.2",
26
26
  "lodash": "^4.17.21",
27
- "redis": "^3.0.2"
27
+ "redis": "^3.1.2"
28
28
  },
29
29
  "devDependencies": {
30
- "@types/bluebird": "^3.5.33",
31
- "@types/lodash": "^4.14.168",
32
- "@types/mocha": "^8.2.1",
33
- "@types/redis": "^2.8.28",
34
- "@typescript-eslint/eslint-plugin": "^4.18.0",
35
- "@typescript-eslint/eslint-plugin-tslint": "^4.18.0",
36
- "@typescript-eslint/parser": "^4.18.0",
37
- "coveralls": "^3.1.0",
30
+ "@types/bluebird": "^3.5.36",
31
+ "@types/lodash": "^4.14.178",
32
+ "@types/mocha": "^9.0.0",
33
+ "@types/redis": "^2.8.31",
34
+ "@typescript-eslint/eslint-plugin": "^5.6.0",
35
+ "@typescript-eslint/eslint-plugin-tslint": "^5.6.0",
36
+ "@typescript-eslint/parser": "^5.6.0",
38
37
  "cross-env": "^7.0.3",
39
- "eslint": "^7.22.0",
40
- "eslint-plugin-prefer-arrow-functions": "^3.0.1",
41
- "mocha": "^8.3.2",
38
+ "eslint": "^8.4.1",
39
+ "eslint-plugin-prefer-arrow-functions": "^3.1.4",
40
+ "mocha": "^9.1.3",
42
41
  "npm-run-all": "^4.1.5",
43
42
  "nyc": "^15.1.0",
44
43
  "rimraf": "^3.0.2",
45
44
  "should": "^13.2.3",
46
45
  "tslint": "^6.1.3",
47
- "typescript": "^4.2.3"
46
+ "typescript": "^4.5.2"
48
47
  },
49
48
  "scripts": {
50
49
  "test": "npm run lint && nyc npm run mocha",
@@ -54,7 +53,7 @@
54
53
  "mocha": "cross-env NODE_ENV=test; mocha -R spec ./test/*.js --exit",
55
54
  "test-debug": "npm run lint && npm run mocha-debug",
56
55
  "mocha-debug": "cross-env NODE_ENV=test; mocha ./test/*_test.js --inspect-brk --exit",
57
- "coveralls": "nyc report --reporter=text-lcov | coveralls",
56
+ "lcov-report": "nyc report --reporter=lcov",
58
57
  "build:tsc": "tsc -d",
59
58
  "build:clean": "rimraf lib",
60
59
  "build": "npm-run-all lint build:clean build:tsc"
package/test.js ADDED
@@ -0,0 +1,16 @@
1
+ const test1 = (val) => {
2
+ console.log('T1 is........', val);
3
+ return 1;
4
+ };
5
+ const test2 = (val) => {
6
+ console.log('T2 is...', val);
7
+ return 2;
8
+ };
9
+
10
+ let values = [test1, test2];
11
+
12
+ const prom = async () => {
13
+ return 3;
14
+ };
15
+
16
+ prom().then(...values);
package/testFilter.js ADDED
@@ -0,0 +1,221 @@
1
+ const _ = require('lodash');
2
+
3
+ const neededFilter =
4
+ {
5
+ filters: {
6
+ filter: [
7
+ {
8
+ field: 'id',
9
+ operation: 'eq',
10
+ value: '12345'
11
+ },
12
+ {
13
+ field: 'id',
14
+ operation: 'eq',
15
+ value: '["BAD", "GOOD"]',
16
+ type: 'ARRAY'
17
+ },
18
+ {
19
+ filters: {
20
+ filter: [{
21
+ field: 'firstname',
22
+ operation: 'eq',
23
+ value: 'arun'
24
+ }, {
25
+ field: 'lastname',
26
+ operation: 'eq',
27
+ value: 'hanu'
28
+ }, {
29
+ field: 'middleName',
30
+ operation: 'eq',
31
+ value: 'kumar'
32
+ }],
33
+ operator: 'and'
34
+ },
35
+ }
36
+ ], // Default And case
37
+ operator: 'or'
38
+ }
39
+ };
40
+
41
+ const testCase =
42
+ {
43
+ filters: [{
44
+ filter: [{
45
+ field: 'value',
46
+ operation: 'gt',
47
+ value: '10',
48
+ type: 'NUMBER',
49
+ filters: []
50
+ }]
51
+ }]
52
+ };
53
+
54
+ /**
55
+ * Takes filter object containing field, operation and value and inserts it
56
+ * to the obj using the operatorList for finding the last operator position and updates obj
57
+ * @param {filter} filter object containing field, operation, value and type
58
+ * @param {obj} obj converted filter object
59
+ * @param {operatorList} operatorList list of operators from original filter object
60
+ */
61
+ const convertFilterToObject = (filter, obj, operatorList) => {
62
+ let temp = _.clone(obj);
63
+ let value;
64
+ if (!filter.type || filter.type === 'STRING') {
65
+ value = filter.value;
66
+ } else if (filter.type === 'NUMBER') {
67
+ value = parseInt(filter.value);
68
+ } else if (filter.type === 'BOOLEAN') {
69
+ if (filter.value === 'true') {
70
+ value = true;
71
+ } else if (filter.value === 'false') {
72
+ value = false;
73
+ }
74
+ } else if (filter.type === 'ARRAY') {
75
+ value = JSON.parse(filter.value);
76
+ }
77
+
78
+ for (let i = 0; i < operatorList.length; i++) {
79
+ if (_.isArray(temp)) {
80
+ temp = _.find(temp, operatorList[i]);
81
+ } else {
82
+ temp = temp[operatorList[i]];
83
+ }
84
+ if (i === (operatorList.length - 1)) {
85
+ // push for final element in the operatorList array
86
+ if (filter.operation === 'eq') {
87
+ if (_.isArray(temp)) {
88
+ temp.push({ [filter.field]: value });
89
+ } else {
90
+ temp[operatorList[i]].push({ [filter.field]: value });
91
+ }
92
+ } else {
93
+ if (_.isArray(temp)) {
94
+ temp.push({ [filter.field]: { [filter.operation]: value } });
95
+ } else {
96
+ temp[operatorList[i]].push({ [filter.field]: { [filter.operation]: value } });
97
+ }
98
+ }
99
+ }
100
+ }
101
+ return obj;
102
+ }
103
+
104
+ /**
105
+ * Inserts the new operator into obj iterating throught the operator list and updates obj
106
+ * @param {obj} obj Converted filter object
107
+ * @param {operatorList} operatorList operator list
108
+ * @param {operatorNew} operatorNew new operator
109
+ */
110
+ const insertNewOpAndUpdateObj = (obj, operatorList, operatorNew) => {
111
+ let pos = _.clone(obj);
112
+ for (let i = 0; i < operatorList.length; i++) {
113
+ if (_.isArray(pos)) {
114
+ pos = _.find(pos, operatorList[i]);
115
+ } else {
116
+ pos = pos[operatorList[i]];
117
+ }
118
+ // push new operator after iterating to the last element in operatorList
119
+ if (i === (operatorList.length - 1)) {
120
+ pos.push({ [operatorNew]: [] });
121
+ }
122
+ }
123
+ return obj;
124
+ };
125
+
126
+ /**
127
+ * toObject takes input contained in the proto structure defined in resource_base proto
128
+ * and converts it into Object understandable by the underlying DB implementation in chassis-srv
129
+ * @param {*} input Original filter input object
130
+ * @param {*} obj converted filter objected passed recursively
131
+ * @param {*} operatorList operatorlist updated and passed recursively
132
+ */
133
+ const toObject = (input, obj, operatorList) => {
134
+ // const filters = _.cloneDeep( (input.filters && input.filters.length > 0) ? input.filters : input);
135
+ let filters;
136
+ if (input && !_.isEmpty(input.filters)) {
137
+ filters = input.filters;
138
+ } else {
139
+ filters = input;
140
+ }
141
+ console.log('INPUT is...', filters);
142
+ // by default use 'and' operator if no operator is specified
143
+ if (filters && _.isArray(filters.filter) && !filters.operator) {
144
+ filters.operator = 'and';
145
+ }
146
+ if (!obj) {
147
+ obj = {};
148
+ }
149
+
150
+ if (_.isArray(filters.filter)) {
151
+ const newOperator = `$${filters.operator}`;
152
+ if (operatorList && newOperator) {
153
+ // insert obj with new operator
154
+ obj = insertNewOpAndUpdateObj(obj, operatorList, newOperator);
155
+ operatorList.push(newOperator);
156
+ } else {
157
+ operatorList = [newOperator];
158
+ obj[newOperator] = [];
159
+ }
160
+ // pass operatorList and obj recursively
161
+ console.log('1...........', filters.filter);
162
+ toObject(filters.filter, obj, operatorList);
163
+ } else if (_.isArray(filters)) {
164
+ console.log('2.............', filters);
165
+ for (let filterObj of filters) {
166
+ console.log('FOV is.......', filterObj);
167
+ toObject(filterObj, obj, operatorList);
168
+ }
169
+ } else if (filters.field && filters.operation && filters.value) {
170
+ // object contains field, operation and value, update it on obj using convertFilterToObject()
171
+ obj = convertFilterToObject(filters, obj, operatorList);
172
+ }
173
+ return obj;
174
+ };
175
+
176
+ const complexFilters =
177
+ {
178
+ filter: [
179
+ {
180
+ field: 'id',
181
+ operation: 'eq',
182
+ value: '12345'
183
+ },
184
+ {
185
+ filters: {
186
+ filter: [{
187
+ field: 'firstname',
188
+ operation: 'eq',
189
+ value: 'arun'
190
+ }, {
191
+ field: 'lastname',
192
+ operation: 'eq',
193
+ value: 'arun'
194
+ }],
195
+ operator: 'or'
196
+ },
197
+ }
198
+ ], // Default And case
199
+ operator: 'and'
200
+ };
201
+
202
+ const simpleFilters = {
203
+ filter: [
204
+ {
205
+ field: 'id',
206
+ operation: 'eq',
207
+ value: '12345'
208
+ },
209
+ {
210
+ field: 'id',
211
+ operation: 'eq',
212
+ value: '56789'
213
+ },
214
+ ], // Default And case
215
+ operator: 'or'
216
+ };
217
+
218
+ const failingTest = { filter: [ { field: 'value', operation: 3, value: '10', type: 'NUMBER' } ] };
219
+
220
+ let convertedFilter = toObject(failingTest);
221
+ console.log('CVF is...', JSON.stringify(convertedFilter));
package/tsconfig.json CHANGED
@@ -10,7 +10,8 @@
10
10
  "outDir": "lib",
11
11
  "typeRoots": [
12
12
  "node_modules/@types"
13
- ]
13
+ ],
14
+ "sourceMap": true
14
15
  },
15
16
  "include": [
16
17
  "./src/**/*.ts"