@opengis/fastify-table 1.1.9 → 1.1.11
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 +305 -305
- package/crud/controllers/deleteCrud.js +22 -22
- package/crud/controllers/insert.js +61 -61
- package/crud/controllers/update.js +62 -62
- package/crud/funcs/dataDelete.js +19 -19
- package/crud/funcs/dataInsert.js +30 -30
- package/crud/funcs/dataUpdate.js +48 -36
- 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 +76 -76
- package/crud/index.js +36 -36
- package/helper.js +28 -28
- package/index.js +97 -97
- package/migration/exec.migrations.js +79 -79
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/notification/funcs/sendNotification.js +111 -111
- package/package.json +26 -26
- package/pg/funcs/getMeta.js +27 -27
- 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/migrations/properties.sql +30 -30
- package/server/migrations/roles.sql +164 -164
- package/server/migrations/users.sql +89 -89
- package/table/controllers/data.js +103 -103
- package/table/controllers/filter.js +40 -37
- package/table/controllers/search.js +80 -80
- package/table/controllers/suggest.js +79 -79
- package/table/controllers/table.js +52 -52
- package/table/controllers/utils/addTemplateDir.js +8 -8
- package/table/controllers/utils/getSelect.js +19 -19
- package/table/controllers/utils/getSelectMeta.js +66 -66
- package/table/controllers/utils/getTemplate_old.js +28 -28
- package/table/controllers/utils/getTemplates.js +18 -18
- package/table/controllers/utils/gisIRColumn.js +68 -68
- package/table/controllers/utils/loadTemplate.js +1 -1
- package/table/controllers/utils/loadTemplatePath.js +1 -1
- package/table/controllers/utils/userTemplateDir.js +1 -1
- package/table/funcs/getFilterSQL/index.js +79 -79
- 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/table/index.js +84 -84
- package/test/api/crud.test.js +89 -89
- package/test/api/suggest.test.js +66 -66
- package/test/api/table.test.js +89 -89
- package/test/api/widget.test.js +117 -117
- package/test/templates/select/test.storage.data.json +3 -3
- package/test/templates/select/test.suggest.ato_new.json +3 -3
- package/test/templates/select/test.suggest.ato_new.sql +25 -25
- package/test/templates/select/test.suggest.data.json +4 -4
- package/test/templates/select/test.suggest.parent.sql +1 -1
- package/util/controllers/properties.add.js +57 -57
- package/util/controllers/status.monitor.js +8 -8
- package/utils.js +48 -48
- 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,52 +1,52 @@
|
|
|
1
|
-
import getTemplate from './utils/getTemplate.js';
|
|
2
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
-
|
|
4
|
-
export default async function tableAPI(req) {
|
|
5
|
-
const {
|
|
6
|
-
pg, params = {}, query = {}, opt = {},
|
|
7
|
-
} = req;
|
|
8
|
-
if (!params.id) return { message: 'not enough params', status: 400 };
|
|
9
|
-
|
|
10
|
-
const loadTable = await getTemplate('table', opt?.table || params.table) || {};
|
|
11
|
-
if (!loadTable) {
|
|
12
|
-
if (!pg.pk?.[opt?.table || params.table]) { return { message: 'not found', status: 404 }; }
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const {
|
|
16
|
-
table, /* columns, */ form,
|
|
17
|
-
} = loadTable;
|
|
18
|
-
|
|
19
|
-
const { pk, columns: dbColumns = [] } = await getMeta(table || opt?.table || params.table);
|
|
20
|
-
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
21
|
-
|
|
22
|
-
// const cols = columns.map((el) => el.name || el).join(',');
|
|
23
|
-
const schema = await getTemplate('form', opt?.form || form) || {};
|
|
24
|
-
// skip DataTable from another table
|
|
25
|
-
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
26
|
-
// skip non-existing columns
|
|
27
|
-
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
28
|
-
|
|
29
|
-
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || opt?.table || params.table} limit 0`) : {};
|
|
30
|
-
const cols = loadTable?.table
|
|
31
|
-
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.join(',')
|
|
32
|
-
: fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
33
|
-
const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
|
|
34
|
-
const geom = columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : '';
|
|
35
|
-
const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table || opt?.table || params.table} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
36
|
-
|
|
37
|
-
if (query.sql === '1') return q;
|
|
38
|
-
|
|
39
|
-
const { rows } = await pg.query(q, [opt?.id || params.id]);
|
|
40
|
-
|
|
41
|
-
if (extraKeys?.length) {
|
|
42
|
-
await Promise.all(rows?.map(async (row) => {
|
|
43
|
-
await Promise.all(extraKeys?.map(async (key) => {
|
|
44
|
-
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
45
|
-
const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
|
|
46
|
-
Object.assign(row, { [key]: extraRows });
|
|
47
|
-
}));
|
|
48
|
-
}));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return rows?.[0] || {};
|
|
52
|
-
}
|
|
1
|
+
import getTemplate from './utils/getTemplate.js';
|
|
2
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
+
|
|
4
|
+
export default async function tableAPI(req) {
|
|
5
|
+
const {
|
|
6
|
+
pg, params = {}, query = {}, opt = {},
|
|
7
|
+
} = req;
|
|
8
|
+
if (!params.id) return { message: 'not enough params', status: 400 };
|
|
9
|
+
|
|
10
|
+
const loadTable = await getTemplate('table', opt?.table || params.table) || {};
|
|
11
|
+
if (!loadTable) {
|
|
12
|
+
if (!pg.pk?.[opt?.table || params.table]) { return { message: 'not found', status: 404 }; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
table, /* columns, */ form,
|
|
17
|
+
} = loadTable;
|
|
18
|
+
|
|
19
|
+
const { pk, columns: dbColumns = [] } = await getMeta(table || opt?.table || params.table);
|
|
20
|
+
if (!pk) return { message: `table not found: ${table}`, status: 404 };
|
|
21
|
+
|
|
22
|
+
// const cols = columns.map((el) => el.name || el).join(',');
|
|
23
|
+
const schema = await getTemplate('form', opt?.form || form) || {};
|
|
24
|
+
// skip DataTable from another table
|
|
25
|
+
const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
|
|
26
|
+
// skip non-existing columns
|
|
27
|
+
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
28
|
+
|
|
29
|
+
const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || opt?.table || params.table} limit 0`) : {};
|
|
30
|
+
const cols = loadTable?.table
|
|
31
|
+
? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.join(',')
|
|
32
|
+
: fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
|
|
33
|
+
const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
|
|
34
|
+
const geom = columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : '';
|
|
35
|
+
const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table || opt?.table || params.table} t where ${where.join(' and ') || 'true'} limit 1`;
|
|
36
|
+
|
|
37
|
+
if (query.sql === '1') return q;
|
|
38
|
+
|
|
39
|
+
const { rows } = await pg.query(q, [opt?.id || params.id]);
|
|
40
|
+
|
|
41
|
+
if (extraKeys?.length) {
|
|
42
|
+
await Promise.all(rows?.map(async (row) => {
|
|
43
|
+
await Promise.all(extraKeys?.map(async (key) => {
|
|
44
|
+
const { colModel, table: extraTable, parent_id: parentId } = schema[key];
|
|
45
|
+
const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
|
|
46
|
+
Object.assign(row, { [key]: extraRows });
|
|
47
|
+
}));
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return rows?.[0] || {};
|
|
52
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import userTemplateDir from './userTemplateDir.js';
|
|
2
|
-
|
|
3
|
-
export default function addTemplateDir(dir) {
|
|
4
|
-
if (dir) {
|
|
5
|
-
userTemplateDir.push(dir);
|
|
6
|
-
}
|
|
7
|
-
return userTemplateDir;
|
|
8
|
-
}
|
|
1
|
+
import userTemplateDir from './userTemplateDir.js';
|
|
2
|
+
|
|
3
|
+
export default function addTemplateDir(dir) {
|
|
4
|
+
if (dir) {
|
|
5
|
+
userTemplateDir.push(dir);
|
|
6
|
+
}
|
|
7
|
+
return userTemplateDir;
|
|
8
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import getTemplate from './getTemplate.js';
|
|
2
|
-
|
|
3
|
-
const loadCls = {};
|
|
4
|
-
|
|
5
|
-
export default async function getSelect(name) {
|
|
6
|
-
if (loadCls[name]) return loadCls[name];
|
|
7
|
-
|
|
8
|
-
const clsData = await getTemplate(['cls', 'select'], name);
|
|
9
|
-
if (!clsData) { return null; }
|
|
10
|
-
|
|
11
|
-
// console.log(clsData);
|
|
12
|
-
if (clsData && Array.isArray(clsData)) {
|
|
13
|
-
loadCls[name] = { arr: clsData };
|
|
14
|
-
return loadCls[name];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
loadCls[name] = clsData.sql ? clsData : { sql: clsData };
|
|
18
|
-
return loadCls[name];
|
|
19
|
-
}
|
|
1
|
+
import getTemplate from './getTemplate.js';
|
|
2
|
+
|
|
3
|
+
const loadCls = {};
|
|
4
|
+
|
|
5
|
+
export default async function getSelect(name) {
|
|
6
|
+
if (loadCls[name]) return loadCls[name];
|
|
7
|
+
|
|
8
|
+
const clsData = await getTemplate(['cls', 'select'], name);
|
|
9
|
+
if (!clsData) { return null; }
|
|
10
|
+
|
|
11
|
+
// console.log(clsData);
|
|
12
|
+
if (clsData && Array.isArray(clsData)) {
|
|
13
|
+
loadCls[name] = { arr: clsData };
|
|
14
|
+
return loadCls[name];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
loadCls[name] = clsData.sql ? clsData : { sql: clsData };
|
|
18
|
+
return loadCls[name];
|
|
19
|
+
}
|
|
@@ -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, nocache }) {
|
|
17
|
-
if (selectMeta[name] && !nocache) 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 `;
|
|
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' : dataOrigin.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 (${sql})q` : sql,
|
|
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, nocache }) {
|
|
17
|
+
if (selectMeta[name] && !nocache) 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 `;
|
|
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' : dataOrigin.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 (${sql})q` : sql,
|
|
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 { 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 { sql };
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import config from '../../../config.js';
|
|
4
|
-
|
|
5
|
-
const loadTemplate = {};
|
|
6
|
-
|
|
7
|
-
export default async function getTemplateDir(type) {
|
|
8
|
-
if (!type) return null;
|
|
9
|
-
|
|
10
|
-
const cwd = process.cwd();
|
|
11
|
-
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
12
|
-
|
|
13
|
-
if (!loadTemplate[type]) {
|
|
14
|
-
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
15
|
-
loadTemplate[type] = typeList;
|
|
16
|
-
}
|
|
17
|
-
return loadTemplate[type];
|
|
18
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import config from '../../../config.js';
|
|
4
|
+
|
|
5
|
+
const loadTemplate = {};
|
|
6
|
+
|
|
7
|
+
export default async function getTemplateDir(type) {
|
|
8
|
+
if (!type) return null;
|
|
9
|
+
|
|
10
|
+
const cwd = process.cwd();
|
|
11
|
+
const typeDir = path.join(cwd, (config.templateDir || 'server/templates'), type);
|
|
12
|
+
|
|
13
|
+
if (!loadTemplate[type]) {
|
|
14
|
+
const typeList = fs.existsSync(typeDir) ? fs.readdirSync(typeDir) : [];
|
|
15
|
+
loadTemplate[type] = typeList;
|
|
16
|
+
}
|
|
17
|
+
return loadTemplate[type];
|
|
18
|
+
}
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import pgClients from '../../../pg/pgClients.js';
|
|
2
|
-
|
|
3
|
-
import getTemplate from './getTemplate.js';
|
|
4
|
-
import getSelect from './getSelect.js';
|
|
5
|
-
import getFilterSQL from '../../funcs/getFilterSQL/index.js';
|
|
6
|
-
|
|
7
|
-
export default async function gisIRColumn({
|
|
8
|
-
pg = pgClients.client, funcs = {}, layer, column, sql, query = '1=1',
|
|
9
|
-
}) {
|
|
10
|
-
const time = Date.now();
|
|
11
|
-
|
|
12
|
-
const { config = {} } = funcs;
|
|
13
|
-
|
|
14
|
-
const sel = await getSelect(query.cls || column);
|
|
15
|
-
|
|
16
|
-
const body = await getTemplate('table', layer);
|
|
17
|
-
const fData = await getFilterSQL({
|
|
18
|
-
table: body?.table || layer, query: body?.query,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
22
|
-
where relkind in ('r','v','m')`);
|
|
23
|
-
|
|
24
|
-
const tableName = body?.table || layer;
|
|
25
|
-
if (!tlist.includes(body?.table || layer)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
26
|
-
|
|
27
|
-
// eslint-disable-next-line max-len
|
|
28
|
-
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${body?.table || layer}`})q limit 0`);
|
|
29
|
-
|
|
30
|
-
const col = fields.find((el) => el.name === column);
|
|
31
|
-
|
|
32
|
-
if (!col) return { status: 404, message: 'not found' };
|
|
33
|
-
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
34
|
-
|
|
35
|
-
const q = `select ${colField} as id, count(*)::int from ${tableName} t where ${body?.query || 'true'}
|
|
36
|
-
group by ${colField} order by count desc limit 15`;
|
|
37
|
-
|
|
38
|
-
if (sql) return q;
|
|
39
|
-
|
|
40
|
-
if (!body?.columns?.length) {
|
|
41
|
-
const { rows } = await pg.query(q);
|
|
42
|
-
if (sel?.arr?.length) {
|
|
43
|
-
rows.forEach((el) => {
|
|
44
|
-
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
45
|
-
Object.assign(el, data || {});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
50
|
-
sql: config.local ? q : undefined,
|
|
51
|
-
rows,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const { rows } = await pg.query(q);
|
|
56
|
-
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
57
|
-
const select = await getSelect(cls, { val: rows.map((el) => el.id), ar: 1 });
|
|
58
|
-
rows.forEach((el) => {
|
|
59
|
-
const data = select?.arr ? select.arr?.find((item) => item.id?.toString() === el.id?.toString()) : undefined;
|
|
60
|
-
Object.assign(el, data || {});
|
|
61
|
-
});
|
|
62
|
-
return {
|
|
63
|
-
time: Date.now() - time,
|
|
64
|
-
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
65
|
-
sql: config.local ? q : undefined,
|
|
66
|
-
rows,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
1
|
+
import pgClients from '../../../pg/pgClients.js';
|
|
2
|
+
|
|
3
|
+
import getTemplate from './getTemplate.js';
|
|
4
|
+
import getSelect from './getSelect.js';
|
|
5
|
+
import getFilterSQL from '../../funcs/getFilterSQL/index.js';
|
|
6
|
+
|
|
7
|
+
export default async function gisIRColumn({
|
|
8
|
+
pg = pgClients.client, funcs = {}, layer, column, sql, query = '1=1',
|
|
9
|
+
}) {
|
|
10
|
+
const time = Date.now();
|
|
11
|
+
|
|
12
|
+
const { config = {} } = funcs;
|
|
13
|
+
|
|
14
|
+
const sel = await getSelect(query.cls || column);
|
|
15
|
+
|
|
16
|
+
const body = await getTemplate('table', layer);
|
|
17
|
+
const fData = await getFilterSQL({
|
|
18
|
+
table: body?.table || layer, query: body?.query,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const { tlist } = await pg.one(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist from pg_class
|
|
22
|
+
where relkind in ('r','v','m')`);
|
|
23
|
+
|
|
24
|
+
const tableName = body?.table || layer;
|
|
25
|
+
if (!tlist.includes(body?.table || layer)) return { error: `table not found: ${tableName}`, status: 400 };
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line max-len
|
|
28
|
+
const { fields } = await pg.query(`select * from (${fData?.optimizedSQL || `select * from ${body?.table || layer}`})q limit 0`);
|
|
29
|
+
|
|
30
|
+
const col = fields.find((el) => el.name === column);
|
|
31
|
+
|
|
32
|
+
if (!col) return { status: 404, message: 'not found' };
|
|
33
|
+
const colField = pg.pgType[col.dataTypeID]?.includes('[]') ? `unnest(${column})` : column;
|
|
34
|
+
|
|
35
|
+
const q = `select ${colField} as id, count(*)::int from ${tableName} t where ${body?.query || 'true'}
|
|
36
|
+
group by ${colField} order by count desc limit 15`;
|
|
37
|
+
|
|
38
|
+
if (sql) return q;
|
|
39
|
+
|
|
40
|
+
if (!body?.columns?.length) {
|
|
41
|
+
const { rows } = await pg.query(q);
|
|
42
|
+
if (sel?.arr?.length) {
|
|
43
|
+
rows.forEach((el) => {
|
|
44
|
+
const data = sel?.find((item) => item.id?.toString() === el.id?.toString());
|
|
45
|
+
Object.assign(el, data || {});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
count: rows?.reduce((acc, el) => acc + el.count, 0),
|
|
50
|
+
sql: config.local ? q : undefined,
|
|
51
|
+
rows,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const { rows } = await pg.query(q);
|
|
56
|
+
const cls = query.cls || body?.columns?.find((el) => el.name === column)?.data || col.data || col.option;
|
|
57
|
+
const select = await getSelect(cls, { val: rows.map((el) => el.id), ar: 1 });
|
|
58
|
+
rows.forEach((el) => {
|
|
59
|
+
const data = select?.arr ? select.arr?.find((item) => item.id?.toString() === el.id?.toString()) : undefined;
|
|
60
|
+
Object.assign(el, data || {});
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
time: Date.now() - time,
|
|
64
|
+
count: rows.reduce((acc, el) => acc + el.count, 0),
|
|
65
|
+
sql: config.local ? q : undefined,
|
|
66
|
+
rows,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default [];
|
|
1
|
+
export default [];
|