@opengis/fastify-table 1.1.8 → 1.1.9

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 (110) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +305 -301
  3. package/README.md +26 -26
  4. package/config.js +10 -10
  5. package/cron/controllers/cronApi.js +22 -22
  6. package/cron/controllers/utils/cronList.js +1 -1
  7. package/cron/funcs/addCron.js +131 -131
  8. package/cron/index.js +10 -10
  9. package/crud/controllers/deleteCrud.js +22 -22
  10. package/crud/controllers/insert.js +61 -61
  11. package/crud/controllers/update.js +62 -62
  12. package/crud/controllers/utils/checkXSS.js +45 -45
  13. package/crud/controllers/utils/xssInjection.js +72 -72
  14. package/crud/funcs/dataDelete.js +19 -19
  15. package/crud/funcs/dataInsert.js +30 -30
  16. package/crud/funcs/dataUpdate.js +36 -30
  17. package/crud/funcs/getAccess.js +53 -53
  18. package/crud/funcs/getOpt.js +10 -10
  19. package/crud/funcs/getToken.js +27 -27
  20. package/crud/funcs/isFileExists.js +13 -13
  21. package/crud/funcs/setOpt.js +16 -16
  22. package/crud/funcs/setToken.js +53 -53
  23. package/crud/funcs/utils/logChanges.js +76 -74
  24. package/crud/index.js +36 -36
  25. package/helper.js +28 -28
  26. package/index.js +97 -97
  27. package/migration/exec.migrations.js +79 -79
  28. package/notification/controllers/testEmail.js +49 -49
  29. package/notification/controllers/userNotifications.js +19 -19
  30. package/notification/funcs/addNotification.js +8 -8
  31. package/notification/funcs/sendNotification.js +111 -111
  32. package/notification/funcs/utils/sendEmail.js +39 -39
  33. package/notification/index.js +38 -38
  34. package/package.json +26 -26
  35. package/pg/funcs/getMeta.js +27 -27
  36. package/pg/funcs/getPG.js +30 -30
  37. package/pg/pgClients.js +20 -20
  38. package/policy/funcs/checkPolicy.js +83 -83
  39. package/policy/funcs/sqlInjection.js +33 -33
  40. package/policy/index.js +14 -14
  41. package/redis/client.js +8 -8
  42. package/redis/funcs/getRedis.js +23 -23
  43. package/redis/funcs/redisClients.js +2 -2
  44. package/redis/index.js +19 -19
  45. package/server/migrations/0.sql +78 -78
  46. package/server/migrations/crm.sql +150 -150
  47. package/server/migrations/log.sql +80 -80
  48. package/server/migrations/properties.sql +30 -30
  49. package/server/migrations/roles.sql +164 -164
  50. package/server/migrations/users.sql +89 -89
  51. package/server.js +14 -14
  52. package/table/controllers/card.js +44 -44
  53. package/table/controllers/data.js +103 -103
  54. package/table/controllers/filter.js +37 -37
  55. package/table/controllers/form.js +28 -28
  56. package/table/controllers/search.js +80 -80
  57. package/table/controllers/suggest.js +79 -79
  58. package/table/controllers/table.js +52 -52
  59. package/table/controllers/utils/addTemplateDir.js +8 -8
  60. package/table/controllers/utils/getSelect.js +19 -19
  61. package/table/controllers/utils/getSelectMeta.js +66 -66
  62. package/table/controllers/utils/getTemplate_old.js +28 -28
  63. package/table/controllers/utils/getTemplates.js +18 -18
  64. package/table/controllers/utils/gisIRColumn.js +68 -68
  65. package/table/controllers/utils/loadTemplate.js +1 -1
  66. package/table/controllers/utils/loadTemplatePath.js +1 -1
  67. package/table/controllers/utils/userTemplateDir.js +1 -1
  68. package/table/funcs/getFilterSQL/index.js +79 -79
  69. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  70. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  71. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  72. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  73. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  74. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  75. package/table/funcs/metaFormat/index.js +28 -28
  76. package/table/index.js +84 -84
  77. package/test/api/crud.test.js +89 -89
  78. package/test/api/crud.xss.test.js +72 -72
  79. package/test/api/notification.test.js +37 -37
  80. package/test/api/suggest.test.js +66 -66
  81. package/test/api/table.test.js +89 -89
  82. package/test/api/widget.test.js +117 -117
  83. package/test/config.example +18 -18
  84. package/test/funcs/pg.test.js +34 -34
  85. package/test/funcs/redis.test.js +19 -19
  86. package/test/templates/cls/test.json +9 -9
  87. package/test/templates/form/cp_building.form.json +32 -32
  88. package/test/templates/select/account_id.json +3 -3
  89. package/test/templates/select/storage.data.json +2 -2
  90. package/test/templates/select/test.storage.data.json +3 -3
  91. package/test/templates/select/test.suggest.ato_new.json +3 -3
  92. package/test/templates/select/test.suggest.ato_new.sql +25 -25
  93. package/test/templates/select/test.suggest.data.json +4 -4
  94. package/test/templates/select/test.suggest.parent.sql +1 -1
  95. package/test/templates/table/gis.dataset.table.json +20 -20
  96. package/util/controllers/logger.file.js +90 -90
  97. package/util/controllers/next.id.js +4 -4
  98. package/util/controllers/properties.add.js +57 -57
  99. package/util/controllers/properties.get.js +19 -19
  100. package/util/controllers/status.monitor.js +8 -8
  101. package/util/controllers/utils/checkUserAccess.js +19 -19
  102. package/util/controllers/utils/getRootDir.js +20 -20
  103. package/util/index.js +23 -23
  104. package/utils.js +48 -48
  105. package/widget/controllers/utils/historyFormat.js +76 -76
  106. package/widget/controllers/utils/obj2db.js +13 -13
  107. package/widget/controllers/widget.del.js +44 -44
  108. package/widget/controllers/widget.get.js +98 -98
  109. package/widget/controllers/widget.set.js +76 -76
  110. package/widget/index.js +40 -40
