@opengis/fastify-table 1.0.18 → 1.0.19

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.
Files changed (44) hide show
  1. package/Changelog.md +52 -52
  2. package/notification/controllers/userNotifications.js +1 -1
  3. package/package.json +22 -22
  4. package/pg/funcs/autoIndex.js +89 -89
  5. package/pg/funcs/getMeta.js +27 -27
  6. package/pg/funcs/getPG.js +29 -29
  7. package/pg/funcs/init.js +42 -42
  8. package/pg/funcs/pgClients.js +2 -2
  9. package/pg/pgClients.js +20 -20
  10. package/policy/funcs/checkPolicy.js +74 -74
  11. package/policy/funcs/sqlInjection.js +33 -33
  12. package/policy/index.js +14 -14
  13. package/redis/client.js +8 -8
  14. package/redis/funcs/getRedis.js +23 -23
  15. package/redis/funcs/redisClients.js +2 -2
  16. package/redis/index.js +19 -19
  17. package/server/migrations/notifications.sql +14 -0
  18. package/server/templates/form/test.dataset.form.json +411 -411
  19. package/server/templates/select/test.storage.data.json +3 -0
  20. package/server/templates/select/test.storage.data.sql +1 -0
  21. package/server/templates/table/test.dataset.table.json +25 -0
  22. package/table/controllers/data.js +57 -57
  23. package/table/controllers/filter.js +32 -32
  24. package/table/controllers/form.js +10 -10
  25. package/table/controllers/search.js +41 -0
  26. package/table/controllers/suggest.js +60 -60
  27. package/table/controllers/utils/getSelect.js +20 -20
  28. package/table/controllers/utils/getSelectMeta.js +66 -66
  29. package/table/funcs/getFilterSQL/index.js +75 -75
  30. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  31. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  32. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  33. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  34. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  35. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  36. package/table/funcs/metaFormat/index.js +26 -26
  37. package/table/index.js +38 -25
  38. package/test/api/notification.test.js +0 -19
  39. package/test/api/table.test.js +57 -49
  40. package/test/api/widget.test.js +39 -0
  41. package/test/funcs/notification.test.js +31 -0
  42. package/widget/controllers/widget.del.js +3 -2
  43. package/widget/controllers/widget.set.js +1 -1
  44. package/widget/index.js +8 -17
