@opengis/fastify-table 1.0.77 → 1.0.79

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 CHANGED
@@ -1,8 +1,12 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.79 - 15.08.2024
4
+
5
+ - refactor /table API
6
+
3
7
  ## 1.0.77 - 13.08.2024
4
8
 
5
- - add statusMonotor API
9
+ - add statusMonitor API
6
10
 
7
11
  ## 1.0.75 - 12.08.2024
8
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.77",
3
+ "version": "1.0.79",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -18,7 +18,7 @@ export default async function dataAPI(req) {
18
18
  if (!loadTable) { return { message: 'template not found', status: 404 }; }
19
19
 
20
20
  const {
21
- table, columns, sql, cardSql, filters, form, meta,
21
+ table, columns, sql, cardSql, filters, form, meta, sqlColumns,
22
22
  } = loadTable;
23
23
  const { pk, columns: dbColumns = [] } = await getMeta(table);
24
24
 
@@ -58,7 +58,7 @@ export default async function dataAPI(req) {
58
58
  const access = await getAccess(req, params.table);
59
59
  const where = [(opt?.id || params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable.query, fData.q, state, custom, search, access?.query || '1=1'].filter((el) => el);
60
60
  const cardColumns = cardSqlFiltered.length ? `,${cardSqlFiltered.map((el) => el.name)}` : '';
61
- const q = `select ${pk ? `"${pk}" as id,` : ''} ${columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : ''} ${query.id || query.key ? '*' : cols || '*'} ${cardColumns} from ${table} t ${sqlTable} ${cardSqlTable} where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
61
+ const q = `select ${pk ? `"${pk}" as id,` : ''} ${columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : ''} ${query.id || query.key ? '*' : sqlColumns || cols || '*'} ${cardColumns} from ${table} t ${sqlTable} ${cardSqlTable} where ${where.join(' and ') || 'true'} ${order} ${offset} limit ${limit}`;
62
62
 
63
63
  if (query.sql === '1') { return q; }
64
64
 
@@ -1,44 +1,47 @@
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', params.table);
11
- if (!loadTable) { return { message: 'not found', status: 404 }; }
12
-
13
- const {
14
- table, columns, form,
15
- } = loadTable;
16
-
17
- const { pk, columns: dbColumns = [] } = await getMeta(table);
18
- if (!pk) return { message: `table not found: ${table}`, status: 404 };
19
-
20
- const cols = columns.map((el) => el.name || el).join(',');
21
- const columnList = dbColumns.map((el) => el.name || el).join(',');
22
-
23
- const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
24
- const geom = columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : '';
25
- const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table} t where ${where.join(' and ') || 'true'} limit 1`;
26
-
27
- if (query.sql === '1') return q;
28
-
29
- const { rows } = await pg.query(q, [opt.id || params.id]);
30
-
31
- const formData = form ? (await getTemplate('form', form) || {}) : {};
32
- const extraKeys = Object.keys(formData)?.filter((key) => formData?.[key]?.type === 'DataTable' && formData?.[key]?.table && formData?.[key]?.parent_id && formData[key]?.colModel);
33
- if (extraKeys?.length) {
34
- await Promise.all(rows?.map(async (row) => {
35
- await Promise.all(extraKeys?.map(async (key) => {
36
- const { colModel, table: extraTable, parent_id: parentId } = formData[key];
37
- const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
38
- Object.assign(row, { [key]: extraRows });
39
- }));
40
- }));
41
- }
42
-
43
- return rows?.[0] || {};
44
- }
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) { return { message: 'not found', status: 404 }; }
12
+
13
+ const {
14
+ table, /* columns, */ form,
15
+ } = loadTable;
16
+
17
+ const { pk, columns: dbColumns = [] } = await getMeta(table);
18
+ if (!pk) return { message: `table not found: ${table}`, status: 404 };
19
+
20
+ // const cols = columns.map((el) => el.name || el).join(',');
21
+ const schema = await getTemplate('form', opt?.form || form) || {};
22
+ // skip DataTable from another table
23
+ const extraKeys = Object.keys(schema)?.filter((key) => schema[key]?.type === 'DataTable' && schema[key]?.table && schema[key]?.parent_id && schema[key]?.colModel?.length);
24
+ // skip non-existing columns
25
+ const columnList = dbColumns.map((el) => el.name || el).join(',');
26
+
27
+ const cols = Object.keys(schema || {}).filter((col) => columnList.includes(col) && !extraKeys.includes(col))?.join(',');
28
+ const where = [`"${pk}" = $1`, loadTable.query].filter((el) => el);
29
+ const geom = columnList.includes('geom') ? 'st_asgeojson(geom)::json as geom,' : '';
30
+ const q = `select "${pk}" as id, ${geom} ${cols || '*'} from ${table} t where ${where.join(' and ') || 'true'} limit 1`;
31
+
32
+ if (query.sql === '1') return q;
33
+
34
+ const { rows } = await pg.query(q, [opt?.id || params.id]);
35
+
36
+ if (extraKeys?.length) {
37
+ await Promise.all(rows?.map(async (row) => {
38
+ await Promise.all(extraKeys?.map(async (key) => {
39
+ const { colModel, table: extraTable, parent_id: parentId } = schema[key];
40
+ const { rows: extraRows } = await pg.query(`select ${parentId} as parent, ${colModel.map((col) => col.name).join(',')} from ${extraTable} a where ${parentId}=$1`, [row.id]);
41
+ Object.assign(row, { [key]: extraRows });
42
+ }));
43
+ }));
44
+ }
45
+
46
+ return rows?.[0] || {};
47
+ }
package/table/index.js CHANGED
@@ -1,78 +1,78 @@
1
- import suggest from './controllers/suggest.js';
2
- import data from './controllers/data.js';
3
- import table from './controllers/table.js';
4
- import card from './controllers/card.js';
5
- import search from './controllers/search.js';
6
- import filter from './controllers/filter.js';
7
- import form from './controllers/form.js';
8
- import metaFormat from './funcs/metaFormat/index.js';
9
- import getFilterSQL from './funcs/getFilterSQL/index.js';
10
- import getTemplate from './controllers/utils/getTemplate.js';
11
-
12
- const tableSchema = {
13
- querystring: {
14
- page: { type: 'string', pattern: '^(\\d+)$' },
15
- order: { type: 'string', pattern: '^(\\d+)$' },
16
- filter: { type: 'string', pattern: '^([\\w\\d_-]+)=([\\w\\d_-]+)$' },
17
- },
18
- params: {
19
- id: { type: 'string', pattern: '^([\\d\\w]+)$' },
20
- table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
21
- },
22
- };
23
-
24
- const searchSchema = {
25
- querystring: {
26
- page: { type: 'string', pattern: '^(\\d+)$' },
27
- limit: { type: 'string', pattern: '^(\\d+)$' },
28
- order: { type: 'string', pattern: '^([\\w_.]+)$' },
29
- desc: { type: 'string', pattern: '^(desc)|(asc)$' },
30
- key: { type: 'string', pattern: '^([\\w\\d_]+)$' },
31
- table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
32
- sql: { type: 'string', pattern: '^(\\d)$' },
33
- },
34
- };
35
-
36
- const suggestSchema = {
37
- querystring: {
38
- lang: { type: 'string', pattern: '^([\\w.]+)$' },
39
- // parent: { type: 'string', pattern: '^([\\w,./]+)$' },
40
- sel: { type: 'string', pattern: '^([\\w,./]+)$' },
41
- name: { type: 'string', pattern: '^([\\w,./]+)$' },
42
- // key: { type: 'string', pattern: '^([\\w\\d_]+)$' },
43
- // val: { type: 'string', pattern: '^([\\w.,]+)$' },
44
- sql: { type: 'string', pattern: '^(\\d)$' },
45
- },
46
- params: {
47
- id: { type: 'string', pattern: '^([\\d\\w]+)$' },
48
- },
49
- };
50
-
51
- const formSchema = {
52
- params: {
53
- form: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
54
- },
55
- };
56
-
57
- const filterSchema = {
58
- params: {
59
- table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
60
- },
61
- };
62
-
63
- async function plugin(fastify, config = {}) {
64
- const prefix = config.prefix || '/api';
65
- fastify.decorate('metaFormat', metaFormat);
66
- fastify.decorate('getFilterSQL', getFilterSQL);
67
- fastify.decorate('getTemplate', getTemplate);
68
-
69
- fastify.get(`${prefix}/suggest/:data`, { schema: suggestSchema }, suggest);
70
- fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
71
- fastify.get(`${prefix}/table/:table/:id`, { schema: tableSchema }, table);
72
- fastify.get(`${prefix}/card/:table/:id`, { schema: tableSchema }, card);
73
- fastify.get(`${prefix}/search`, { schema: searchSchema }, search);
74
- fastify.get(`${prefix}/filter/:table`, { schema: filterSchema }, filter);
75
- fastify.get(`${prefix}/form/:form`, { schema: formSchema }, form);
76
- }
77
-
78
- export default plugin;
1
+ import suggest from './controllers/suggest.js';
2
+ import data from './controllers/data.js';
3
+ import table from './controllers/table.js';
4
+ import card from './controllers/card.js';
5
+ import search from './controllers/search.js';
6
+ import filter from './controllers/filter.js';
7
+ import form from './controllers/form.js';
8
+ import metaFormat from './funcs/metaFormat/index.js';
9
+ import getFilterSQL from './funcs/getFilterSQL/index.js';
10
+ import getTemplate from './controllers/utils/getTemplate.js';
11
+
12
+ const tableSchema = {
13
+ querystring: {
14
+ page: { type: 'string', pattern: '^(\\d+)$' },
15
+ order: { type: 'string', pattern: '^(\\d+)$' },
16
+ filter: { type: 'string', pattern: '^([\\w\\d_-]+)=([\\w\\d_-]+)$' },
17
+ },
18
+ params: {
19
+ id: { type: 'string', pattern: '^([\\d\\w]+)$' },
20
+ table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
21
+ },
22
+ };
23
+
24
+ const searchSchema = {
25
+ querystring: {
26
+ page: { type: 'string', pattern: '^(\\d+)$' },
27
+ limit: { type: 'string', pattern: '^(\\d+)$' },
28
+ order: { type: 'string', pattern: '^([\\w_.]+)$' },
29
+ desc: { type: 'string', pattern: '^(desc)|(asc)$' },
30
+ key: { type: 'string', pattern: '^([\\w\\d_]+)$' },
31
+ table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
32
+ sql: { type: 'string', pattern: '^(\\d)$' },
33
+ },
34
+ };
35
+
36
+ const suggestSchema = {
37
+ querystring: {
38
+ lang: { type: 'string', pattern: '^([\\w.]+)$' },
39
+ // parent: { type: 'string', pattern: '^([\\w,./]+)$' },
40
+ sel: { type: 'string', pattern: '^([\\w,./]+)$' },
41
+ name: { type: 'string', pattern: '^([\\w,./]+)$' },
42
+ // key: { type: 'string', pattern: '^([\\w\\d_]+)$' },
43
+ // val: { type: 'string', pattern: '^([\\w.,]+)$' },
44
+ sql: { type: 'string', pattern: '^(\\d)$' },
45
+ },
46
+ params: {
47
+ id: { type: 'string', pattern: '^([\\d\\w]+)$' },
48
+ },
49
+ };
50
+
51
+ const formSchema = {
52
+ params: {
53
+ form: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
54
+ },
55
+ };
56
+
57
+ const filterSchema = {
58
+ params: {
59
+ table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
60
+ },
61
+ };
62
+
63
+ async function plugin(fastify, config = {}) {
64
+ const prefix = config.prefix || '/api';
65
+ fastify.decorate('metaFormat', metaFormat);
66
+ fastify.decorate('getFilterSQL', getFilterSQL);
67
+ fastify.decorate('getTemplate', getTemplate);
68
+
69
+ fastify.get(`${prefix}/suggest/:data`, { schema: suggestSchema }, suggest);
70
+ fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
71
+ fastify.get(`${prefix}/table/:table/:id`, { schema: tableSchema }, table);
72
+ fastify.get(`${prefix}/card/:table/:id`, { schema: tableSchema }, card);
73
+ fastify.get(`${prefix}/search`, { schema: searchSchema }, search);
74
+ fastify.get(`${prefix}/filter/:table`, { schema: filterSchema }, filter);
75
+ fastify.get(`${prefix}/form/:form`, { schema: formSchema }, form);
76
+ }
77
+
78
+ export default plugin;