@opengis/fastify-table 1.0.26 → 1.0.27

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 (86) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +81 -81
  3. package/README.md +26 -26
  4. package/config.js +11 -11
  5. package/crud/controllers/deleteCrud.js +14 -14
  6. package/crud/controllers/insert.js +29 -29
  7. package/crud/controllers/update.js +31 -31
  8. package/crud/controllers/utils/checkXSS.js +45 -45
  9. package/crud/controllers/utils/xssInjection.js +72 -72
  10. package/crud/funcs/dataDelete.js +15 -15
  11. package/crud/funcs/dataInsert.js +24 -24
  12. package/crud/funcs/dataUpdate.js +24 -20
  13. package/crud/funcs/getOpt.js +10 -10
  14. package/crud/funcs/getToken.js +27 -27
  15. package/crud/funcs/isFileExists.js +13 -13
  16. package/crud/funcs/setOpt.js +16 -16
  17. package/crud/funcs/setToken.js +53 -53
  18. package/crud/index.js +29 -29
  19. package/dblist/controllers/createItem.js +17 -17
  20. package/dblist/controllers/deleteItem.js +12 -12
  21. package/dblist/controllers/readItems.js +8 -8
  22. package/dblist/controllers/updateItem.js +22 -22
  23. package/dblist/controllers/utils/checkItem.js +9 -9
  24. package/dblist/index.js +40 -40
  25. package/helper.js +28 -28
  26. package/index.js +41 -41
  27. package/notification/controllers/userNotifications.js +19 -19
  28. package/notification/funcs/addNotification.js +8 -8
  29. package/notification/index.js +19 -19
  30. package/package.json +22 -22
  31. package/pg/funcs/getPG.js +29 -29
  32. package/pg/pgClients.js +20 -20
  33. package/policy/funcs/checkPolicy.js +74 -74
  34. package/policy/funcs/sqlInjection.js +33 -33
  35. package/policy/index.js +14 -14
  36. package/redis/client.js +8 -8
  37. package/redis/funcs/getRedis.js +23 -23
  38. package/redis/funcs/redisClients.js +2 -2
  39. package/redis/index.js +19 -19
  40. package/server/migrations/crm.sql +57 -57
  41. package/server/migrations/log.sql +41 -41
  42. package/server/migrations/notifications.sql +14 -14
  43. package/server/templates/form/test.dataset.form.json +411 -411
  44. package/server/templates/select/test.storage.data.json +2 -2
  45. package/server/templates/table/test.dataset.table.json +24 -24
  46. package/server.js +14 -14
  47. package/table/controllers/data.js +57 -57
  48. package/table/controllers/filter.js +32 -32
  49. package/table/controllers/form.js +10 -10
  50. package/table/controllers/search.js +41 -41
  51. package/table/controllers/suggest.js +60 -60
  52. package/table/controllers/utils/getSelect.js +20 -20
  53. package/table/controllers/utils/getSelectMeta.js +66 -66
  54. package/table/controllers/utils/getTemplate.js +28 -28
  55. package/table/funcs/getFilterSQL/index.js +75 -75
  56. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  57. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  58. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  59. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  60. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  61. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  62. package/table/funcs/metaFormat/index.js +26 -26
  63. package/table/index.js +42 -42
  64. package/test/api/crud.test.js +56 -56
  65. package/test/api/crud.xss.test.js +71 -71
  66. package/test/api/dblist.test.js +47 -47
  67. package/test/api/notification.test.js +37 -37
  68. package/test/api/table.test.js +57 -57
  69. package/test/api/widget.test.js +39 -39
  70. package/test/config.example +18 -18
  71. package/test/funcs/crud.test.js +76 -76
  72. package/test/funcs/notification.test.js +31 -31
  73. package/test/funcs/pg.test.js +34 -34
  74. package/test/funcs/redis.test.js +19 -19
  75. package/test/templates/cls/test.json +9 -9
  76. package/test/templates/form/cp_building.form.json +32 -32
  77. package/test/templates/select/account_id.json +3 -3
  78. package/test/templates/select/storage.data.json +2 -2
  79. package/test/templates/table/gis.dataset.table.json +20 -20
  80. package/test/widget.test.js +39 -39
  81. package/widget/controllers/utils/historyFormat.js +76 -76
  82. package/widget/controllers/utils/obj2db.js +13 -13
  83. package/widget/controllers/widget.del.js +40 -40
  84. package/widget/controllers/widget.get.js +74 -74
  85. package/widget/controllers/widget.set.js +45 -45
  86. package/widget/index.js +29 -29