@@ -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`, { cache: 1 });
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`, { cache: 1 });
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,26 +1,26 @@
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
-
8
- // cls & select format
9
-
10
- await Promise.all(selectCols?.map(async (attr) => {
11
- const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
12
- if (!values.length) return null;
13
-
14
- const cls = await getSelectVal({ name: attr.data, values });
15
- if (!cls) return null;
16
- rows.forEach(el => {
17
- const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
18
- if (!val) return;
19
- Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
20
- });
21
-
22
- return null;
23
- }));
24
-
25
- return rows;
26
- }
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
+
8
+ // cls & select format
9
+
10
+ await Promise.all(selectCols?.map(async (attr) => {
11
+ const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
12
+ if (!values.length) return null;
13
+
14
+ const cls = await getSelectVal({ name: attr.data, values });
15
+ if (!cls) return null;
16
+ rows.forEach(el => {
17
+ const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
18
+ if (!val) return;
19
+ Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
20
+ });
21
+
22
+ return null;
23
+ }));
24
+
25
+ return rows;
26
+ }
package/table/index.js CHANGED
@@ -1,25 +1,38 @@
1
- import suggest from './controllers/suggest.js';
2
- import data from './controllers/data.js';
3
- import filter from './controllers/filter.js';
4
- import form from './controllers/form.js';
5
- import metaFormat from './funcs/metaFormat/index.js';
6
-
7
- const tableSchema = {
8
- querystring: {
9
- page: { type: 'number' },
10
- order: { type: 'string' },
11
- filter: { type: 'string' },
12
- },
13
- };
14
-
15
- async function plugin(fastify, config = {}) {
16
- const prefix = config.prefix || '/api';
17
- fastify.decorate('metaFormat', metaFormat);
18
-
19
- fastify.get(`${prefix}/suggest/:data`, {}, suggest);
20
- fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
21
- fastify.get(`${prefix}/filter/:table`, {}, filter);
22
- fastify.get(`${prefix}/form/:form`, {}, form);
23
- }
24
-
25
- export default plugin;
1
+ import suggest from './controllers/suggest.js';
2
+ import data from './controllers/data.js';
3
+ import search from './controllers/search.js';
4
+ import filter from './controllers/filter.js';
5
+ import form from './controllers/form.js';
6
+ import metaFormat from './funcs/metaFormat/index.js';
7
+
8
+ const tableSchema = {
9
+ querystring: {
10
+ page: { type: 'number' },
11
+ order: { type: 'string' },
12
+ filter: { type: 'string' },
13
+ },
14
+ };
15
+
16
+ const searchTableSchema = {
17
+ querystring: {
18
+ page: { type: 'number' },
19
+ order: { type: 'string' },
20
+ desc: { type: 'string' },
21
+ filter: { type: 'string' },
22
+ key: { type: 'string' },
23
+ table: { type: 'string' },
24
+ },
25
+ };
26
+
27
+ async function plugin(fastify, config = {}) {
28
+ const prefix = config.prefix || '/api';
29
+ fastify.decorate('metaFormat', metaFormat);
30
+
31
+ fastify.get(`${prefix}/suggest/:data`, {}, suggest);
32
+ fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
33
+ fastify.get(`${prefix}/search`, { schema: searchTableSchema }, search);
34
+ fastify.get(`${prefix}/filter/:table`, {}, filter);
35
+ fastify.get(`${prefix}/form/:form`, {}, form);
36
+ }
37
+
38
+ export default plugin;
@@ -7,7 +7,6 @@ import config from '../config.js';
7
7
  const session = { passport: { user: { uid: config.testUser?.uid || '1' } } };
8
8
 
9
9
  import userNotifications from '../../notification/controllers/userNotifications.js';
10
- import addNotification from '../../notification/funcs/addNotification.js';
11
10
 
12
11
  import pgClients from '../../pg/pgClients.js';
13
12
 
@@ -35,22 +34,4 @@ test('api && funcs notification', async (t) => {
35
34
  const rep = await userNotifications({ pg, session });
36
35
  assert.ok(rep.time);
37
36
  });
38
-
39
- let resp;
40
- await t.test('GET /notification', async () => {
41
- resp = await addNotification({
42
- pg,
43
- session,
44
- title: 'test title',
45
- body: 'test body',
46
- link: 'http://link',
47
- notificationType: 'mention',
48
- uid: session.passport.user.uid,
49
- });
50
- assert.ok(resp.id);
51
- });
52
- await t.test('clean up after test, before pool is closed', async () => {
53
- await pg.query('delete from crm.notification where notification_id=$1', [resp.id]);
54
- console.log(resp.id, 'deleted');
55
- });
56
37
  });
