@autofleet/sadot 0.8.1 → 0.8.2-beta-65630a08.0
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/dist/scopes/filter.js +12 -16
- package/package.json +3 -2
- package/src/scopes/filter.ts +12 -16
- package/tsconfig.json +1 -1
- package/tsconfig.build.json +0 -7
package/dist/scopes/filter.js
CHANGED
|
@@ -10,25 +10,24 @@ const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
|
10
10
|
const isConditionStringArray = (input) => Array.isArray(input) && typeof input[0] === 'string';
|
|
11
11
|
const isBooleanString = (input) => ['true', 'false'].includes(input.toString());
|
|
12
12
|
const isDate = (input) => input instanceof Date || Object.prototype.toString.call(input) === '[object Date]';
|
|
13
|
-
const
|
|
14
|
-
const castValueToJsonbText = (value) => castValueToJsonb(value, 'text');
|
|
15
|
-
const castValueToJsonbBoolean = (value) => castValueToJsonb(value, 'boolean');
|
|
16
|
-
const castValueToJsonbNumeric = (value) => castValueToJsonb(value, 'numeric');
|
|
17
|
-
const castIfNeeded = (columnName, conditionValue) => {
|
|
13
|
+
const castIfNeeded = (conditionValue) => {
|
|
18
14
|
if (isDate(conditionValue)) {
|
|
19
|
-
return
|
|
15
|
+
return '::timestamp';
|
|
20
16
|
}
|
|
21
17
|
if (!Number.isNaN(Number(conditionValue))) {
|
|
22
|
-
return
|
|
18
|
+
return '::numeric';
|
|
23
19
|
}
|
|
24
|
-
return
|
|
20
|
+
return '';
|
|
25
21
|
};
|
|
26
22
|
const AND_DELIMITER = ' AND ';
|
|
27
23
|
const OR_DELIMITER = ' OR ';
|
|
28
24
|
const CD_TABLE_ALIAS = 'cd';
|
|
29
25
|
const CD_NAME_COLUMN = `${CD_TABLE_ALIAS}.name`;
|
|
30
26
|
const CV_TABLE_ALIAS = 'cv';
|
|
31
|
-
const CV_VALUE_COLUMN =
|
|
27
|
+
const CV_VALUE_COLUMN = `(${CV_TABLE_ALIAS}.value)`;
|
|
28
|
+
const castValueToJsonb = (value, type) => `to_jsonb(${value}::${type})`;
|
|
29
|
+
const castValueToJsonbText = (value) => castValueToJsonb(value, 'text');
|
|
30
|
+
const castValueToJsonbBoolean = (value) => castValueToJsonb(value, 'boolean');
|
|
32
31
|
/**
|
|
33
32
|
* A Sequelize scope for filtering models by custom fields.
|
|
34
33
|
* This scope builds a WHERE clause to be applied on the main query.
|
|
@@ -58,9 +57,6 @@ const customFieldsFilterScope = (name) => ({ replacementsMap: replacements, scop
|
|
|
58
57
|
if (isBooleanString(v)) {
|
|
59
58
|
return [castValueToJsonbText(`:${valRandom}`), castValueToJsonbBoolean(`:${valRandom}`)];
|
|
60
59
|
}
|
|
61
|
-
if (!Number.isNaN(Number(v))) {
|
|
62
|
-
return castValueToJsonbNumeric(`:${valRandom}`);
|
|
63
|
-
}
|
|
64
60
|
return castValueToJsonbText(`:${valRandom}`);
|
|
65
61
|
}).join(',');
|
|
66
62
|
return `(${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN} IN (${values}))`;
|
|
@@ -68,19 +64,19 @@ const customFieldsFilterScope = (name) => ({ replacementsMap: replacements, scop
|
|
|
68
64
|
return condition.map((c) => {
|
|
69
65
|
const valRep = reverseReplacementsMap.get(c.value);
|
|
70
66
|
const valueAsJsonb = castValueToJsonbText(`:${valRep}`);
|
|
71
|
-
return `(${columnCondition}${AND_DELIMITER}${castIfNeeded(
|
|
67
|
+
return `(${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN}${castIfNeeded(c.value)} ${c.operator} ${valueAsJsonb})`;
|
|
72
68
|
}).join(AND_DELIMITER);
|
|
73
69
|
}
|
|
74
70
|
if (typeof condition === 'string' || typeof condition === 'number') {
|
|
75
71
|
const conditionRep = reverseReplacementsMap.get(condition);
|
|
76
|
-
const valueAsJsonb =
|
|
72
|
+
const valueAsJsonb = castValueToJsonbText(`:${conditionRep}`);
|
|
77
73
|
const valueAsJsonbBoolean = isBooleanString(condition) ? `${OR_DELIMITER}${CV_VALUE_COLUMN} = ${castValueToJsonbBoolean(`:${conditionRep}`)}` : '';
|
|
78
|
-
return `(${columnCondition}${AND_DELIMITER}(${castIfNeeded(
|
|
74
|
+
return `(${columnCondition}${AND_DELIMITER}(${CV_VALUE_COLUMN}${castIfNeeded(condition)} = ${valueAsJsonb}${valueAsJsonbBoolean}))`;
|
|
79
75
|
}
|
|
80
76
|
if (condition?.operator) {
|
|
81
77
|
const valueRep = reverseReplacementsMap.get(condition.value);
|
|
82
78
|
const valueAsJsonb = castValueToJsonbText(`:${valueRep}`);
|
|
83
|
-
return `( ${columnCondition}${AND_DELIMITER}${castIfNeeded(
|
|
79
|
+
return `( ${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN}${castIfNeeded(condition.value)} ${condition.operator} ${valueAsJsonb})`;
|
|
84
80
|
}
|
|
85
81
|
return false;
|
|
86
82
|
}).filter(Boolean);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2-beta-65630a08.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -50,7 +50,8 @@
|
|
|
50
50
|
"npm-watch": "^0.11.0",
|
|
51
51
|
"ts-jest": "^29.1.2",
|
|
52
52
|
"ts-node": "^8.6.2",
|
|
53
|
-
"typescript": "^5.3.3"
|
|
53
|
+
"typescript": "^5.3.3",
|
|
54
|
+
"typescript-eslint": "^0.0.1-alpha.0"
|
|
54
55
|
},
|
|
55
56
|
"peerDependencies": {
|
|
56
57
|
"@autofleet/sheilta": ">=1.4.0"
|
package/src/scopes/filter.ts
CHANGED
|
@@ -36,25 +36,24 @@ const isConditionStringArray = (input: any): input is string[] => Array.isArray(
|
|
|
36
36
|
const isBooleanString = (input: string): boolean => ['true', 'false'].includes(input.toString());
|
|
37
37
|
const isDate = (input: any): input is Date => input instanceof Date || Object.prototype.toString.call(input) === '[object Date]';
|
|
38
38
|
|
|
39
|
-
const
|
|
40
|
-
const castValueToJsonbText = (value: string) => castValueToJsonb(value, 'text');
|
|
41
|
-
const castValueToJsonbBoolean = (value: string) => castValueToJsonb(value, 'boolean');
|
|
42
|
-
const castValueToJsonbNumeric = (value: string) => castValueToJsonb(value, 'numeric');
|
|
43
|
-
const castIfNeeded = (columnName: string, conditionValue: string): string => {
|
|
39
|
+
const castIfNeeded = (conditionValue: string): string => {
|
|
44
40
|
if (isDate(conditionValue)) {
|
|
45
|
-
return
|
|
41
|
+
return '::timestamp';
|
|
46
42
|
}
|
|
47
43
|
if (!Number.isNaN(Number(conditionValue))) {
|
|
48
|
-
return
|
|
44
|
+
return '::numeric';
|
|
49
45
|
}
|
|
50
|
-
return
|
|
46
|
+
return '';
|
|
51
47
|
};
|
|
52
48
|
const AND_DELIMITER = ' AND ';
|
|
53
49
|
const OR_DELIMITER = ' OR ';
|
|
54
50
|
const CD_TABLE_ALIAS = 'cd';
|
|
55
51
|
const CD_NAME_COLUMN = `${CD_TABLE_ALIAS}.name`;
|
|
56
52
|
const CV_TABLE_ALIAS = 'cv';
|
|
57
|
-
const CV_VALUE_COLUMN =
|
|
53
|
+
const CV_VALUE_COLUMN = `(${CV_TABLE_ALIAS}.value)`;
|
|
54
|
+
const castValueToJsonb = (value: string, type: string) => `to_jsonb(${value}::${type})`;
|
|
55
|
+
const castValueToJsonbText = (value: string) => castValueToJsonb(value, 'text');
|
|
56
|
+
const castValueToJsonbBoolean = (value: string) => castValueToJsonb(value, 'boolean');
|
|
58
57
|
|
|
59
58
|
/**
|
|
60
59
|
* A Sequelize scope for filtering models by custom fields.
|
|
@@ -87,9 +86,6 @@ export const customFieldsFilterScope = (
|
|
|
87
86
|
if (isBooleanString(v)) {
|
|
88
87
|
return [castValueToJsonbText(`:${valRandom}`), castValueToJsonbBoolean(`:${valRandom}`)];
|
|
89
88
|
}
|
|
90
|
-
if (!Number.isNaN(Number(v))) {
|
|
91
|
-
return castValueToJsonbNumeric(`:${valRandom}`);
|
|
92
|
-
}
|
|
93
89
|
return castValueToJsonbText(`:${valRandom}`);
|
|
94
90
|
}).join(',');
|
|
95
91
|
return `(${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN} IN (${values}))`;
|
|
@@ -97,19 +93,19 @@ export const customFieldsFilterScope = (
|
|
|
97
93
|
return condition.map((c) => {
|
|
98
94
|
const valRep = reverseReplacementsMap.get(c.value);
|
|
99
95
|
const valueAsJsonb = castValueToJsonbText(`:${valRep}`);
|
|
100
|
-
return `(${columnCondition}${AND_DELIMITER}${castIfNeeded(
|
|
96
|
+
return `(${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN}${castIfNeeded(c.value)} ${c.operator} ${valueAsJsonb})`;
|
|
101
97
|
}).join(AND_DELIMITER);
|
|
102
98
|
}
|
|
103
99
|
if (typeof condition === 'string' || typeof condition === 'number') {
|
|
104
100
|
const conditionRep = reverseReplacementsMap.get(condition);
|
|
105
|
-
const valueAsJsonb =
|
|
101
|
+
const valueAsJsonb = castValueToJsonbText(`:${conditionRep}`);
|
|
106
102
|
const valueAsJsonbBoolean = isBooleanString(condition) ? `${OR_DELIMITER}${CV_VALUE_COLUMN} = ${castValueToJsonbBoolean(`:${conditionRep}`)}` : '';
|
|
107
|
-
return `(${columnCondition}${AND_DELIMITER}(${castIfNeeded(
|
|
103
|
+
return `(${columnCondition}${AND_DELIMITER}(${CV_VALUE_COLUMN}${castIfNeeded(condition)} = ${valueAsJsonb}${valueAsJsonbBoolean}))`;
|
|
108
104
|
}
|
|
109
105
|
if (condition?.operator) {
|
|
110
106
|
const valueRep = reverseReplacementsMap.get(condition.value);
|
|
111
107
|
const valueAsJsonb = castValueToJsonbText(`:${valueRep}`);
|
|
112
|
-
return `( ${columnCondition}${AND_DELIMITER}${castIfNeeded(
|
|
108
|
+
return `( ${columnCondition}${AND_DELIMITER}${CV_VALUE_COLUMN}${castIfNeeded(condition.value)} ${condition.operator} ${valueAsJsonb})`;
|
|
113
109
|
}
|
|
114
110
|
return false;
|
|
115
111
|
}).filter(Boolean);
|
package/tsconfig.json
CHANGED