@@ -1,22 +1,22 @@
1
- import { existsSync } from 'fs';
2
- import { readFile, writeFile } from 'fs/promises';
3
-
4
- import checkItem from './utils/checkItem.js';
5
-
6
- export default async function updateItem({ body = {}, params = {} }) {
7
- const { id } = params;
8
- if (!id) return { error: 'not enough params', status: 400 };
9
-
10
- if (!existsSync('dblist.json')) {
11
- return { error: 'nothing to update: 1', status: 400 };
12
- }
13
-
14
- const check = checkItem(body.data);
15
- if (check?.error) return check;
16
-
17
- const data = JSON.parse(await readFile('dblist.json') || '[]');
18
- data.filter((el) => el.id === id)?.forEach((el) => Object.assign(el, body.data));
19
-
20
- await writeFile('dblist.json', JSON.stringify(data));
21
- return { data };
22
- }
1
+ import { existsSync } from 'fs';
2
+ import { readFile, writeFile } from 'fs/promises';
3
+
4
+ import checkItem from './utils/checkItem.js';
5
+
6
+ export default async function updateItem({ body = {}, params = {} }) {
7
+ const { id } = params;
8
+ if (!id) return { error: 'not enough params', status: 400 };
9
+
10
+ if (!existsSync('dblist.json')) {
11
+ return { error: 'nothing to update: 1', status: 400 };
12
+ }
13
+
14
+ const check = checkItem(body.data);
15
+ if (check?.error) return check;
16
+
17
+ const data = JSON.parse(await readFile('dblist.json') || '[]');
18
+ data.filter((el) => el.id === id)?.forEach((el) => Object.assign(el, body.data));
19
+
20
+ await writeFile('dblist.json', JSON.stringify(data));
21
+ return { data };
22
+ }
@@ -1,9 +1,9 @@
1
- export default function validateItem(item) {
2
- if (!item || typeof item !== 'object' || Array.isArray(item)) {
3
- return { error: 'param data is invalid: 1', status: 400 };
4
- }
5
- if (!item.options?.database || Object.keys(item).filter((key) => !['key', 'name', 'options'].includes(key) || (typeof item[key] !== 'string' && key !== 'options')).length) {
6
- return { error: 'param data is invalid: 2', status: 400 };
7
- }
8
- return null;
9
- }
1
+ export default function validateItem(item) {
2
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
3
+ return { error: 'param data is invalid: 1', status: 400 };
4
+ }
5
+ if (!item.options?.database || Object.keys(item).filter((key) => !['key', 'name', 'options'].includes(key) || (typeof item[key] !== 'string' && key !== 'options')).length) {
6
+ return { error: 'param data is invalid: 2', status: 400 };
7
+ }
8
+ return null;
9
+ }
package/dblist/index.js CHANGED
@@ -1,40 +1,40 @@
1
- import createItem from './controllers/createItem.js';
2
- import readItemList from './controllers/readItems.js';
3
- import updateItem from './controllers/updateItem.js';
4
- import deleteItem from './controllers/deleteItem.js';
5
-
6
- export default async function plugin(fastify, config = {}) {
7
- const prefix = config.prefix || '/api';
8
- fastify.route({
9
- method: 'POST',
10
- url: `${prefix}/list`,
11
- config: {
12
- policy: [],
13
- },
14
- handler: createItem,
15
- });
16
- fastify.route({
17
- method: 'GET',
18
- url: `${prefix}/list`,
19
- config: {
20
- policy: [],
21
- },
22
- handler: readItemList,
23
- });
24
- fastify.route({
25
- method: 'PUT',
26
- url: `${prefix}/list/:id`,
27
- config: {
28
- policy: [],
29
- },
30
- handler: updateItem,
31
- });
32
- fastify.route({
33
- method: 'DELETE',
34
- url: `${prefix}/list/:id`,
35
- config: {
36
- policy: [],
37
- },
38
- handler: deleteItem,
39
- });
40
- }
1
+ import createItem from './controllers/createItem.js';
2
+ import readItemList from './controllers/readItems.js';
3
+ import updateItem from './controllers/updateItem.js';
4
+ import deleteItem from './controllers/deleteItem.js';
5
+
6
+ export default async function plugin(fastify, config = {}) {
7
+ const prefix = config.prefix || '/api';
8
+ fastify.route({
9
+ method: 'POST',
10
+ url: `${prefix}/list`,
11
+ config: {
12
+ policy: [],
13
+ },
14
+ handler: createItem,
15
+ });
16
+ fastify.route({
17
+ method: 'GET',
18
+ url: `${prefix}/list`,
19
+ config: {
20
+ policy: [],
21
+ },
22
+ handler: readItemList,
23
+ });
24
+ fastify.route({
25
+ method: 'PUT',
26
+ url: `${prefix}/list/:id`,
27
+ config: {
28
+ policy: [],
29
+ },
30
+ handler: updateItem,
31
+ });
32
+ fastify.route({
33
+ method: 'DELETE',
34
+ url: `${prefix}/list/:id`,
35
+ config: {
36
+ policy: [],
37
+ },
38
+ handler: deleteItem,
39
+ });
40
+ }
package/helper.js CHANGED
@@ -1,28 +1,28 @@
1
- // This file contains code that we reuse
2
- // between our tests.
3
- import Fastify from 'fastify';
4
- import config from './test/config.js';
5
- import appService from './index.js';
6
-
7
- import rclient from './redis/client.js';
8
- import pgClients from './pg/pgClients.js';
9
-
10
- // automatically build and tear down our instance
11
- async function build(t) {
12
- // you can set all the options supported by the fastify CLI command
13
- // const argv = [AppPath]
14
- process.env.NODE_ENV = 'production';
15
- const app = Fastify({ logger: false });
16
- app.register(appService, config);
17
- // close the app after we are done
18
- t.after(() => {
19
- // console.log('close app');
20
- pgClients.client.end();
21
- rclient.quit();
22
- app.close();
23
- });
24
-
25
- return app;
26
- }
27
-
28
- export default build;
1
+ // This file contains code that we reuse
2
+ // between our tests.
3
+ import Fastify from 'fastify';
4
+ import config from './test/config.js';
5
+ import appService from './index.js';
6
+
7
+ import rclient from './redis/client.js';
8
+ import pgClients from './pg/pgClients.js';
9
+
10
+ // automatically build and tear down our instance
11
+ async function build(t) {
12
+ // you can set all the options supported by the fastify CLI command
13
+ // const argv = [AppPath]
14
+ process.env.NODE_ENV = 'production';
15
+ const app = Fastify({ logger: false });
16
+ app.register(appService, config);
17
+ // close the app after we are done
18
+ t.after(() => {
19
+ // console.log('close app');
20
+ pgClients.client.end();
21
+ rclient.quit();
22
+ app.close();
23
+ });
24
+
25
+ return app;
26
+ }
27
+
28
+ export default build;
package/index.js CHANGED
@@ -1,41 +1,41 @@
1
- // import pg from 'pg';
2
- import fp from 'fastify-plugin';
3
- import config from './config.js';
4
- // import rclient from './redis/client.js';
5
-
6
- import redisPlugin from './redis/index.js';
7
- import pgPlugin from './pg/index.js';
8
- import tablePlugin from './table/index.js';
9
- import notificationPlugin from './notification/index.js';
10
- import widgetPlugin from './widget/index.js';
11
- import crudPlugin from './crud/index.js';
12
- import policyPlugin from './policy/index.js';
13
- import dbListPlugin from './dblist/index.js';
14
-
15
- async function plugin(fastify, opt) {
16
- // console.log(opt);
17
- config.pg = opt.pg;
18
- config.redis = opt.redis;
19
-
20
- // independent npm start / unit test
21
- if (!fastify.config) {
22
- fastify.decorate('config', config);
23
- }
24
- if (!fastify.funcs) {
25
- fastify.addHook('onRequest', async (req) => {
26
- req.funcs = fastify;
27
- });
28
- // fastify.decorateRequest('funcs', fastify);
29
- }
30
-
31
- policyPlugin(fastify);
32
- redisPlugin(fastify);
33
- await pgPlugin(fastify, opt);
34
- tablePlugin(fastify, opt);
35
- crudPlugin(fastify, opt);
36
- notificationPlugin(fastify, opt);
37
- widgetPlugin(fastify, opt);
38
- dbListPlugin(fastify, opt);
39
- }
40
- export default fp(plugin);
41
- // export { rclient };
1
+ // import pg from 'pg';
2
+ import fp from 'fastify-plugin';
3
+ import config from './config.js';
4
+ // import rclient from './redis/client.js';
5
+
6
+ import redisPlugin from './redis/index.js';
7
+ import pgPlugin from './pg/index.js';
8
+ import tablePlugin from './table/index.js';
9
+ import notificationPlugin from './notification/index.js';
10
+ import widgetPlugin from './widget/index.js';
11
+ import crudPlugin from './crud/index.js';
12
+ import policyPlugin from './policy/index.js';
13
+ import dbListPlugin from './dblist/index.js';
14
+
15
+ async function plugin(fastify, opt) {
16
+ // console.log(opt);
17
+ config.pg = opt.pg;
18
+ config.redis = opt.redis;
19
+
20
+ // independent npm start / unit test
21
+ if (!fastify.config) {
22
+ fastify.decorate('config', config);
23
+ }
24
+ if (!fastify.funcs) {
25
+ fastify.addHook('onRequest', async (req) => {
26
+ req.funcs = fastify;
27
+ });
28
+ // fastify.decorateRequest('funcs', fastify);
29
+ }
30
+
31
+ policyPlugin(fastify);
32
+ redisPlugin(fastify);
33
+ await pgPlugin(fastify, opt);
34
+ tablePlugin(fastify, opt);
35
+ crudPlugin(fastify, opt);
36
+ notificationPlugin(fastify, opt);
37
+ widgetPlugin(fastify, opt);
38
+ dbListPlugin(fastify, opt);
39
+ }
40
+ export default fp(plugin);
41
+ // export { rclient };
@@ -1,19 +1,19 @@
1
- export default async function userNotifications({
2
- pg, session = {}, query = {},
3
- }) {
4
- const time = Date.now();
5
- try {
6
- const { uid } = session.passport?.user || {};
7
- if (!uid) return { error: 'access restricted', status: 403 };
8
-
9
- const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
-
11
- // queryCache not supports $1 params
12
- const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
- notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
- return { time: Date.now() - time, total: rows?.length, rows };
15
- }
16
- catch (err) {
17
- return { error: err.toString(), status: 500 };
18
- }
19
- }
1
+ export default async function userNotifications({
2
+ pg, session = {}, query = {},
3
+ }) {
4
+ const time = Date.now();
5
+ try {
6
+ const { uid } = session.passport?.user || {};
7
+ if (!uid) return { error: 'access restricted', status: 403 };
8
+
9
+ const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
+
11
+ // queryCache not supports $1 params
12
+ const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
+ notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
+ return { time: Date.now() - time, total: rows?.length, rows };
15
+ }
16
+ catch (err) {
17
+ return { error: err.toString(), status: 500 };
18
+ }
19
+ }
@@ -1,8 +1,8 @@
1
- export default async function addNotification({
2
- pg, session = {}, title, body, link, notificationType, uid: uid1,
3
- }) {
4
- const uid = uid1 || session.passport?.user?.uid || {};
5
- const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
- values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
- return { id, status };
8
- }
1
+ export default async function addNotification({
2
+ pg, session = {}, title, body, link, notificationType, uid: uid1,
3
+ }) {
4
+ const uid = uid1 || session.passport?.user?.uid || {};
5
+ const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
+ values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
+ return { id, status };
8
+ }
@@ -1,19 +1,19 @@
1
- // api
2
- import userNotifications from './controllers/userNotifications.js';
3
- // funcs
4
- import addNotification from './funcs/addNotification.js';
5
-
6
- async function plugin(fastify, config = {}) {
7
- const prefix = config.prefix || '/api';
8
- fastify.route({
9
- method: 'GET',
10
- url: `${prefix}/notification`,
11
- config: {
12
- policy: ['user'], // implement user auth check policy??
13
- },
14
- handler: userNotifications,
15
- });
16
- fastify.decorate('addNotification', addNotification);
17
- }
18
-
19
- export default plugin;
1
+ // api
2
+ import userNotifications from './controllers/userNotifications.js';
3
+ // funcs
4
+ import addNotification from './funcs/addNotification.js';
5
+
6
+ async function plugin(fastify, config = {}) {
7
+ const prefix = config.prefix || '/api';
8
+ fastify.route({
9
+ method: 'GET',
10
+ url: `${prefix}/notification`,
11
+ config: {
12
+ policy: ['user'], // implement user auth check policy??
13
+ },
14
+ handler: userNotifications,
15
+ });
16
+ fastify.decorate('addNotification', addNotification);
17
+ }
18
+
19
+ export default plugin;
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "@opengis/fastify-table",
3
- "version": "1.0.26",
4
- "type": "module",
5
- "description": "core-plugins",
6
- "main": "index.js",
7
- "scripts": {
8
- "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
9
- "test": "echo \"Error: no test specified\" && exit 1"
10
- },
11
- "dependencies": {
12
- "ioredis": "^5.3.2",
13
- "fastify": "^4.26.1",
14
- "fastify-plugin": "^4.0.0",
15
- "pg": "^8.11.3"
16
- },
17
- "devDependencies": {
18
- "eslint": "^8.49.0",
19
- "eslint-config-airbnb": "^19.0.4"
20
- },
21
- "author": "Softpro",
22
- "license": "ISC"
1
+ {
2
+ "name": "@opengis/fastify-table",
3
+ "version": "1.0.27",
4
+ "type": "module",
5
+ "description": "core-plugins",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "dependencies": {
12
+ "ioredis": "^5.3.2",
13
+ "fastify": "^4.26.1",
14
+ "fastify-plugin": "^4.0.0",
15
+ "pg": "^8.11.3"
16
+ },
17
+ "devDependencies": {
18
+ "eslint": "^8.49.0",
19
+ "eslint-config-airbnb": "^19.0.4"
20
+ },
21
+ "author": "Softpro",
22
+ "license": "ISC"
23
23
  }