@@ -1,49 +1,57 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- test('api table', async (t) => {
7
- const app = await build(t);
8
- // assert.ok(1);
9
- await t.test('GET /suggest', async () => {
10
- const res = await app.inject({
11
- method: 'GET',
12
- url: '/api/suggest/storage.data',
13
- });
14
- // console.log(res?.body);
15
- const rep = JSON.parse(res?.body);
16
- // console.log(rep.total);
17
- assert.ok(rep.total);
18
- });
19
- await t.test('GET /data', async () => {
20
- const res = await app.inject({
21
- method: 'GET',
22
- url: '/api/data/gis.dataset.table',
23
- });
24
- // console.log(res);
25
- const rep = JSON.parse(res?.body);
26
- // console.log(rep.total);
27
- assert.ok(rep.total);
28
- });
29
- await t.test('GET /form', async () => {
30
- const res = await app.inject({
31
- method: 'GET',
32
- url: '/api/form/cp_building.form',
33
- });
34
- // console.log(res);
35
- const rep = JSON.parse(res?.body);
36
- // console.log(rep.total);
37
- assert.ok(rep);
38
- });
39
- await t.test('GET /filter', async () => {
40
- const res = await app.inject({
41
- method: 'GET',
42
- url: '/api/filter/gis.dataset.table',
43
- });
44
- // console.log(res);
45
- const rep = JSON.parse(res?.body);
46
- // console.log(rep.total);
47
- assert.ok(rep);
48
- });
49
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ test('api table', async (t) => {
7
+ const app = await build(t);
8
+ // assert.ok(1);
9
+ await t.test('GET /suggest', async () => {
10
+ const res = await app.inject({
11
+ method: 'GET',
12
+ url: '/api/suggest/test.storage.data',
13
+ });
14
+ // console.log(res?.body);
15
+ const rep = JSON.parse(res?.body);
16
+ // console.log(rep.total);
17
+ assert.ok(rep.total);
18
+ });
19
+ await t.test('GET /data', async () => {
20
+ const res = await app.inject({
21
+ method: 'GET',
22
+ url: '/api/data/test.dataset.table',
23
+ });
24
+ // console.log(res);
25
+ const rep = JSON.parse(res?.body);
26
+ // console.log(rep.total);
27
+ assert.ok(rep.total);
28
+ });
29
+ await t.test('GET /search', async () => {
30
+ const res = await app.inject({
31
+ method: 'GET',
32
+ url: '/api/search?table=test.dataset.table&key=0',
33
+ });
34
+ const rep = JSON.parse(res?.body);
35
+ assert.ok(rep.total);
36
+ });
37
+ await t.test('GET /form', async () => {
38
+ const res = await app.inject({
39
+ method: 'GET',
40
+ url: '/api/form/test.dataset.form',
41
+ });
42
+ // console.log(res);
43
+ const rep = JSON.parse(res?.body);
44
+ // console.log(rep.total);
45
+ assert.ok(rep);
46
+ });
47
+ await t.test('GET /filter', async () => {
48
+ const res = await app.inject({
49
+ method: 'GET',
50
+ url: '/api/filter/test.dataset.table',
51
+ });
52
+ // console.log(res);
53
+ const rep = JSON.parse(res?.body);
54
+ // console.log(rep.total);
55
+ assert.ok(rep);
56
+ });
57
+ });
@@ -0,0 +1,39 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+ import config from '../config.js';
6
+
7
+ const session = { passport: { user: { uid: config.testUser?.uid || '1' } } };
8
+
9
+ import widgetGet from '../../widget/controllers/widget.get.js';
10
+ import widgetSet from '../../widget/controllers/widget.set.js';
11
+ import widgetDel from '../../widget/controllers/widget.del.js';
12
+
13
+ import pgClients from '../../pg/pgClients.js';
14
+
15
+ test('widget api', async (t) => {
16
+ await build(t);
17
+ const pg = pgClients.client;
18
+ await t.test('POST /widget/:type/:objectid', async () => {
19
+ const body = {};
20
+ const rep = await widgetSet({
21
+ pg, params: { type: 'comment', objectid: '1' }, session, body,
22
+ });
23
+ assert.ok(rep.data);
24
+ });
25
+
26
+ let resp;
27
+ await t.test('GET /widget/:type/:objectid', async () => {
28
+ resp = await widgetGet({
29
+ pg, session, params: { type: 'comment', objectid: '1' },
30
+ });
31
+ assert.ok(resp.rows.length > 0, 'widget data get fail');
32
+ });
33
+ await t.test('DELETE /widget/:type/:objectid', async () => {
34
+ resp = await widgetDel({
35
+ pg, session, params: { type: 'comment', objectid: '1', id: resp.rows?.find((row) => row)?.comment_id },
36
+ });
37
+ assert.ok(resp.data.rowCount === 1, 'widget data delete fail');
38
+ });
39
+ });
@@ -0,0 +1,31 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+ import config from '../config.js';
6
+
7
+ import addNotification from '../../notification/funcs/addNotification.js';
8
+
9
+ import pgClients from '../../pg/pgClients.js';
10
+
11
+ test('notification funcs', async (t) => {
12
+ await build(t);
13
+ const pg = pgClients.client;
14
+ let resp;
15
+ await t.test('addNotification', async () => {
16
+ resp = await addNotification({
17
+ pg,
18
+
19
+ title: 'test title',
20
+ body: 'test body',
21
+ link: 'http://link',
22
+ notificationType: 'mention',
23
+ uid: config.testUser?.uid || '1',
24
+ });
25
+ assert.ok(resp.id);
26
+ });
27
+ await t.test('clean up after test, before pool is closed', async () => {
28
+ await pg.query('delete from crm.notification where notification_id=$1', [resp.id]);
29
+ console.log(resp.id, 'deleted');
30
+ });
31
+ });
@@ -21,8 +21,9 @@ export default async function widgetDel({
21
21
  pg, params = {}, session = {},
22
22
  }) {
23
23
  const { user = {} } = session.passport || {};
24
+ if (!user.uid) return { error: 'access restricted', status: 403 };
24
25
  const { type, objectid, id } = params;
25
- if (!['comment', 'checklist', 'link', 'time'].includes(type)) return { status: 401, error: 'type not valid required' };
26
+ if (!['comment', 'checklist', 'link', 'time'].includes(type)) return { error: 'type not valid required', status: 401 };
26
27
  if (!objectid) return { error: 'id required', status: 400 };
27
28
 
28
29
  const sql = {
@@ -33,7 +34,7 @@ export default async function widgetDel({
33
34
  };
34
35
  try {
35
36
  const { rows, rowCount } = await pg.query(sql[type], [objectid, user.uid, id]);
36
- return { data: { rows, rowCount }, user: { uid: user?.uid, name: user?.user_name } };
37
+ return { data: { rows, rowCount }, user: { uid: user.uid, name: user.user_name } };
37
38
  }
38
39
  catch (err) {
39
40
  return { message: err.toString(), status: 500 };
@@ -36,7 +36,7 @@ export default async function widgetSet({
36
36
  ? `UPDATE ${table} SET ${columns?.map((key, index) => `"${key}"=$${++index}`).join(',')} WHERE ${pkList[type]} = '${id}' `
37
37
  : `INSERT INTO ${table} (${columns?.join(',')}) VALUES (${columns?.map((el, index) => `$${++index}`).join(',')}) returning *;`;
38
38
 
39
- console.log(query, args);
39
+ // console.log(query, args);
40
40
  const result = await pg.query(query, args);
41
41
 
42
42
  return {
package/widget/index.js CHANGED
@@ -1,37 +1,28 @@
1
- const schema = {
2
- params: {
3
- type: { type: 'string' },
4
- objectid: { type: 'string' },
5
- id: { type: 'string' },
6
- },
7
- querystring: {
8
- table: { type: 'string' },
9
- },
10
- };
11
-
12
1
  import widgetDel from './controllers/widget.del.js';
13
2
  import widgetSet from './controllers/widget.set.js';
14
3
  import widgetGet from './controllers/widget.get.js';
15
4
 
16
- async function route(fastify) {
5
+ async function route(fastify, opt) {
6
+ const prefix = opt.prefix || '/api';
17
7
  fastify.route({
18
8
  method: 'DELETE',
19
- url: '/api-user/widget/:type/:objectid/:id',
20
- schema,
9
+ url: `${prefix}/widget/:type/:objectid/:id`,
10
+ schema: {},
21
11
  handler: widgetDel,
22
12
  });
23
13
  fastify.route({
24
14
  method: 'POST',
25
- path: '/api-user/widget/:type/:objectid/:id?',
26
- schema,
15
+ path: `${prefix}/widget/:type/:objectid/:id?`,
16
+ schema: {},
27
17
  handler: widgetSet,
28
18
  });
29
19
  fastify.route({
30
20
  method: 'GET',
31
- path: '/api-user/widget/:type/:objectid',
21
+ path: `${prefix}/widget/:type/:objectid`,
32
22
  config: {
33
23
  policy: ['public'],
34
24
  },
25
+ schema: {},
35
26
  handler: widgetGet,
36
27
  });
37
28
  }