@@ -1,111 +1,111 @@
1
- // eslint-disable-next-line max-len, no-control-regex
2
- const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
-
4
- // eslint-disable-next-line import/no-relative-packages
5
- // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
- import pgClients from '../../pg/pgClients.js';
7
- import sendEmail from './utils/sendEmail.js';
8
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
9
- import getRedis from '../../redis/funcs/getRedis.js';
10
-
11
- async function generateNotificationContent({
12
- pg, funcs, table, template, id, message, data: data1,
13
- }) {
14
- if (template) {
15
- const data = table && id ? await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]) : data1;
16
- // console.log(data);
17
- const body = await getTemplate('pt', template);
18
- // console.log(body);
19
- const html = funcs.handlebars.compile(body || template || 'template not found')(data || {});
20
- // console.log(html);
21
- return html;
22
- }
23
- return message;
24
- }
25
-
26
- export default async function notification({
27
- pg: pg1,
28
- funcs,
29
- log,
30
- provider = ['email'],
31
- from,
32
- to,
33
- template,
34
- table,
35
- message,
36
- title,
37
- file,
38
- data,
39
- id,
40
- nocache,
41
- }) {
42
- const rclient = getRedis();
43
- const pg = pg1 || pgClients.client;
44
-
45
- if (!pg) throw new Error('need pg');
46
-
47
- if (pg?.readonly) {
48
- return null;
49
- }
50
-
51
- const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
52
- const uniqueTo = await rclient.setnx(keyTo, 1);
53
- log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
54
-
55
- if (!uniqueTo && !nocache) {
56
- return `already sent: ${keyTo}`;
57
- }
58
- await rclient.expire(keyTo, 1000 * 600);
59
-
60
- if (!to.length) {
61
- return null;
62
- }
63
-
64
- if (!(Array.isArray(provider) && provider.length)) {
65
- return 'notification provider - must be array and not empty';
66
- }
67
-
68
- try {
69
- const html = await generateNotificationContent({
70
- pg, funcs, table, template, id, message, data,
71
- });
72
-
73
- // return html;
74
-
75
- if (provider.includes('email')) {
76
- const files = Array.isArray(file) ? file : [file];
77
- const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
78
- const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
79
- return {
80
- filename: el.split('/').pop(),
81
- encoding: 'base64',
82
- content,
83
- };
84
- })) : [];
85
-
86
- const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
87
-
88
- const result = await sendEmail({
89
- funcs,
90
- attachments,
91
- html,
92
- subject: title,
93
- from,
94
- to: toEmail,
95
- });
96
-
97
- log.info('notification', {
98
- provider, title, to: toEmail, from, result, len: message?.length, files,
99
- });
100
-
101
- return result;
102
- }
103
- return null;
104
- }
105
- catch (err) {
106
- log.info('notification/error', {
107
- provider, from, to, err: err.toString(),
108
- });
109
- throw err;
110
- }
111
- }
1
+ // eslint-disable-next-line max-len, no-control-regex
2
+ const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
+
4
+ // eslint-disable-next-line import/no-relative-packages
5
+ // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
+ import pgClients from '../../pg/pgClients.js';
7
+ import sendEmail from './utils/sendEmail.js';
8
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
9
+ import getRedis from '../../redis/funcs/getRedis.js';
10
+
11
+ async function generateNotificationContent({
12
+ pg, funcs, table, template, id, message, data: data1,
13
+ }) {
14
+ if (template) {
15
+ const data = table && id ? await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]) : data1;
16
+ // console.log(data);
17
+ const body = await getTemplate('pt', template);
18
+ // console.log(body);
19
+ const html = funcs.handlebars.compile(body || template || 'template not found')(data || {});
20
+ // console.log(html);
21
+ return html;
22
+ }
23
+ return message;
24
+ }
25
+
26
+ export default async function notification({
27
+ pg: pg1,
28
+ funcs,
29
+ log,
30
+ provider = ['email'],
31
+ from,
32
+ to,
33
+ template,
34
+ table,
35
+ message,
36
+ title,
37
+ file,
38
+ data,
39
+ id,
40
+ nocache,
41
+ }) {
42
+ const rclient = getRedis();
43
+ const pg = pg1 || pgClients.client;
44
+
45
+ if (!pg) throw new Error('need pg');
46
+
47
+ if (pg?.readonly) {
48
+ return null;
49
+ }
50
+
51
+ const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
52
+ const uniqueTo = await rclient.setnx(keyTo, 1);
53
+ log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
54
+
55
+ if (!uniqueTo && !nocache) {
56
+ return `already sent: ${keyTo}`;
57
+ }
58
+ await rclient.expire(keyTo, 1000 * 600);
59
+
60
+ if (!to.length) {
61
+ return null;
62
+ }
63
+
64
+ if (!(Array.isArray(provider) && provider.length)) {
65
+ return 'notification provider - must be array and not empty';
66
+ }
67
+
68
+ try {
69
+ const html = await generateNotificationContent({
70
+ pg, funcs, table, template, id, message, data,
71
+ });
72
+
73
+ // return html;
74
+
75
+ if (provider.includes('email')) {
76
+ const files = Array.isArray(file) ? file : [file];
77
+ const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
78
+ const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
79
+ return {
80
+ filename: el.split('/').pop(),
81
+ encoding: 'base64',
82
+ content,
83
+ };
84
+ })) : [];
85
+
86
+ const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
87
+
88
+ const result = await sendEmail({
89
+ funcs,
90
+ attachments,
91
+ html,
92
+ subject: title,
93
+ from,
94
+ to: toEmail,
95
+ });
96
+
97
+ log.info('notification', {
98
+ provider, title, to: toEmail, from, result, len: message?.length, files,
99
+ });
100
+
101
+ return result;
102
+ }
103
+ return null;
104
+ }
105
+ catch (err) {
106
+ log.info('notification/error', {
107
+ provider, from, to, err: err.toString(),
108
+ });
109
+ throw err;
110
+ }
111
+ }
@@ -1,39 +1,39 @@
1
- import nodemailer from 'nodemailer';
2
-
3
- /**
4
- * Надсилає поваідомлення на пошту
5
- *
6
- * @type function
7
- * @alias sendEmail
8
- * @summary Функція здійснює розсилку по email
9
- */
10
-
11
- export default async function sendEmail({
12
- funcs, to, from, subject, html, attachments,
13
- }) {
14
- const { config = {} } = funcs;
15
-
16
- if (!to?.length) {
17
- throw new Error('empty to list');
18
- }
19
-
20
- const { mailSetting = {} } = config;
21
-
22
- /*= == check service and setting === */
23
- if (!mailSetting.service) {
24
- throw new Error('service is not defined in config');
25
- }
26
-
27
- Object.assign(mailSetting, { rejectUnauthorized: false });
28
-
29
- if (mailSetting.port === 465) {
30
- Object.assign(mailSetting, { secure: true });
31
- }
32
-
33
- const transport = nodemailer.createTransport(mailSetting);
34
-
35
- const result = await transport.sendMail({
36
- from: from || mailSetting.from, to, subject, html, attachments,
37
- });
38
- return result;
39
- }
1
+ import nodemailer from 'nodemailer';
2
+
3
+ /**
4
+ * Надсилає поваідомлення на пошту
5
+ *
6
+ * @type function
7
+ * @alias sendEmail
8
+ * @summary Функція здійснює розсилку по email
9
+ */
10
+
11
+ export default async function sendEmail({
12
+ funcs, to, from, subject, html, attachments,
13
+ }) {
14
+ const { config = {} } = funcs;
15
+
16
+ if (!to?.length) {
17
+ throw new Error('empty to list');
18
+ }
19
+
20
+ const { mailSetting = {} } = config;
21
+
22
+ /*= == check service and setting === */
23
+ if (!mailSetting.service) {
24
+ throw new Error('service is not defined in config');
25
+ }
26
+
27
+ Object.assign(mailSetting, { rejectUnauthorized: false });
28
+
29
+ if (mailSetting.port === 465) {
30
+ Object.assign(mailSetting, { secure: true });
31
+ }
32
+
33
+ const transport = nodemailer.createTransport(mailSetting);
34
+
35
+ const result = await transport.sendMail({
36
+ from: from || mailSetting.from, to, subject, html, attachments,
37
+ });
38
+ return result;
39
+ }
@@ -1,38 +1,38 @@
1
- // api
2
- import userNotifications from './controllers/userNotifications.js';
3
- import testEmail from './controllers/testEmail.js';
4
- // funcs
5
- import addNotification from './funcs/addNotification.js'; // add to db
6
- import notification from './funcs/sendNotification.js'; // send
7
-
8
- const tableSchema = {
9
- querystring: {
10
- nocache: { type: 'string', pattern: '^(\\d+)$' },
11
- },
12
- };
13
-
14
- async function plugin(fastify, config = {}) {
15
- const prefix = config.prefix || '/api';
16
- fastify.route({
17
- method: 'GET',
18
- url: `${prefix}/notification`,
19
- config: {
20
- policy: ['user'], // implement user auth check policy??
21
- },
22
- schema: tableSchema,
23
- handler: userNotifications,
24
- });
25
- fastify.route({
26
- method: 'GET',
27
- url: `${prefix}/test-email`,
28
- config: {
29
- policy: ['user'],
30
- },
31
- handler: testEmail,
32
- });
33
-
34
- fastify.decorate('addNotification', addNotification);
35
- fastify.decorate('notification', notification);
36
- }
37
-
38
- export default plugin;
1
+ // api
2
+ import userNotifications from './controllers/userNotifications.js';
3
+ import testEmail from './controllers/testEmail.js';
4
+ // funcs
5
+ import addNotification from './funcs/addNotification.js'; // add to db
6
+ import notification from './funcs/sendNotification.js'; // send
7
+
8
+ const tableSchema = {
9
+ querystring: {
10
+ nocache: { type: 'string', pattern: '^(\\d+)$' },
11
+ },
12
+ };
13
+
14
+ async function plugin(fastify, config = {}) {
15
+ const prefix = config.prefix || '/api';
16
+ fastify.route({
17
+ method: 'GET',
18
+ url: `${prefix}/notification`,
19
+ config: {
20
+ policy: ['user'], // implement user auth check policy??
21
+ },
22
+ schema: tableSchema,
23
+ handler: userNotifications,
24
+ });
25
+ fastify.route({
26
+ method: 'GET',
27
+ url: `${prefix}/test-email`,
28
+ config: {
29
+ policy: ['user'],
30
+ },
31
+ handler: testEmail,
32
+ });
33
+
34
+ fastify.decorate('addNotification', addNotification);
35
+ fastify.decorate('notification', notification);
36
+ }
37
+
38
+ export default plugin;
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "@opengis/fastify-table",
3
- "version": "1.1.8",
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": "node --test"
10
- },
11
- "dependencies": {
12
- "@opengis/fastify-hb": "^1.1.0",
13
- "@fastify/sensible": "^5.0.0",
14
- "@fastify/url-data": "^5.4.0",
15
- "fastify": "^4.26.1",
16
- "fastify-plugin": "^4.0.0",
17
- "ioredis": "^5.3.2",
18
- "pg": "^8.11.3",
19
- "nodemailer": "^6.5.0"
20
- },
21
- "devDependencies": {
22
- "eslint": "^8.49.0",
23
- "eslint-config-airbnb": "^19.0.4"
24
- },
25
- "author": "Softpro",
26
- "license": "ISC"
1
+ {
2
+ "name": "@opengis/fastify-table",
3
+ "version": "1.1.9",
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": "node --test"
10
+ },
11
+ "dependencies": {
12
+ "@opengis/fastify-hb": "^1.1.0",
13
+ "@fastify/sensible": "^5.0.0",
14
+ "@fastify/url-data": "^5.4.0",
15
+ "fastify": "^4.26.1",
16
+ "fastify-plugin": "^4.0.0",
17
+ "ioredis": "^5.3.2",
18
+ "pg": "^8.11.3",
19
+ "nodemailer": "^6.5.0"
20
+ },
21
+ "devDependencies": {
22
+ "eslint": "^8.49.0",
23
+ "eslint-config-airbnb": "^19.0.4"
24
+ },
25
+ "author": "Softpro",
26
+ "license": "ISC"
27
27
  }
