@opengis/admin 0.1.6 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/admin",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "This project Softpro Admin",
5
5
  "main": "dist/admin.js",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "dependencies": {
33
33
  "@opengis/fastify-auth": "^1.0.20",
34
34
  "@opengis/fastify-file": "^1.0.17",
35
- "@opengis/fastify-table": "^1.1.5",
35
+ "@opengis/fastify-table": "^1.1.7",
36
36
  "@opengis/v3-core": "^0.1.92",
37
37
  "@opengis/v3-filter": "^0.0.31",
38
38
  "@turf/turf": "^7.1.0",
@@ -62,4 +62,4 @@
62
62
  "vitepress-plugin-tabs": "^0.5.0",
63
63
  "vitepress-sidebar": "^1.22.0"
64
64
  }
65
- }
65
+ }
@@ -11,19 +11,21 @@ export default async function getCardData({
11
11
  const template = await getTemplate('card', table);
12
12
 
13
13
  const tableTemplate = await getTemplate('table', table);
14
- const index = template.find(el => el[0] === 'index.yml')?.[1] || {}
14
+ const index = template?.find(el => el[0] === 'index.yml')?.[1] || {}
15
15
  //return template
16
16
 
17
17
  const data = {};
18
- const { message, rows = [] } = await getTableData({ pg, funcs, params: { table, id }, session });
18
+ const { message, rows = [] } = await getTableData({ pg, funcs, params: { table, id }, session, mode: 'card' });
19
19
 
20
20
  if (message) return { message };
21
21
 
22
- await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
23
- const htmlContent = await funcs.handlebars.compile(el[1])(rows[0]);
24
- const name = el[0].substring(0, el[0].lastIndexOf('.'))
25
- data[name] = htmlContent;
26
- }));
22
+ if (template) {
23
+ await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
24
+ const htmlContent = await funcs.handlebars.compile(el[1])(rows[0]);
25
+ const name = el[0].substring(0, el[0].lastIndexOf('.'))
26
+ data[name] = htmlContent;
27
+ }));
28
+ }
27
29
 
28
30
  return { time: Date.now() - time, ...index, data, rows, columns: tableTemplate?.columns || tableTemplate?.colModel };
29
31
 
@@ -9,13 +9,15 @@ import getFilterSQL from "./funcs/getFilterSQL/index.js";
9
9
 
10
10
  import assignTokens from './utils/assignTokens.js';
11
11
 
12
+ import getColumns from './utils/getColumns.js';
13
+
12
14
  const maxLimit = 100;
13
15
 
14
16
  export default async function getTableData(req) {
15
17
  const time = Date.now();
16
18
 
17
19
  const {
18
- pg, params, funcs = {}, query = {}, opt = {}, session = {},
20
+ pg, params, funcs = {}, query = {}, opt = {}, session = {}, mode = 'table',
19
21
  } = req;
20
22
  const { uid } = session.passport?.user || {};
21
23
 
@@ -37,14 +39,14 @@ export default async function getTableData(req) {
37
39
  return { message: `table not found: ${table}`, status: 404 };
38
40
  }
39
41
 
40
- const cols = columns.filter((el) => el.name !== 'geom').map((el) => el.name || el).join(',');
42
+ const { cols, columnList, extraKeys, schema } = await getColumns({
43
+ columns, params, opt, loadTable, form, table, dbColumns, mode,
44
+ });
41
45
 
42
46
  const metaCols = Object.keys(loadTable?.meta?.cls || {}).filter((el) => !cols.includes(el)).length
43
47
  ? `,${Object.keys(loadTable?.meta?.cls || {})?.filter((el) => !cols.includes(el)).join(',')}`
44
48
  : '';
45
49
 
46
- const columnList = dbColumns.map((el) => el.name || el).join(',');
47
-
48
50
  const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql.replace('{{uid}}', uid)}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
49
51
 