package/pg/funcs/getPG.js CHANGED
@@ -1,29 +1,29 @@
1
- import pg from 'pg';
2
- import config from '../../config.js';
3
- import pgClients from '../pgClients.js';
4
- import init from './init.js';
5
-
6
- function getPG({
7
- user, password, host, port, db, database, name: origin, funcs,
8
- }) {
9
- if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
10
- const name = origin || db || database;
11
- if (pgClients[name]) return pgClients[name];
12
-
13
- const dbConfig = {
14
- user: user || config.pg?.user,
15
- password: password || config.pg?.password,
16
- host: host || config.pg?.host,
17
- port: port || config.pg?.port,
18
- database: db || database || config.pg?.db || config.pg?.database,
19
- };
20
-
21
- pgClients[name] = new pg.Pool(dbConfig);
22
- pgClients[name].init = async () => {
23
- await init(pgClients[name]);
24
- };
25
- init(pgClients[name]);
26
- return pgClients[name];
27
- }
28
-
29
- export default getPG;
1
+ import pg from 'pg';
2
+ import config from '../../config.js';
3
+ import pgClients from '../pgClients.js';
4
+ import init from './init.js';
5
+
6
+ function getPG({
7
+ user, password, host, port, db, database, name: origin, funcs,
8
+ } = { name: 'client' }) {
9
+ if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
10
+ const name = origin || db || database;
11
+ if (pgClients[name]) return pgClients[name];
12
+
13
+ const dbConfig = {
14
+ user: user || config.pg?.user,
15
+ password: password || config.pg?.password,
16
+ host: host || config.pg?.host,
17
+ port: port || config.pg?.port,
18
+ database: db || database || config.pg?.db || config.pg?.database,
19
+ };
20
+
21
+ pgClients[name] = new pg.Pool(dbConfig);
22
+ pgClients[name].init = async () => {
23
+ await init(pgClients[name]);
24
+ };
25
+ init(pgClients[name]);
26
+ return pgClients[name];
27
+ }
28
+
29
+ export default getPG;
package/pg/pgClients.js CHANGED
@@ -1,20 +1,20 @@
1
- import pg from 'pg';
2
- import config from '../config.js';
3
- import init from './funcs/init.js';
4
-
5
- const pgClients = {};
6
- if (config.pg) {
7
- const client = new pg.Pool({
8
- host: config.pg?.host || '127.0.0.1',
9
- port: config.pg?.port || 5432,
10
- database: config.pg?.database || 'postgres',
11
- user: config.pg?.user || 'postgres',
12
- password: config.pg?.password || 'postgres',
13
- });
14
- client.init = async () => {
15
- await init(client);
16
- };
17
- client.init();
18
- pgClients.client = client;
19
- }
20
- export default pgClients;
1
+ import pg from 'pg';
2
+ import config from '../config.js';
3
+ import init from './funcs/init.js';
4
+
5
+ const pgClients = {};
6
+ if (config.pg) {
7
+ const client = new pg.Pool({
8
+ host: config.pg?.host || '127.0.0.1',
9
+ port: config.pg?.port || 5432,
10
+ database: config.pg?.database || 'postgres',
11
+ user: config.pg?.user || 'postgres',
12
+ password: config.pg?.password || 'postgres',
13
+ });
14
+ client.init = async () => {
15
+ await init(client);
16
+ };
17
+ client.init();
18
+ pgClients.client = client;
19
+ }
20
+ export default pgClients;