@opengis/fastify-table 1.0.34 → 1.0.36
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/.eslintrc.cjs +42 -42
- package/Changelog.md +105 -101
- package/README.md +26 -26
- package/config.js +12 -12
- package/crud/controllers/deleteCrud.js +14 -14
- package/crud/controllers/insert.js +29 -29
- package/crud/controllers/update.js +31 -31
- package/crud/controllers/utils/checkXSS.js +45 -45
- package/crud/controllers/utils/xssInjection.js +72 -72
- package/crud/funcs/dataDelete.js +15 -15
- package/crud/funcs/dataInsert.js +24 -24
- package/crud/funcs/dataUpdate.js +24 -24
- package/crud/funcs/getOpt.js +10 -10
- package/crud/funcs/getToken.js +27 -27
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/funcs/setOpt.js +16 -16
- package/crud/funcs/setToken.js +53 -53
- package/crud/index.js +29 -29
- package/helper.js +28 -28
- package/index.js +39 -39
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/notification/index.js +19 -19
- package/package.json +22 -22
- package/pg/funcs/getPG.js +29 -29
- package/pg/pgClients.js +20 -20
- package/policy/funcs/checkPolicy.js +74 -74
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/getRedis.js +23 -23
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/migrations/crm.sql +95 -95
- package/server/migrations/log.sql +41 -41
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server/templates/select/test.storage.data.json +2 -2
- package/server/templates/table/test.dataset.table.json +24 -24
- package/server.js +14 -14
- package/table/controllers/data.js +57 -57
- package/table/controllers/filter.js +37 -32
- package/table/controllers/form.js +10 -10
- package/table/controllers/search.js +41 -41
- package/table/controllers/suggest.js +60 -60
- package/table/controllers/utils/getSelect.js +20 -20
- package/table/controllers/utils/getSelectMeta.js +66 -66
- package/table/controllers/utils/getTemplate.js +28 -28
- package/table/funcs/getFilterSQL/index.js +75 -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 +26 -26
- package/table/index.js +42 -42
- package/test/api/crud.test.js +56 -56
- package/test/api/crud.xss.test.js +71 -71
- package/test/api/notification.test.js +37 -37
- package/test/api/table.test.js +57 -57
- package/test/api/widget.test.js +114 -114
- package/test/config.example +18 -18
- package/test/funcs/crud.test.js +76 -76
- package/test/funcs/notification.test.js +31 -31
- package/test/funcs/pg.test.js +34 -34
- package/test/funcs/redis.test.js +19 -19
- package/test/templates/cls/test.json +9 -9
- package/test/templates/form/cp_building.form.json +32 -32
- package/test/templates/select/account_id.json +3 -3
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -20
- package/widget/controllers/utils/historyFormat.js +76 -76
- package/widget/controllers/utils/obj2db.js +13 -13
- package/widget/controllers/widget.del.js +41 -41
- package/widget/controllers/widget.get.js +77 -77
- package/widget/controllers/widget.set.js +64 -64
- package/widget/index.js +29 -29
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
// import pgClients from '../../../pg/pgClients.js';
|
|
2
|
-
import getPG from '../../../pg/funcs/getPG.js';
|
|
3
|
-
|
|
4
|
-
import getSelect from './getSelect.js';
|
|
5
|
-
|
|
6
|
-
/*
|
|
7
|
-
function getTable(table) {
|
|
8
|
-
// eslint-disable-next-line class-methods-use-this
|
|
9
|
-
const result = table.toLowerCase().replace(/[\n\r]+/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
10
|
-
.map((el) => el.split(/[ )]/)[0]);
|
|
11
|
-
return result?.pop();
|
|
12
|
-
} */
|
|
13
|
-
|
|
14
|
-
const selectMeta = {};
|
|
15
|
-
|
|
16
|
-
export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
17
|
-
if (selectMeta[name]) return selectMeta[name];
|
|
18
|
-
|
|
19
|
-
const cls = await getSelect(name);
|
|
20
|
-
|
|
21
|
-
if (!cls) return null;
|
|
22
|
-
if (cls.arr) return cls;
|
|
23
|
-
if (!cls.sql) return null;
|
|
24
|
-
|
|
25
|
-
const pg = pg1 || getPG({ db: cls.db || 'client' });
|
|
26
|
-
const { sql: original } = cls;
|
|
27
|
-
const sql = `with c(id,text) as (${original} ) select * from c where id is not null `;
|
|
28
|
-
|
|
29
|
-
/*= == meta table === */
|
|
30
|
-
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
31
|
-
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
32
|
-
|
|
33
|
-
const dataOrigin = await pg.query(`${sql} limit 0`);
|
|
34
|
-
|
|
35
|
-
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
36
|
-
|
|
37
|
-
// const table = getTable(original);
|
|
38
|
-
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
39
|
-
|
|
40
|
-
// column name
|
|
41
|
-
const cols = dataOrigin.fields.map((el) => el.name);
|
|
42
|
-
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
43
|
-
|
|
44
|
-
const searchColumn = cls?.searchColumn || (
|
|
45
|
-
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin1.fields[1].name);
|
|
46
|
-
|
|
47
|
-
const searchQuery = `(${searchColumn.split(',').map((el) => `"${el}" ilike $1 `).join(' or ')})`;
|
|
48
|
-
|
|
49
|
-
const data = {
|
|
50
|
-
|
|
51
|
-
type: type.join(','),
|
|
52
|
-
cols: cols.join(','),
|
|
53
|
-
originalCols: dataOrigin1.fields.map((el) => el.name).join(','),
|
|
54
|
-
db: cls.db,
|
|
55
|
-
original: original?.includes('where') ? `select * from (${original})q` : original,
|
|
56
|
-
sql,
|
|
57
|
-
count,
|
|
58
|
-
searchQuery,
|
|
59
|
-
table: tableNew.join(','),
|
|
60
|
-
time: new Date().toISOString(),
|
|
61
|
-
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
selectMeta[name] = data;
|
|
65
|
-
return data;
|
|
66
|
-
}
|
|
1
|
+
// import pgClients from '../../../pg/pgClients.js';
|
|
2
|
+
import getPG from '../../../pg/funcs/getPG.js';
|
|
3
|
+
|
|
4
|
+
import getSelect from './getSelect.js';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
function getTable(table) {
|
|
8
|
+
// eslint-disable-next-line class-methods-use-this
|
|
9
|
+
const result = table.toLowerCase().replace(/[\n\r]+/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
10
|
+
.map((el) => el.split(/[ )]/)[0]);
|
|
11
|
+
return result?.pop();
|
|
12
|
+
} */
|
|
13
|
+
|
|
14
|
+
const selectMeta = {};
|
|
15
|
+
|
|
16
|
+
export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
17
|
+
if (selectMeta[name]) return selectMeta[name];
|
|
18
|
+
|
|
19
|
+
const cls = await getSelect(name);
|
|
20
|
+
|
|
21
|
+
if (!cls) return null;
|
|
22
|
+
if (cls.arr) return cls;
|
|
23
|
+
if (!cls.sql) return null;
|
|
24
|
+
|
|
25
|
+
const pg = pg1 || getPG({ db: cls.db || 'client' });
|
|
26
|
+
const { sql: original } = cls;
|
|
27
|
+
const sql = `with c(id,text) as (${original} ) select * from c where id is not null `;
|
|
28
|
+
|
|
29
|
+
/*= == meta table === */
|
|
30
|
+
const tableNew = original.toLowerCase().replace(/\n/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
31
|
+
.map((el) => el.split(/[ )]/)[0].replace(/[\r\n]+/g, ''));
|
|
32
|
+
|
|
33
|
+
const dataOrigin = await pg.query(`${sql} limit 0`);
|
|
34
|
+
|
|
35
|
+
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
36
|
+
|
|
37
|
+
// const table = getTable(original);
|
|
38
|
+
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
39
|
+
|
|
40
|
+
// column name
|
|
41
|
+
const cols = dataOrigin.fields.map((el) => el.name);
|
|
42
|
+
const type = dataOrigin.fields.map((el) => pg.pgType?.[el.dataTypeID] || 'text');
|
|
43
|
+
|
|
44
|
+
const searchColumn = cls?.searchColumn || (
|
|
45
|
+
dataOrigin.fields.find((el) => el.name === 'search') ? 'search' : dataOrigin1.fields[1].name);
|
|
46
|
+
|
|
47
|
+
const searchQuery = `(${searchColumn.split(',').map((el) => `"${el}" ilike $1 `).join(' or ')})`;
|
|
48
|
+
|
|
49
|
+
const data = {
|
|
50
|
+
|
|
51
|
+
type: type.join(','),
|
|
52
|
+
cols: cols.join(','),
|
|
53
|
+
originalCols: dataOrigin1.fields.map((el) => el.name).join(','),
|
|
54
|
+
db: cls.db,
|
|
55
|
+
original: original?.includes('where') ? `select * from (${original})q` : original,
|
|
56
|
+
sql,
|
|
57
|
+
count,
|
|
58
|
+
searchQuery,
|
|
59
|
+
table: tableNew.join(','),
|
|
60
|
+
time: new Date().toISOString(),
|
|
61
|
+
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
selectMeta[name] = data;
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { readFile } from 'fs/promises';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import config from '../../../config.js';
|
|
5
|
-
|
|
6
|
-
const loadTemplate = {};
|
|
7
|
-
|
|
8
|
-
export default async function getTemplateDir(type, name) {
|
|
9
|
-
if (!type) return null;
|
|
10
|
-
if (!name) return null;
|
|
11
|
-
|
|
12
|
-
const cwd = process.cwd();
|
|
13
|
-
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
14
|
-
|
|
15
|
-
if (!loadTemplate[type]) {
|
|
16
|
-
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
17
|
-
loadTemplate[type] = typeList;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const fullname = loadTemplate[type].find((el) => path.parse(el).name === name);
|
|
21
|
-
const ext = fullname ? path.extname(fullname)?.slice(1) : null;
|
|
22
|
-
if (!ext) return null;
|
|
23
|
-
|
|
24
|
-
const sql = loadTemplate[type].includes(`${name}.sql`) ? await readFile(path.join(typeDir, `${name}.sql`), 'utf-8') : null;
|
|
25
|
-
const data = loadTemplate[type].includes(`${name}.json`) ? JSON.parse(await readFile(path.join(typeDir, `${name}.json`), 'utf-8')) : await readFile(path.join(typeDir, `${name}.${ext}`), 'utf-8');
|
|
26
|
-
if (sql) return { ...data || {}, sql };
|
|
27
|
-
return data;
|
|
28
|
-
}
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import config from '../../../config.js';
|
|
5
|
+
|
|
6
|
+
const loadTemplate = {};
|
|
7
|
+
|
|
8
|
+
export default async function getTemplateDir(type, name) {
|
|
9
|
+
if (!type) return null;
|
|
10
|
+
if (!name) return null;
|
|
11
|
+
|
|
12
|
+
const cwd = process.cwd();
|
|
13
|
+
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
14
|
+
|
|
15
|
+
if (!loadTemplate[type]) {
|
|
16
|
+
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
17
|
+
loadTemplate[type] = typeList;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const fullname = loadTemplate[type].find((el) => path.parse(el).name === name);
|
|
21
|
+
const ext = fullname ? path.extname(fullname)?.slice(1) : null;
|
|
22
|
+
if (!ext) return null;
|
|
23
|
+
|
|
24
|
+
const sql = loadTemplate[type].includes(`${name}.sql`) ? await readFile(path.join(typeDir, `${name}.sql`), 'utf-8') : null;
|
|
25
|
+
const data = loadTemplate[type].includes(`${name}.json`) ? JSON.parse(await readFile(path.join(typeDir, `${name}.json`), 'utf-8')) : await readFile(path.join(typeDir, `${name}.${ext}`), 'utf-8');
|
|
26
|
+
if (sql) return { ...data || {}, sql };
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
/* const getTable = require('../../controllers/utils/getTable');
|
|
2
|
-
const pg = require('../../../pg/client');
|
|
3
|
-
const config = require('../../../../config'); */
|
|
4
|
-
|
|
5
|
-
import getTemplate from '../../controllers/utils/getTemplate.js';
|
|
6
|
-
import pgClients from '../../../pg/pgClients.js';
|
|
7
|
-
import config from '../../../config.js';
|
|
8
|
-
// filter util
|
|
9
|
-
import getTableSql from './util/getTableSql.js';
|
|
10
|
-
// import getCustomQuery from './util/getCustomQuery.js';
|
|
11
|
-
import getFilterQuery from './util/getFilterQuery.js';
|
|
12
|
-
import getOptimizedQuery from './util/getOptimizedQuery.js';
|
|
13
|
-
|
|
14
|
-
async function getFilterSQL({
|
|
15
|
-
table, filter, pg = pgClients.client, search, filterList, query,
|
|
16
|
-
}) {
|
|
17
|
-
if (!table) return { error: 'param table is required', status: 400 };
|
|
18
|
-
|
|
19
|
-
const body = await getTemplate('table', table);
|
|
20
|
-
|
|
21
|
-
const fieldQuery = config.allTemplates?.table?.[table] ? `${config.allTemplates?.table?.[table]} limit 0`
|
|
22
|
-
: `select * from ${body?.table || table} limit 0`;
|
|
23
|
-
const { fields = [] } = await pg.query(fieldQuery);
|
|
24
|
-
|
|
25
|
-
const { fields: fieldsModel } = pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
|
|
26
|
-
|
|
27
|
-
const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
|
|
28
|
-
const searchColumn = body?.search_column || autoSearchColumn;
|
|
29
|
-
const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
|
|
30
|
-
try {
|
|
31
|
-
const tableSQL = await getTableSql({
|
|
32
|
-
pg, body, table, fields,
|
|
33
|
-
});
|
|
34
|
-
const sval = `ilike '%${decodeURIComponent(search).replace(/'/g, "''")}%'`;
|
|
35
|
-
const searchQuery = search && searchColumn
|
|
36
|
-
? ` (${searchColumn.split(',')?.map((name) => {
|
|
37
|
-
const { pk } = tableSQL.find((el) => el.name === name) || {};
|
|
38
|
-
return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery})q where ${name} ${sval})` : `${name} ${sval}`;
|
|
39
|
-
}).join(' or ')} )` : '';
|
|
40
|
-
|
|
41
|
-
const filters = getFilterQuery({
|
|
42
|
-
filter,
|
|
43
|
-
tableSQL,
|
|
44
|
-
fields,
|
|
45
|
-
filterList: filterList || (body?.filter_list || []).concat(body?.filterInline || []).concat(body?.filterCustom || []).concat(body?.filterState || []).concat(body?.filterList || []),
|
|
46
|
-
pg,
|
|
47
|
-
config,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// filter
|
|
51
|
-
const filterQuery = filters?.filter((el) => el.query)?.map((el) => `${el.query} `).join(' and ');
|
|
52
|
-
const q = [body?.query, query, searchQuery, filterQuery].filter((el) => el).join(' and ');
|
|
53
|
-
|
|
54
|
-
// table
|
|
55
|
-
const modelQuery = body?.model || body?.table || table;
|
|
56
|
-
const optimizedSQL = `select * from ${getOptimizedQuery({ body, table, q })} `;
|
|
57
|
-
const tableCount = getOptimizedQuery({ body, table, q }, true);
|
|
58
|
-
// console.log(optimizedSQL);
|
|
59
|
-
return {
|
|
60
|
-
filterList,
|
|
61
|
-
|
|
62
|
-
q,
|
|
63
|
-
optimizedSQL,
|
|
64
|
-
tableCount,
|
|
65
|
-
table: modelQuery,
|
|
66
|
-
// filter parts
|
|
67
|
-
searchQuery,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
throw new Error(err.toString());
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default getFilterSQL;
|
|
1
|
+
/* const getTable = require('../../controllers/utils/getTable');
|
|
2
|
+
const pg = require('../../../pg/client');
|
|
3
|
+
const config = require('../../../../config'); */
|
|
4
|
+
|
|
5
|
+
import getTemplate from '../../controllers/utils/getTemplate.js';
|
|
6
|
+
import pgClients from '../../../pg/pgClients.js';
|
|
7
|
+
import config from '../../../config.js';
|
|
8
|
+
// filter util
|
|
9
|
+
import getTableSql from './util/getTableSql.js';
|
|
10
|
+
// import getCustomQuery from './util/getCustomQuery.js';
|
|
11
|
+
import getFilterQuery from './util/getFilterQuery.js';
|
|
12
|
+
import getOptimizedQuery from './util/getOptimizedQuery.js';
|
|
13
|
+
|
|
14
|
+
async function getFilterSQL({
|
|
15
|
+
table, filter, pg = pgClients.client, search, filterList, query,
|
|
16
|
+
}) {
|
|
17
|
+
if (!table) return { error: 'param table is required', status: 400 };
|
|
18
|
+
|
|
19
|
+
const body = await getTemplate('table', table);
|
|
20
|
+
|
|
21
|
+
const fieldQuery = config.allTemplates?.table?.[table] ? `${config.allTemplates?.table?.[table]} limit 0`
|
|
22
|
+
: `select * from ${body?.table || table} limit 0`;
|
|
23
|
+
const { fields = [] } = await pg.query(fieldQuery);
|
|
24
|
+
|
|
25
|
+
const { fields: fieldsModel } = pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
|
|
26
|
+
|
|
27
|
+
const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
|
|
28
|
+
const searchColumn = body?.search_column || autoSearchColumn;
|
|
29
|
+
const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
|
|
30
|
+
try {
|
|
31
|
+
const tableSQL = await getTableSql({
|
|
32
|
+
pg, body, table, fields,
|
|
33
|
+
});
|
|
34
|
+
const sval = `ilike '%${decodeURIComponent(search).replace(/'/g, "''")}%'`;
|
|
35
|
+
const searchQuery = search && searchColumn
|
|
36
|
+
? ` (${searchColumn.split(',')?.map((name) => {
|
|
37
|
+
const { pk } = tableSQL.find((el) => el.name === name) || {};
|
|
38
|
+
return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery})q where ${name} ${sval})` : `${name} ${sval}`;
|
|
39
|
+
}).join(' or ')} )` : '';
|
|
40
|
+
|
|
41
|
+
const filters = getFilterQuery({
|
|
42
|
+
filter,
|
|
43
|
+
tableSQL,
|
|
44
|
+
fields,
|
|
45
|
+
filterList: filterList || (body?.filter_list || []).concat(body?.filterInline || []).concat(body?.filterCustom || []).concat(body?.filterState || []).concat(body?.filterList || []),
|
|
46
|
+
pg,
|
|
47
|
+
config,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// filter
|
|
51
|
+
const filterQuery = filters?.filter((el) => el.query)?.map((el) => `${el.query} `).join(' and ');
|
|
52
|
+
const q = [body?.query, query, searchQuery, filterQuery].filter((el) => el).join(' and ');
|
|
53
|
+
|
|
54
|
+
// table
|
|
55
|
+
const modelQuery = body?.model || body?.table || table;
|
|
56
|
+
const optimizedSQL = `select * from ${getOptimizedQuery({ body, table, q })} `;
|
|
57
|
+
const tableCount = getOptimizedQuery({ body, table, q }, true);
|
|
58
|
+
// console.log(optimizedSQL);
|
|
59
|
+
return {
|
|
60
|
+
filterList,
|
|
61
|
+
|
|
62
|
+
q,
|
|
63
|
+
optimizedSQL,
|
|
64
|
+
tableCount,
|
|
65
|
+
table: modelQuery,
|
|
66
|
+
// filter parts
|
|
67
|
+
searchQuery,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
throw new Error(err.toString());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default getFilterSQL;
|
|
@@ -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;
|