@@ -1,27 +1,27 @@
1
- // import pgClient from '../pgClients.js';
2
- import getPG from './getPG.js';
3
-
4
- const data = {};
5
-
6
- // decorator
7
- export default async function getMeta(opt) {
8
- const pg = opt.pg || getPG({ name: 'client' });
9
- const table = opt.table || opt;
10
-
11
- if (data[table]) return data[table];
12
-
13
- if (!pg.tlist?.includes(table)) {
14
- return { error: `${table} - not found`, status: 400 };
15
- }
16
-
17
- const { fields } = await pg.query(`select * from ${table} where $1=$1 limit 0`, [1]);
18
- const { pks1 } = await pg.one(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1 FROM pg_constraint c
19
- left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1] WHERE c.contype='p'::"char"`, { cache: 0 });
20
- const pk = table.startsWith('public.') ? pks1[table.replace('public.', '')] : pks1[table];
21
-
22
- const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
23
-
24
- const res = { pk, columns: fields, geom: geomAttr };
25
- data[table] = res;
26
- return res;
27
- }
1
+ // import pgClient from '../pgClients.js';
2
+ import getPG from './getPG.js';
3
+
4
+ const data = {};
5
+
6
+ // decorator
7
+ export default async function getMeta(opt) {
8
+ const pg = opt.pg || getPG({ name: 'client' });
9
+ const table = opt.table || opt;
10
+
11
+ if (data[table]) return data[table];
12
+
13
+ if (!pg.tlist?.includes(table)) {
14
+ return { error: `${table} - not found`, status: 400 };
15
+ }
16
+
17
+ const { fields } = await pg.query(`select * from ${table} where $1=$1 limit 0`, [1]);
18
+ const { pks1 } = await pg.one(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1 FROM pg_constraint c
19
+ left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1] WHERE c.contype='p'::"char"`, { cache: 0 });
20
+ const pk = table.startsWith('public.') ? pks1[table.replace('public.', '')] : pks1[table];
21
+
22
+ const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
23
+
24
+ const res = { pk, columns: fields, geom: geomAttr };
25
+ data[table] = res;
26
+ return res;
27
+ }
package/pg/funcs/getPG.js CHANGED
@@ -1,30 +1,30 @@
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(param) {
7
- const {
8
- user, password, host, port, db, database, name: origin, funcs,
9
- } = param || {};
10
- if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
11
- const name = origin || db || database || param || 'client';
12
- if (pgClients[name]) return pgClients[name];
13
-
14
- const dbConfig = {
15
- user: user || config.pg?.user,
16
- password: password || config.pg?.password,
17
- host: host || config.pg?.host,
18
- port: port || config.pg?.port,
19
- database: db || database || config.pg?.db || config.pg?.database,
20
- };
21
-
22
- pgClients[name] = new pg.Pool(dbConfig);
23
- pgClients[name].init = async () => {
24
- await init(pgClients[name]);
25
- };
26
- init(pgClients[name]);
27
- return pgClients[name];
28
- }
29
-
30
- 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(param) {
7
+ const {
8
+ user, password, host, port, db, database, name: origin, funcs,
9
+ } = param || {};
10
+ if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
11
+ const name = origin || db || database || param || 'client';
12
+ if (pgClients[name]) return pgClients[name];
13
+
14
+ const dbConfig = {
15
+ user: user || config.pg?.user,
16
+ password: password || config.pg?.password,
17
+ host: host || config.pg?.host,
18
+ port: port || config.pg?.port,
19
+ database: db || database || config.pg?.db || config.pg?.database,
20
+ };
21
+
22
+ pgClients[name] = new pg.Pool(dbConfig);
23
+ pgClients[name].init = async () => {
24
+ await init(pgClients[name]);
25
+ };
26
+ init(pgClients[name]);
27
+ return pgClients[name];
28
+ }
29
+
30
+ 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;