50
52
  if (params.id && columnList.includes(params.id)) {
@@ -95,6 +97,16 @@ export default async function getTableData(req) {
95
97
 
96
98
  const total = keyQuery || opt?.id || params.id ? rows.length : await pg.queryCache(`select count(*) from ${table} t ${sqlTable} where ${where.join(' and ') || 'true'}`).then((el) => (el?.rows[0]?.count || 0) - 0);
97
99
 
100
+ if (extraKeys?.length && mode === 'card') {
101
+ await Promise.all(rows?.map(async (row) => {
102
+ await Promise.all(extraKeys?.map(async (key) => {
103
+ const { colModel, table: extraTable, parent_id: parentId } = schema[key];
104
+ const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
105
+ Object.assign(row, { [key]: extraRows });
106
+ }));
107
+ }));
108
+ }
109
+
98
110
  await metaFormat({ funcs, rows, table: params.table });
99
111
 
100
112
  const res = {
@@ -0,0 +1,22 @@
1
+ import { getTemplate } from "@opengis/fastify-table/utils.js";
2
+
3
+ export default async function getColumns({
4
+ columns = [], params = {}, opt = {}, loadTable = {}, form, table, dbColumns = [], mode = 'table',
5
+ }) {
6
+ const columnList = dbColumns.map((el) => el.name || el).join(',');
7
+ if (mode === 'table') {
8
+ const cols = columns.filter((el) => el?.name !== 'geom').map((el) => el?.name || el).join(',');
9
+ return { cols, columnList };
10
+ }
11
+ // card / edit form
12
+ const schema = await getTemplate('form', opt?.form || form) || {};
13
+ // skip DataTable from another table
14
+ const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
15
+ // skip non-existing columns
16
+ const { fields = [] } = !loadTable?.table ? await pg.query(`select * from ${table || opt?.table || params.table} limit 0`) : {};
17
+ const cols = loadTable?.table
18
+ ? Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.join(',')
19
+ : fields.map((el) => (el?.name?.includes('geom') ? `st_asgeojson(${el.name})::json as "${el.name}"` : `"${el?.name}"`)).join(',');
20
+
21
+ return { cols, columnList, extraKeys, schema };
22
+ }
@@ -1,50 +1,33 @@
1
1
  import { readdir, readFile } from 'fs/promises';
2
2
  import { join } from 'path';
3
- import config from '../../../../config.js';
3
+ import userTemplateDir from '@opengis/fastify-table/table/controllers/utils/userTemplateDir.js'
4
+ import { existsSync, readdirSync } from 'fs';
4
5
 
5
- const findMenuJsons = async (startPath, filter) => {
6
- const files = await readdir(startPath, { withFileTypes: true });
6
+ // check module dir
7
+ const moduleDir = join(process.cwd(), 'module');
8
+ const dirs = existsSync(moduleDir) ?
9
+ readdirSync(moduleDir).map(el => join(moduleDir, el)) : [];
7
10
 
8
- return await files.reduce(async (prevPromise, file) => {
9
- const acc = await prevPromise;
10
- const filePath = join(startPath, file.name);
11
+ // all menu files module + user
12
+ const menuList = dirs.concat(userTemplateDir)
13
+ .map(el => join(el, 'menu.json'))
14
+ .filter(el => existsSync(el))
11
15
 
12
- if (file.isDirectory()) {
13
- const nestedFiles = await findMenuJsons(filePath, filter);
14
- return acc.concat(nestedFiles);
15
- } else if (file.name === filter) {
16
- acc.push(filePath);
17
- }
18
16
 
19
- return acc;
20
- }, Promise.resolve([]));
21
-
22
- }
23
-
24
- const readJsonFiles = async (filePaths) => {
25
- const menu = []
26
- await Promise.all(
27
- filePaths.map(async (filePath) => {
28
- const content = JSON.parse(await readFile(filePath, 'utf-8'));
29
- content.forEach(el => {
30
- menu.push(el)
31
- });
17
+ const isProduction = process.env.NODE_ENV === 'production';
18
+ const menuCache = []
32
19
 
20
+ async function readMenu() {
21
+ const list = await Promise.all(menuList.map(el => readFile(el, 'utf-8')));
22
+ const menus = list.reduce((p, el) => p.concat(JSON.parse(el)), [])
23
+ .map(el => ({ order: 0, ...el }))
24
+ .sort((a, b) => (a.order > b.order) ? 1 : ((b.order > a.order) ? -1 : 0))
33
25
 
34
- })
35
- );
36
- return menu;
26
+ if (!menuCache.length) menus.forEach(el => menuCache.push(el))
27
+ return menus;
37
28
  }
38
-
39
29
  export default async function adminMenu() {
40
- const cwd = process.cwd();
41
- //const typeDir = join(cwd, (config.templateDir || 'server/templates'));
42
- const modulesDir = join(cwd, 'module');
43
-
44
- // const menus = await findMenuJsons(typeDir, 'menu.json');
45
- const menuModules = await findMenuJsons(modulesDir, 'menu.json');
46
-
47
- const jsons = await readJsonFiles(menuModules);
30
+ const menus = isProduction && menuCache.length ? menuCache : await readMenu();
48
31
 
49
- return jsons;
32
+ return menus;
50
33
  }
@@ -1,7 +1,15 @@
1
- import { getTemplate } from "@opengis/fastify-table/utils.js"
1
+ import { getTemplate } from "@opengis/fastify-table/utils.js";
2
2
 
3
- export default async function getTemplateApi({ params }) {
3
+ export default async function getTemplateApi({ funcs, params, session = {} }) {
4
4
  const { type, name } = params;
5
5
  const data = await getTemplate(type, name);
6
- return data || { message: `template not found "${name}"`, status: 404 };
6
+ if (!['interface', 'table'].includes(type)
7
+ && (!data?.public || !data?.ispublic)
8
+ && !funcs?.config?.auth?.disable
9
+ && !session.passport?.user?.uid
10
+ && false
11
+ ) {
12
+ return `access restricted ${name}`;
13
+ }
14
+ return data?.html || data || `template not found "${name}"`;
7
15
  }