@opengis/fastify-table 1.0.92 → 1.0.93
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/Changelog.md +281 -277
- package/crud/controllers/deleteCrud.js +22 -22
- package/crud/controllers/insert.js +58 -58
- package/crud/controllers/update.js +61 -61
- package/crud/funcs/dataDelete.js +19 -19
- package/crud/funcs/dataInsert.js +30 -30
- package/crud/funcs/getAccess.js +53 -53
- package/crud/funcs/getOpt.js +10 -10
- package/crud/funcs/setOpt.js +16 -16
- package/crud/funcs/utils/logChanges.js +71 -71
- package/crud/index.js +36 -36
- package/helper.js +28 -28
- package/index.js +97 -97
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/package.json +26 -26
- package/pg/pgClients.js +20 -20
- package/policy/funcs/checkPolicy.js +83 -83
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/migrations/0.sql +78 -78
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server/templates/table/test.dataset.table.json +28 -28
- package/server/templates/table/test.gis.map.table.json +44 -44
- package/table/controllers/data.js +103 -103
- package/table/controllers/suggest.js +79 -79
- package/table/controllers/table.js +52 -52
- package/table/controllers/utils/getSelectMeta.js +66 -66
- package/table/controllers/utils/getTemplate.js +28 -28
- package/table/controllers/utils/gisIRColumn.js +68 -68
- package/table/funcs/getFilterSQL/index.js +79 -75
- package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
- package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
- package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/table/funcs/metaFormat/getSelectVal.js +20 -20
- package/table/funcs/metaFormat/index.js +28 -28
- package/test/api/crud.test.js +88 -88
- package/test/api/table.test.js +89 -89
- package/test/api/widget.test.js +117 -117
- package/test/funcs/crud.test.js +122 -122
- package/util/controllers/properties.add.js +57 -57
- package/util/controllers/status.monitor.js +8 -8
- package/widget/controllers/utils/historyFormat.js +76 -76
- package/widget/controllers/utils/obj2db.js +13 -13
- package/widget/controllers/widget.del.js +44 -44
- package/widget/controllers/widget.get.js +98 -98
- package/widget/controllers/widget.set.js +76 -76
- package/widget/index.js +40 -40
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
const dateTypeList = ['date', 'timestamp', 'timestamp without time zone'];
|
|
2
|
-
const numberTypeList = ['float8', 'int4', 'int8', 'numeric', 'double precision', 'integer'];
|
|
3
|
-
|
|
4
|
-
function dt(y, m, d) {
|
|
5
|
-
return new Date(Date.UTC(y, m, d)).toISOString().slice(0, 10);
|
|
6
|
-
}
|
|
7
|
-
const dp = {
|
|
8
|
-
d: new Date().getDate(),
|
|
9
|
-
w: new Date().getDate() - (new Date().getDay() || 7) + 1,
|
|
10
|
-
m: new Date().getMonth(),
|
|
11
|
-
q: (new Date().getMonth() / 4).toFixed() * 3,
|
|
12
|
-
y: new Date().getFullYear(),
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
function formatDateISOString(date) {
|
|
16
|
-
if (!date?.includes('.')) return date;
|
|
17
|
-
const [day, month, year] = date.split('.');
|
|
18
|
-
return `${year}-${month}-${day}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function formatValue({
|
|
22
|
-
clsList, filterType, filter, name, value, operator = '=', fieldType = 'text', uid = 1, optimize,
|
|
23
|
-
}) {
|
|
24
|
-
if (!name || !value) return {};
|
|
25
|
-
|
|
26
|
-
// current day, week, month, year etc.
|
|
27
|
-
if (dateTypeList.includes(fieldType) && !value?.includes('_') && ['cd', 'cw', 'cm', 'cq', 'cy'].includes(value)) {
|
|
28
|
-
const query = {
|
|
29
|
-
cd: `${name}::date = '${dt(dp.y, dp.m, dp.d)}'::date`,
|
|
30
|
-
cw: `${name}::date >= '${dt(dp.y, dp.m, dp.w)}'::date and ${name} <= '${dt(dp.y, dp.m, dp.w + 6)}'::date`,
|
|
31
|
-
cm: `${name}::date >= '${dt(dp.y, dp.m, 1)}'::date and ${name} <= '${dt(dp.y, dp.m + 1, 0)}'::date`,
|
|
32
|
-
cq: `${name}::date >= '${dt(dp.y, dp.q, 1)}'::date and ${name} <= '${dt(dp.y, dp.q + 3, 0)}'::date`,
|
|
33
|
-
cy: `${name}::date >= '${dt(dp.y, 0, 1)}'::date and ${name}::date <= '${dt(dp.y, 11, 31)}'::date`,
|
|
34
|
-
}[value];
|
|
35
|
-
return { op: '=', query };
|
|
36
|
-
}
|
|
37
|
-
// date range
|
|
38
|
-
if (dateTypeList.includes(fieldType) && value?.includes('_')) {
|
|
39
|
-
const [min, max] = value.split('_');
|
|
40
|
-
const query = `${name} >= '${min}'::date and ${name} <= '${max}'::date`;
|
|
41
|
-
return { op: 'between', query };
|
|
42
|
-
}
|
|
43
|
-
// v3 filter date range, example - "01.01.2024-31.12.2024"
|
|
44
|
-
if (dateTypeList.includes(fieldType) && value?.includes('.') && value?.indexOf('-') === 10 && value?.length === 21) {
|
|
45
|
-
const [startDate, endDate] = value.split('-');
|
|
46
|
-
const min = formatDateISOString(startDate);
|
|
47
|
-
const max = formatDateISOString(endDate);
|
|
48
|
-
const query = `${name}::date >= '${min}'::date and ${name}::date <= '${max}'::date`;
|
|
49
|
-
return { op: 'between', query };
|
|
50
|
-
}
|
|
51
|
-
// my rows
|
|
52
|
-
if (value === 'me' && uid && fieldType === 'text') {
|
|
53
|
-
return { op: '=', query: `${name}::text = '${uid}'` };
|
|
54
|
-
}
|
|
55
|
-
const formatType = {
|
|
56
|
-
float8: 'numeric',
|
|
57
|
-
int4: 'numeric',
|
|
58
|
-
int8: 'numeric',
|
|
59
|
-
varchar: 'text',
|
|
60
|
-
bool: 'boolean',
|
|
61
|
-
geometry: 'geom',
|
|
62
|
-
}[fieldType] || 'text';
|
|
63
|
-
|
|
64
|
-
if (optimize && optimize.name !== optimize.pk) {
|
|
65
|
-
const val = filterType === 'text' ? `ilike '%${value}%'` : `= any('{${value}}')`;
|
|
66
|
-
return {
|
|
67
|
-
op: '~',
|
|
68
|
-
query: fieldType?.includes('[]')
|
|
69
|
-
? `${optimize.pk} && (select array_agg(${optimize.pk}) from ${optimize.table} where ${name} ${val} )`
|
|
70
|
-
: `${optimize.pk} in (select ${optimize.pk} from ${optimize.table} where ${name} ${val} )`,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (filter.sql) {
|
|
75
|
-
return { op: '~', query: filter.sql.replace('{val}', value) };
|
|
76
|
-
}
|
|
77
|
-
if (fieldType?.includes('[]')) {
|
|
78
|
-
return { op: 'in', query: `'{${value}}'::text[] && ${name}::text[]` };
|
|
79
|
-
}
|
|
80
|
-
// multiple items of 1 param
|
|
81
|
-
if (value?.indexOf(',') !== -1) {
|
|
82
|
-
const values = value.split(',').filter((el) => el !== 'null');
|
|
83
|
-
const query = value?.indexOf('null' !== -1)
|
|
84
|
-
? `( ${name} is null or ${name}::text in (${values?.map((el) => `'${el}'`).join(',')}) )`
|
|
85
|
-
: `${name}::text in (${value.split(',')?.map((el) => `'${el}'`).join(',')})`;
|
|
86
|
-
return { op: 'in', query };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// v3 filter number range, example - "100_500"
|
|
90
|
-
if (numberTypeList.includes(fieldType) && value?.indexOf('_') !== -1) {
|
|
91
|
-
const [min, max] = value.split('_');
|
|
92
|
-
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} < ${max}` : null) || `${name} between ${min} and ${max}`;
|
|
93
|
-
return { op: 'between', query };
|
|
94
|
-
}
|
|
95
|
-
// number range
|
|
96
|
-
if (numberTypeList.includes(fieldType) && value?.indexOf('-') !== -1) {
|
|
97
|
-
const [min, max] = value.split('-');
|
|
98
|
-
if (min === 'min' && max === 'max') return {};
|
|
99
|
-
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} < ${max}` : null) || `${name} between ${min} and ${max}`;
|
|
100
|
-
return { op: 'between', query };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (['<', '>'].includes(operator)) {
|
|
104
|
-
const query = `${name} ${operator} '${value}'::${formatType}`;
|
|
105
|
-
return { op: operator, query };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (operator === '=' && filterType !== 'text') {
|
|
109
|
-
const query = {
|
|
110
|
-
null: `${name} is null`,
|
|
111
|
-
notnull: `${name} is not null`,
|
|
112
|
-
}[value] || `${name}::${formatType}='${value}'::${formatType}`;
|
|
113
|
-
return { op: '=', query };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (['~', '='].includes(operator)) {
|
|
117
|
-
const query = clsList[filter?.data]
|
|
118
|
-
? `${filter?.name} in ( ( with q(id,name) as (${clsList?.[filter?.data]}) select id from q where name ilike '%${value}%') )` // filter with cls
|
|
119
|
-
: `${name}::text ilike '%${value}%'`; // simple filter
|
|
120
|
-
return { op: 'ilike', query };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// json
|
|
124
|
-
if (name.includes('.')) {
|
|
125
|
-
const [col, prop] = name.split('.');
|
|
126
|
-
const query = ` ${col}->>'${prop}' in ('${value.join("','")}')`;
|
|
127
|
-
return { op: 'in', query };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// geometry
|
|
131
|
-
if (['geometry'].includes(fieldType)) {
|
|
132
|
-
const bbox = value[0].split('_');
|
|
133
|
-
|
|
134
|
-
if (bbox?.length === 4) {
|
|
135
|
-
const query = ` ${name} && 'box(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d `;
|
|
136
|
-
return { op: '&&', query };
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return {};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export default formatValue;
|
|
1
|
+
const dateTypeList = ['date', 'timestamp', 'timestamp without time zone'];
|
|
2
|
+
const numberTypeList = ['float8', 'int4', 'int8', 'numeric', 'double precision', 'integer'];
|
|
3
|
+
|
|
4
|
+
function dt(y, m, d) {
|
|
5
|
+
return new Date(Date.UTC(y, m, d)).toISOString().slice(0, 10);
|
|
6
|
+
}
|
|
7
|
+
const dp = {
|
|
8
|
+
d: new Date().getDate(),
|
|
9
|
+
w: new Date().getDate() - (new Date().getDay() || 7) + 1,
|
|
10
|
+
m: new Date().getMonth(),
|
|
11
|
+
q: (new Date().getMonth() / 4).toFixed() * 3,
|
|
12
|
+
y: new Date().getFullYear(),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function formatDateISOString(date) {
|
|
16
|
+
if (!date?.includes('.')) return date;
|
|
17
|
+
const [day, month, year] = date.split('.');
|
|
18
|
+
return `${year}-${month}-${day}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function formatValue({
|
|
22
|
+
clsList, filterType, filter, name, value, operator = '=', fieldType = 'text', uid = 1, optimize,
|
|
23
|
+
}) {
|
|
24
|
+
if (!name || !value) return {};
|
|
25
|
+
|
|
26
|
+
// current day, week, month, year etc.
|
|
27
|
+
if (dateTypeList.includes(fieldType) && !value?.includes('_') && ['cd', 'cw', 'cm', 'cq', 'cy'].includes(value)) {
|
|
28
|
+
const query = {
|
|
29
|
+
cd: `${name}::date = '${dt(dp.y, dp.m, dp.d)}'::date`,
|
|
30
|
+
cw: `${name}::date >= '${dt(dp.y, dp.m, dp.w)}'::date and ${name} <= '${dt(dp.y, dp.m, dp.w + 6)}'::date`,
|
|
31
|
+
cm: `${name}::date >= '${dt(dp.y, dp.m, 1)}'::date and ${name} <= '${dt(dp.y, dp.m + 1, 0)}'::date`,
|
|
32
|
+
cq: `${name}::date >= '${dt(dp.y, dp.q, 1)}'::date and ${name} <= '${dt(dp.y, dp.q + 3, 0)}'::date`,
|
|
33
|
+
cy: `${name}::date >= '${dt(dp.y, 0, 1)}'::date and ${name}::date <= '${dt(dp.y, 11, 31)}'::date`,
|
|
34
|
+
}[value];
|
|
35
|
+
return { op: '=', query };
|
|
36
|
+
}
|
|
37
|
+
// date range
|
|
38
|
+
if (dateTypeList.includes(fieldType) && value?.includes('_')) {
|
|
39
|
+
const [min, max] = value.split('_');
|
|
40
|
+
const query = `${name} >= '${min}'::date and ${name} <= '${max}'::date`;
|
|
41
|
+
return { op: 'between', query };
|
|
42
|
+
}
|
|
43
|
+
// v3 filter date range, example - "01.01.2024-31.12.2024"
|
|
44
|
+
if (dateTypeList.includes(fieldType) && value?.includes('.') && value?.indexOf('-') === 10 && value?.length === 21) {
|
|
45
|
+
const [startDate, endDate] = value.split('-');
|
|
46
|
+
const min = formatDateISOString(startDate);
|
|
47
|
+
const max = formatDateISOString(endDate);
|
|
48
|
+
const query = `${name}::date >= '${min}'::date and ${name}::date <= '${max}'::date`;
|
|
49
|
+
return { op: 'between', query };
|
|
50
|
+
}
|
|
51
|
+
// my rows
|
|
52
|
+
if (value === 'me' && uid && fieldType === 'text') {
|
|
53
|
+
return { op: '=', query: `${name}::text = '${uid}'` };
|
|
54
|
+
}
|
|
55
|
+
const formatType = {
|
|
56
|
+
float8: 'numeric',
|
|
57
|
+
int4: 'numeric',
|
|
58
|
+
int8: 'numeric',
|
|
59
|
+
varchar: 'text',
|
|
60
|
+
bool: 'boolean',
|
|
61
|
+
geometry: 'geom',
|
|
62
|
+
}[fieldType] || 'text';
|
|
63
|
+
|
|
64
|
+
if (optimize && optimize.name !== optimize.pk) {
|
|
65
|
+
const val = filterType === 'text' ? `ilike '%${value}%'` : `= any('{${value}}')`;
|
|
66
|
+
return {
|
|
67
|
+
op: '~',
|
|
68
|
+
query: fieldType?.includes('[]')
|
|
69
|
+
? `${optimize.pk} && (select array_agg(${optimize.pk}) from ${optimize.table} where ${name} ${val} )`
|
|
70
|
+
: `${optimize.pk} in (select ${optimize.pk} from ${optimize.table} where ${name} ${val} )`,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (filter.sql) {
|
|
75
|
+
return { op: '~', query: filter.sql.replace('{val}', value) };
|
|
76
|
+
}
|
|
77
|
+
if (fieldType?.includes('[]')) {
|
|
78
|
+
return { op: 'in', query: `'{${value}}'::text[] && ${name}::text[]` };
|
|
79
|
+
}
|
|
80
|
+
// multiple items of 1 param
|
|
81
|
+
if (value?.indexOf(',') !== -1) {
|
|
82
|
+
const values = value.split(',').filter((el) => el !== 'null');
|
|
83
|
+
const query = value?.indexOf('null' !== -1)
|
|
84
|
+
? `( ${name} is null or ${name}::text in (${values?.map((el) => `'${el}'`).join(',')}) )`
|
|
85
|
+
: `${name}::text in (${value.split(',')?.map((el) => `'${el}'`).join(',')})`;
|
|
86
|
+
return { op: 'in', query };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// v3 filter number range, example - "100_500"
|
|
90
|
+
if (numberTypeList.includes(fieldType) && value?.indexOf('_') !== -1) {
|
|
91
|
+
const [min, max] = value.split('_');
|
|
92
|
+
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} < ${max}` : null) || `${name} between ${min} and ${max}`;
|
|
93
|
+
return { op: 'between', query };
|
|
94
|
+
}
|
|
95
|
+
// number range
|
|
96
|
+
if (numberTypeList.includes(fieldType) && value?.indexOf('-') !== -1) {
|
|
97
|
+
const [min, max] = value.split('-');
|
|
98
|
+
if (min === 'min' && max === 'max') return {};
|
|
99
|
+
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} < ${max}` : null) || `${name} between ${min} and ${max}`;
|
|
100
|
+
return { op: 'between', query };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (['<', '>'].includes(operator)) {
|
|
104
|
+
const query = `${name} ${operator} '${value}'::${formatType}`;
|
|
105
|
+
return { op: operator, query };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (operator === '=' && filterType !== 'text') {
|
|
109
|
+
const query = {
|
|
110
|
+
null: `${name} is null`,
|
|
111
|
+
notnull: `${name} is not null`,
|
|
112
|
+
}[value] || `${name}::${formatType}='${value}'::${formatType}`;
|
|
113
|
+
return { op: '=', query };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (['~', '='].includes(operator)) {
|
|
117
|
+
const query = clsList[filter?.data]
|
|
118
|
+
? `${filter?.name} in ( ( with q(id,name) as (${clsList?.[filter?.data]}) select id from q where name ilike '%${value}%') )` // filter with cls
|
|
119
|
+
: `${name}::text ilike '%${value}%'`; // simple filter
|
|
120
|
+
return { op: 'ilike', query };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// json
|
|
124
|
+
if (name.includes('.')) {
|
|
125
|
+
const [col, prop] = name.split('.');
|
|
126
|
+
const query = ` ${col}->>'${prop}' in ('${value.join("','")}')`;
|
|
127
|
+
return { op: 'in', query };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// geometry
|
|
131
|
+
if (['geometry'].includes(fieldType)) {
|
|
132
|
+
const bbox = value[0].split('_');
|
|
133
|
+
|
|
134
|
+
if (bbox?.length === 4) {
|
|
135
|
+
const query = ` ${name} && 'box(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d `;
|
|
136
|
+
return { op: '&&', query };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export default formatValue;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
async function getCustomQuery({
|
|
2
|
-
pg, table, customFilter,
|
|
3
|
-
}) {
|
|
4
|
-
if (!customFilter) return null;
|
|
5
|
-
const customFilterList = customFilter?.split(',')?.map((el) => el?.split('_').pop());
|
|
6
|
-
const { property_json: customFilterSQL } = await pg.one(`select json_agg(json_build_object('id',property_id,'name',property_key,'query',property_text)
|
|
7
|
-
) as property_json from setting.property where property_key is not null and property_entity='customQuery' and object_id=$1`, [table]);
|
|
8
|
-
const data = customFilterSQL?.length ? customFilterSQL.filter((el) => customFilterList.includes(el.id)) || [] : [];
|
|
9
|
-
const customQuery = data?.map((el) => el.query).join(' and ');
|
|
10
|
-
return `${customQuery}`;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default getCustomQuery;
|
|
1
|
+
async function getCustomQuery({
|
|
2
|
+
pg, table, customFilter,
|
|
3
|
+
}) {
|
|
4
|
+
if (!customFilter) return null;
|
|
5
|
+
const customFilterList = customFilter?.split(',')?.map((el) => el?.split('_').pop());
|
|
6
|
+
const { property_json: customFilterSQL } = await pg.one(`select json_agg(json_build_object('id',property_id,'name',property_key,'query',property_text)
|
|
7
|
+
) as property_json from setting.property where property_key is not null and property_entity='customQuery' and object_id=$1`, [table]);
|
|
8
|
+
const data = customFilterSQL?.length ? customFilterSQL.filter((el) => customFilterList.includes(el.id)) || [] : [];
|
|
9
|
+
const customQuery = data?.map((el) => el.query).join(' and ');
|
|
10
|
+
return `${customQuery}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default getCustomQuery;
|
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
/* eslint-disable no-continue */
|
|
2
|
-
/**
|
|
3
|
-
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
4
|
-
* @param {String} opt.query - запит до таблиці
|
|
5
|
-
* @param {String} opt.hash - інформація з хешу по запиту
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import formatValue from './formatValue.js';
|
|
9
|
-
|
|
10
|
-
function getQuery({
|
|
11
|
-
filter: filterStr, tableSQL, fields, filterList, config = {}, pg,
|
|
12
|
-
}) {
|
|
13
|
-
if (!filterStr) return null; // filter list API
|
|
14
|
-
|
|
15
|
-
const mainOperators = ['=', '~', '>', '<'];
|
|
16
|
-
|
|
17
|
-
// v3 filter
|
|
18
|
-
const { allTemplates } = config;
|
|
19
|
-
|
|
20
|
-
const filterQueryArray = config.v3?.filter
|
|
21
|
-
? decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/)
|
|
22
|
-
: decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')?.replace(/,null/g, '')).replace(/'/g, '').split(/[;|]/);
|
|
23
|
-
|
|
24
|
-
const resultList = [];
|
|
25
|
-
|
|
26
|
-
for (let i = 0; i < filterQueryArray.length; i += 1) {
|
|
27
|
-
const item = filterQueryArray[i];
|
|
28
|
-
const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
|
|
29
|
-
const [name] = item.split(operator);
|
|
30
|
-
|
|
31
|
-
// skip already added filter
|
|
32
|
-
if (resultList.find((el) => el.name === name)) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// find all value
|
|
37
|
-
const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
|
|
38
|
-
|
|
39
|
-
const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
|
|
40
|
-
|
|
41
|
-
// find field and skip not exists
|
|
42
|
-
const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
|
|
43
|
-
if (!dataTypeID) continue;
|
|
44
|
-
|
|
45
|
-
const type = pg.pgType?.[dataTypeID];
|
|
46
|
-
|
|
47
|
-
// filter
|
|
48
|
-
const filter = filterList?.find((el) => el.id === name) || { type: 'text' };
|
|
49
|
-
const filterType = filter.type?.toLowerCase();
|
|
50
|
-
// format query
|
|
51
|
-
|
|
52
|
-
const { op, query } = formatValue({
|
|
53
|
-
clsList: allTemplates?.cls || [],
|
|
54
|
-
filter,
|
|
55
|
-
optimize,
|
|
56
|
-
filterType,
|
|
57
|
-
name,
|
|
58
|
-
value: decodeURIComponent(value), // decodeURIComponent(value)?.replace(new RegExp(String.raw`\b${name}=\b`, 'g'), '') for checkboxes?
|
|
59
|
-
operator,
|
|
60
|
-
fieldType: type || 'text',
|
|
61
|
-
}) || {};
|
|
62
|
-
// console.log({ query, value });
|
|
63
|
-
if (!query) continue;
|
|
64
|
-
|
|
65
|
-
resultList.push({
|
|
66
|
-
name, value, query, operator: op, filterType, type,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return resultList;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export default getQuery;
|
|
1
|
+
/* eslint-disable no-continue */
|
|
2
|
+
/**
|
|
3
|
+
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
4
|
+
* @param {String} opt.query - запит до таблиці
|
|
5
|
+
* @param {String} opt.hash - інформація з хешу по запиту
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import formatValue from './formatValue.js';
|
|
9
|
+
|
|
10
|
+
function getQuery({
|
|
11
|
+
filter: filterStr, tableSQL, fields, filterList, config = {}, pg,
|
|
12
|
+
}) {
|
|
13
|
+
if (!filterStr) return null; // filter list API
|
|
14
|
+
|
|
15
|
+
const mainOperators = ['=', '~', '>', '<'];
|
|
16
|
+
|
|
17
|
+
// v3 filter
|
|
18
|
+
const { allTemplates } = config;
|
|
19
|
+
|
|
20
|
+
const filterQueryArray = config.v3?.filter
|
|
21
|
+
? decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/)
|
|
22
|
+
: decodeURI(filterStr?.replace(/(^,)|(,$)/g, '')?.replace(/,null/g, '')).replace(/'/g, '').split(/[;|]/);
|
|
23
|
+
|
|
24
|
+
const resultList = [];
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < filterQueryArray.length; i += 1) {
|
|
27
|
+
const item = filterQueryArray[i];
|
|
28
|
+
const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
|
|
29
|
+
const [name] = item.split(operator);
|
|
30
|
+
|
|
31
|
+
// skip already added filter
|
|
32
|
+
if (resultList.find((el) => el.name === name)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// find all value
|
|
37
|
+
const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
|
|
38
|
+
|
|
39
|
+
const optimize = fields?.find((el) => el.name === name) ? null : tableSQL.find((el) => el.name === name);
|
|
40
|
+
|
|
41
|
+
// find field and skip not exists
|
|
42
|
+
const { dataTypeID } = fields?.find((el) => el.name === name) || fields?.find((el) => el.name === optimize?.pk) || {};
|
|
43
|
+
if (!dataTypeID) continue;
|
|
44
|
+
|
|
45
|
+
const type = pg.pgType?.[dataTypeID];
|
|
46
|
+
|
|
47
|
+
// filter
|
|
48
|
+
const filter = filterList?.find((el) => el.id === name) || { type: 'text' };
|
|
49
|
+
const filterType = filter.type?.toLowerCase();
|
|
50
|
+
// format query
|
|
51
|
+
|
|
52
|
+
const { op, query } = formatValue({
|
|
53
|
+
clsList: allTemplates?.cls || [],
|
|
54
|
+
filter,
|
|
55
|
+
optimize,
|
|
56
|
+
filterType,
|
|
57
|
+
name,
|
|
58
|
+
value: decodeURIComponent(value), // decodeURIComponent(value)?.replace(new RegExp(String.raw`\b${name}=\b`, 'g'), '') for checkboxes?
|
|
59
|
+
operator,
|
|
60
|
+
fieldType: type || 'text',
|
|
61
|
+
}) || {};
|
|
62
|
+
// console.log({ query, value });
|
|
63
|
+
if (!query) continue;
|
|
64
|
+
|
|
65
|
+
resultList.push({
|
|
66
|
+
name, value, query, operator: op, filterType, type,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return resultList;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default getQuery;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
function getOptimizedQuery({ body, table, q }, count) {
|
|
2
|
-
const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
|
|
3
|
-
|
|
4
|
-
const tableName = body?.table || body?.model || table;
|
|
5
|
-
|
|
6
|
-
const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
|
|
7
|
-
.map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
|
|
8
|
-
|
|
9
|
-
return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default getOptimizedQuery;
|
|
1
|
+
function getOptimizedQuery({ body, table, q }, count) {
|
|
2
|
+
const order = body?.orderby || body?.order ? `order by ${body?.orderby || body?.order}` : '';
|
|
3
|
+
|
|
4
|
+
const tableName = body?.table || body?.model || table;
|
|
5
|
+
|
|
6
|
+
const sqlList = body?.sql?.filter((el) => !el.disabled && el?.sql?.replace && (count ? el.count !== false : true))
|
|
7
|
+
.map((el) => ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `).join(' ');
|
|
8
|
+
|
|
9
|
+
return `(select * from ${tableName} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 and ${q?.replace('q.', 't.') || '1=1'} ${order})q`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default getOptimizedQuery;
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
function getTable(table) {
|
|
2
|
-
const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
3
|
-
?.map((el) => el.split(/[ )]/)[0]);
|
|
4
|
-
return result;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
9
|
-
* @param {String} opt.query - запит до таблиці
|
|
10
|
-
* @param {String} opt.hash - інформація з хешу по запиту
|
|
11
|
-
*/
|
|
12
|
-
const tableSql = {};
|
|
13
|
-
async function getTableSql({
|
|
14
|
-
pg, body, table, fields,
|
|
15
|
-
}) {
|
|
16
|
-
if (tableSql[table]) return tableSql[table];
|
|
17
|
-
|
|
18
|
-
const fieldList = fields.map((el) => el.name);
|
|
19
|
-
|
|
20
|
-
const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
|
|
21
|
-
|
|
22
|
-
if (!tableList) { tableSql[table] = []; return []; }
|
|
23
|
-
|
|
24
|
-
const data = await Promise.all(tableList?.map(async (tableEl) => {
|
|
25
|
-
const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
|
|
26
|
-
return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
|
-
tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
|
|
30
|
-
|
|
31
|
-
return tableSql[table];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default getTableSql;
|
|
1
|
+
function getTable(table) {
|
|
2
|
+
const result = table?.toLowerCase()?.replace(/[\n\r]+/g, ' ')?.split(' from ')?.filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
3
|
+
?.map((el) => el.split(/[ )]/)[0]);
|
|
4
|
+
return result;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
|
9
|
+
* @param {String} opt.query - запит до таблиці
|
|
10
|
+
* @param {String} opt.hash - інформація з хешу по запиту
|
|
11
|
+
*/
|
|
12
|
+
const tableSql = {};
|
|
13
|
+
async function getTableSql({
|
|
14
|
+
pg, body, table, fields,
|
|
15
|
+
}) {
|
|
16
|
+
if (tableSql[table]) return tableSql[table];
|
|
17
|
+
|
|
18
|
+
const fieldList = fields.map((el) => el.name);
|
|
19
|
+
|
|
20
|
+
const tableList = body?.sql?.map((el) => getTable(el.sql)).reduce((acc, el) => acc.concat(el), []).filter((el) => fieldList.includes(pg.pk[el]));
|
|
21
|
+
|
|
22
|
+
if (!tableList) { tableSql[table] = []; return []; }
|
|
23
|
+
|
|
24
|
+
const data = await Promise.all(tableList?.map(async (tableEl) => {
|
|
25
|
+
const { fields: fieldsEl } = await pg.query(`select * from ${tableEl} limit 0`);
|
|
26
|
+
return fieldsEl.map((el) => ({ name: el.name, table: tableEl, pk: pg.pk[tableEl] }));
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
tableSql[table] = data.reduce((acc, el) => acc.concat(el), []);
|
|
30
|
+
|
|
31
|
+
return tableSql[table];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default getTableSql;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import getSelect from '../../controllers/utils/getSelect.js';
|
|
2
|
-
import pg from '../../../pg/pgClients.js';
|
|
3
|
-
import redis from '../../../redis/client.js';
|
|
4
|
-
|
|
5
|
-
export default async function metaFormat({ name, values }) {
|
|
6
|
-
const cls = await getSelect(name);
|
|
7
|
-
if (!cls?.arr && !cls?.sql) return null;
|
|
8
|
-
const key = `select:${name}`;
|
|
9
|
-
const cache = !cls.arr ? (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {}) : {};
|
|
10
|
-
|
|
11
|
-
const data = cls.arr || (values.filter(el => !cache[el]).length
|
|
12
|
-
? await pg.client.query(`with c(id,text) as (${cls.sql}) select * from c where id = any('{${values.filter(el => !cache[el])}}')`).then(el => el.rows)
|
|
13
|
-
: []);
|
|
14
|
-
|
|
15
|
-
const clsAr = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
|
|
16
|
-
if (!cls.arr && data.length) {
|
|
17
|
-
redis.hmset(key, clsAr);
|
|
18
|
-
}
|
|
19
|
-
return clsAr;
|
|
20
|
-
}
|
|
1
|
+
import getSelect from '../../controllers/utils/getSelect.js';
|
|
2
|
+
import pg from '../../../pg/pgClients.js';
|
|
3
|
+
import redis from '../../../redis/client.js';
|
|
4
|
+
|
|
5
|
+
export default async function metaFormat({ name, values }) {
|
|
6
|
+
const cls = await getSelect(name);
|
|
7
|
+
if (!cls?.arr && !cls?.sql) return null;
|
|
8
|
+
const key = `select:${name}`;
|
|
9
|
+
const cache = !cls.arr ? (await redis.hmget(key, values)).reduce((p, el, i) => ({ ...p, [values[i]]: el }), {}) : {};
|
|
10
|
+
|
|
11
|
+
const data = cls.arr || (values.filter(el => !cache[el]).length
|
|
12
|
+
? await pg.client.query(`with c(id,text) as (${cls.sql}) select * from c where id = any('{${values.filter(el => !cache[el])}}')`).then(el => el.rows)
|
|
13
|
+
: []);
|
|
14
|
+
|
|
15
|
+
const clsAr = { ...cache, ...data.reduce((p, el) => ({ ...p, [el.id.toString()]: el.color ? el : el.text }), {}) };
|
|
16
|
+
if (!cls.arr && data.length) {
|
|
17
|
+
redis.hmset(key, clsAr);
|
|
18
|
+
}
|
|
19
|
+
return clsAr;
|
|
20
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import getTemplate from '../../controllers/utils/getTemplate.js';
|
|
2
|
-
import getSelectVal from './getSelectVal.js';
|
|
3
|
-
|
|
4
|
-
export default async function metaFormat({ rows, table }) {
|
|
5
|
-
const loadTable = await getTemplate('table', table);
|
|
6
|
-
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
7
|
-
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
8
|
-
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
9
|
-
|
|
10
|
-
// cls & select format
|
|
11
|
-
|
|
12
|
-
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
13
|
-
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
|
|
14
|
-
if (!values.length) return null;
|
|
15
|
-
|
|
16
|
-
const cls = await getSelectVal({ name: attr.data, values });
|
|
17
|
-
if (!cls) return null;
|
|
18
|
-
rows.forEach(el => {
|
|
19
|
-
const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
|
|
20
|
-
if (!val) return;
|
|
21
|
-
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
return null;
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
return rows;
|
|
28
|
-
}
|
|
1
|
+
import getTemplate from '../../controllers/utils/getTemplate.js';
|
|
2
|
+
import getSelectVal from './getSelectVal.js';
|
|
3
|
+
|
|
4
|
+
export default async function metaFormat({ rows, table }) {
|
|
5
|
+
const loadTable = await getTemplate('table', table);
|
|
6
|
+
const selectCols = loadTable?.columns?.filter((e) => e.data);
|
|
7
|
+
const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
|
|
8
|
+
if (!selectCols?.length && !metaCls?.length) return rows;
|
|
9
|
+
|
|
10
|
+
// cls & select format
|
|
11
|
+
|
|
12
|
+
await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
|
|
13
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
|
|
14
|
+
if (!values.length) return null;
|
|
15
|
+
|
|
16
|
+
const cls = await getSelectVal({ name: attr.data, values });
|
|
17
|
+
if (!cls) return null;
|
|
18
|
+
rows.forEach(el => {
|
|
19
|
+
const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
|
|
20
|
+
if (!val) return;
|
|
21
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return null;
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
return rows;
|
|
28
|
+
}
|