@opengis/fastify-table 1.0.18 → 1.0.20

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 +56 -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
package/Changelog.md CHANGED
@@ -1,52 +1,56 @@
1
- # fastify-table
2
-
3
- ## 1.1.18 - 02.05.2024
4
-
5
- - widget plugin
6
- - notification plugin
7
-
8
- ## 1.0.9 - 29.04.2024
9
-
10
- - crud token support
11
- - security - xss restriction
12
-
13
- ## 1.0.8 - 29.04.2024
14
-
15
- - filter fix
16
-
17
- ## 1.0.7 - 26.04.2024
18
-
19
- - code optimization
20
-
21
- ## 1.0.6 - 25.04.2024
22
-
23
- - code optimization
24
-
25
- ## 1.0.5 - 24.04.2024
26
-
27
- - code optimization
28
-
29
- ## 1.0.4 - 20.04.2024
30
-
31
- - data api - order
32
- - suggest api - db support
33
- - del api fix
34
-
35
- ## 1.0.3 - 17.04.2024
36
-
37
- - fix unit test
38
-
39
- ## 1.0.2 - 14.04.2024
40
-
41
- - fix redis
42
-
43
- ## 1.0.1 - 14.04.2024
44
-
45
- - fix redis
46
-
47
- ## 1.0.0 - 14.04.2024
48
-
49
- - crud
50
- - pg
51
- - redis
52
- - table
1
+ # fastify-table
2
+
3
+ ## 1.0.20 - 03.05.2024
4
+
5
+ - fix filter separator
6
+
7
+ ## 1.0.19 - 02.05.2024
8
+
9
+ - widget plugin
10
+ - notification plugin
11
+
12
+ ## 1.0.9 - 29.04.2024
13
+
14
+ - crud token support
15
+ - security - xss restriction
16
+
17
+ ## 1.0.8 - 29.04.2024
18
+
19
+ - filter fix
20
+
21
+ ## 1.0.7 - 26.04.2024
22
+
23
+ - code optimization
24
+
25
+ ## 1.0.6 - 25.04.2024
26
+
27
+ - code optimization
28
+
29
+ ## 1.0.5 - 24.04.2024
30
+
31
+ - code optimization
32
+
33
+ ## 1.0.4 - 20.04.2024
34
+
35
+ - data api - order
36
+ - suggest api - db support
37
+ - del api fix
38
+
39
+ ## 1.0.3 - 17.04.2024
40
+
41
+ - fix unit test
42
+
43
+ ## 1.0.2 - 14.04.2024
44
+
45
+ - fix redis
46
+
47
+ ## 1.0.1 - 14.04.2024
48
+
49
+ - fix redis
50
+
51
+ ## 1.0.0 - 14.04.2024
52
+
53
+ - crud
54
+ - pg
55
+ - redis
56
+ - table
@@ -1,5 +1,5 @@
1
1
  export default async function userNotifications({
2
- pg, session = {}, query = { },
2
+ pg, session = {}, query = {},
3
3
  }) {
4
4
  const time = Date.now();
5
5
  try {
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "@opengis/fastify-table",
3
- "version": "1.0.18",
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.20",
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
  }
@@ -1,89 +1,89 @@
1
- // const rclient = require('../../redis/client')
2
- import pgClient from '../pgClients.js';
3
-
4
- // subfunc
5
- const getTable = ({ table }) => table.toLowerCase().replace(/[\n\r]+/g, ' ').split(' from ')
6
- .filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
7
- .map((el) => el.split(/[ )]/)[0]);
8
-
9
- const loadedIndex = {};
10
-
11
- // main func
12
- async function autoIndex({ table, columns: filter, pg = pgClient.client }) {
13
- if (!filter?.length || !table) return null;
14
-
15
- const attrs = filter.map((el) => (el.name || el).replace(/'/g, '')).sort();
16
- const types = filter.map((el) => (el.name ? el : { name: el }))
17
- .reduce((p, el) => ({ ...p, [el.name]: el.type || '-' }), {});
18
-
19
- const redisKey = `autoindex1:${table}:${attrs.join(';')}`;
20
- const tableList = getTable({ table });
21
- const existsCache = loadedIndex[redisKey];
22
- if (existsCache) { return 'ok'; }
23
-
24
- const tbs = tableList[0];
25
- const [ns, tbl] = tableList[0].split('.');
26
- const { rows: index } = await pg.query(`select * from pg_indexes where tablename = '${tbl}' and schemaname = '${ns}'`);
27
-
28
- if (existsCache && index?.length > 0) { return null; }
29
- // console.log('autoindex', table, filter?.map((el) => el.name || el));
30
- const { rows: cols } = await pg.query(`SELECT attrelid::regclass AS tbl, attname AS aname, atttypid::regtype AS datatype, atttypid
31
- FROM pg_attribute WHERE attrelid = '${tbs}'::regclass and attname = any('{ ${attrs} }')`);
32
-
33
- const qIndex = [];
34
- const indexAr = {};
35
-
36
- // console.log(cols);
37
- for (let i = 0; i < cols.length; i += 1) {
38
- const el = cols[i];
39
- el.relname = tbl;
40
- el.nspname = ns;
41
-
42
- const indexUsing = (el.datatype === 'geometry' ? 'gist' : null)
43
- || (types[el.aname] === 'Text' || el.datatype?.includes('[]') ? 'gin' : null)
44
- || 'btree';
45
-
46
- const name = `${el.nspname}_${el.relname}_${el.aname}_${indexUsing}_idx`;
47
- const exists = index.filter((ind) => ind.tablename === el.relname && ind.schemaname === el.nspname && ind.indexdef.includes(types[el.aname] === 'Text'
48
- ? `gin (${el.aname} gin_trgm_ops)` : `btree (${el.aname})`));
49
-
50
- indexAr[el.aname] = {
51
- name, type: el.datatype, exists, // exists1,
52
- };
53
-
54
- // drop
55
- if (exists?.length > 1) {
56
- exists.filter((ind, i1) => i1).forEach((ind) => qIndex.push(`DROP INDEX if exists ${ind.schemaname}.${ind.indexname} `));
57
- }
58
-
59
- // add
60
- if (exists?.length === 0) {
61
- // console.log(`${name} - ${el.datatype}`);
62
-
63
- const suffix = types[el.aname] === 'Text' ? 'gin_trgm_ops' : '';
64
- if (['text'].includes(el?.datatype)) {
65
- qIndex.push(`CREATE INDEX if not exists ${name} ON ${el.nspname}.${el.relname} USING ${indexUsing} (${el.aname} ${suffix})`);
66
- }
67
- if (indexUsing === 'gist') {
68
- qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', 'gist1')}
69
- ON ${el.nspname}.${el.relname} USING gist (${el.aname})`);
70
-
71
- qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', 'area')}
72
- ON ${el.nspname}.${el.relname} USING btree (st_area(st_transform(${el.aname},3857)))`);
73
- qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', '4326')}
74
- ON ${el.nspname}.${el.relname} USING gist (st_transform(${el.aname},4326))`);
75
- }
76
- }
77
- }
78
-
79
- loadedIndex[redisKey] = 1;
80
- // throw qIndex;
81
- if (!qIndex.length) return null;
82
-
83
- // console.log(qIndex.filter((v, i, a) => a.indexOf(v) === i));
84
- // logger.file('index', { table, filter, sql: qIndex.filter((v, i, a) => a.indexOf(v) === i) });
85
- qIndex.filter((v, i, a) => a.indexOf(v) === i).map((el) => pg.one(el).catch((err) => console.log(err.toString())));
86
- return 'ok';
87
- }
88
-
89
- export default autoIndex;
1
+ // const rclient = require('../../redis/client')
2
+ import pgClient from '../pgClients.js';
3
+
4
+ // subfunc
5
+ const getTable = ({ table }) => table.toLowerCase().replace(/[\n\r]+/g, ' ').split(' from ')
6
+ .filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
7
+ .map((el) => el.split(/[ )]/)[0]);
8
+
9
+ const loadedIndex = {};
10
+
11
+ // main func
12
+ async function autoIndex({ table, columns: filter, pg = pgClient.client }) {
13
+ if (!filter?.length || !table) return null;
14
+
15
+ const attrs = filter.map((el) => (el.name || el).replace(/'/g, '')).sort();
16
+ const types = filter.map((el) => (el.name ? el : { name: el }))
17
+ .reduce((p, el) => ({ ...p, [el.name]: el.type || '-' }), {});
18
+
19
+ const redisKey = `autoindex1:${table}:${attrs.join(';')}`;
20
+ const tableList = getTable({ table });
21
+ const existsCache = loadedIndex[redisKey];
22
+ if (existsCache) { return 'ok'; }
23
+
24
+ const tbs = tableList[0];
25
+ const [ns, tbl] = tableList[0].split('.');
26
+ const { rows: index } = await pg.query(`select * from pg_indexes where tablename = '${tbl}' and schemaname = '${ns}'`);
27
+
28
+ if (existsCache && index?.length > 0) { return null; }
29
+ // console.log('autoindex', table, filter?.map((el) => el.name || el));
30
+ const { rows: cols } = await pg.query(`SELECT attrelid::regclass AS tbl, attname AS aname, atttypid::regtype AS datatype, atttypid
31
+ FROM pg_attribute WHERE attrelid = '${tbs}'::regclass and attname = any('{ ${attrs} }')`);
32
+
33
+ const qIndex = [];
34
+ const indexAr = {};
35
+
36
+ // console.log(cols);
37
+ for (let i = 0; i < cols.length; i += 1) {
38
+ const el = cols[i];
39
+ el.relname = tbl;
40
+ el.nspname = ns;
41
+
42
+ const indexUsing = (el.datatype === 'geometry' ? 'gist' : null)
43
+ || (types[el.aname] === 'Text' || el.datatype?.includes('[]') ? 'gin' : null)
44
+ || 'btree';
45
+
46
+ const name = `${el.nspname}_${el.relname}_${el.aname}_${indexUsing}_idx`;
47
+ const exists = index.filter((ind) => ind.tablename === el.relname && ind.schemaname === el.nspname && ind.indexdef.includes(types[el.aname] === 'Text'
48
+ ? `gin (${el.aname} gin_trgm_ops)` : `btree (${el.aname})`));
49
+
50
+ indexAr[el.aname] = {
51
+ name, type: el.datatype, exists, // exists1,
52
+ };
53
+
54
+ // drop
55
+ if (exists?.length > 1) {
56
+ exists.filter((ind, i1) => i1).forEach((ind) => qIndex.push(`DROP INDEX if exists ${ind.schemaname}.${ind.indexname} `));
57
+ }
58
+
59
+ // add
60
+ if (exists?.length === 0) {
61
+ // console.log(`${name} - ${el.datatype}`);
62
+
63
+ const suffix = types[el.aname] === 'Text' ? 'gin_trgm_ops' : '';
64
+ if (['text'].includes(el?.datatype)) {
65
+ qIndex.push(`CREATE INDEX if not exists ${name} ON ${el.nspname}.${el.relname} USING ${indexUsing} (${el.aname} ${suffix})`);
66
+ }
67
+ if (indexUsing === 'gist') {
68
+ qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', 'gist1')}
69
+ ON ${el.nspname}.${el.relname} USING gist (${el.aname})`);
70
+
71
+ qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', 'area')}
72
+ ON ${el.nspname}.${el.relname} USING btree (st_area(st_transform(${el.aname},3857)))`);
73
+ qIndex.push(`CREATE INDEX if not exists ${name.replace('gist', '4326')}
74
+ ON ${el.nspname}.${el.relname} USING gist (st_transform(${el.aname},4326))`);
75
+ }
76
+ }
77
+ }
78
+
79
+ loadedIndex[redisKey] = 1;
80
+ // throw qIndex;
81
+ if (!qIndex.length) return null;
82
+
83
+ // console.log(qIndex.filter((v, i, a) => a.indexOf(v) === i));
84
+ // logger.file('index', { table, filter, sql: qIndex.filter((v, i, a) => a.indexOf(v) === i) });
85
+ qIndex.filter((v, i, a) => a.indexOf(v) === i).map((el) => pg.one(el).catch((err) => console.log(err.toString())));
86
+ return 'ok';
87
+ }
88
+
89
+ export default autoIndex;
@@ -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 = 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 = 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,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
+ }) {
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/funcs/init.js CHANGED
@@ -1,42 +1,42 @@
1
- import crypto from 'crypto';
2
-
3
- // import pg from 'pg';
4
- import getRedis from '../../redis/funcs/getRedis.js';
5
- // import config from '../config.js';
6
-
7
- async function init(client) {
8
- const textQuery = `select
9
- (select json_object_agg(conrelid::regclass ,(SELECT attname FROM pg_attribute WHERE attrelid = c.conrelid and attnum = c.conkey[1]))
10
- from pg_constraint c where contype='p' and connamespace::regnamespace::text not in ('sde')) as pk,
11
- (SELECT json_object_agg(t.oid::text,pg_catalog.format_type(t.oid, NULL)) FROM pg_catalog.pg_type t) as "pgType"`;
12
- const { pgType, pk } = await client.query(textQuery).then((d) => d.rows[0]);
13
-
14
- const tlist = await client.query(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist
15
- from pg_class where relkind in ('r','v')`).then((d) => d.rows[0].tlist);
16
-
17
- async function one(query, param = {}) {
18
- const data = await client.query(query, Array.isArray(param) ? param : param.args || []);
19
- const result = ((Array.isArray(data) ? data.pop() : data)?.rows || [])[0] || {};
20
- return result;
21
- }
22
-
23
- async function queryCache(query) {
24
- const rclient = getRedis({ db: 0 });
25
- const hash = crypto.createHash('sha1').update(query).digest('base64');
26
- const keyCache = `pg:${hash}`;
27
- const cache = await rclient.get(keyCache);
28
- if (cache) {
29
- return JSON.parse(cache);
30
- }
31
- const data = await client.query(query);
32
- rclient.set(keyCache, JSON.stringify(data), 'EX', 60 * 60);
33
- return data;
34
- }
35
-
36
- Object.assign(client, {
37
- one, pgType, pk, tlist, queryCache,
38
- });
39
- }
40
-
41
- // export default client;
42
- export default init;
1
+ import crypto from 'crypto';
2
+
3
+ // import pg from 'pg';
4
+ import getRedis from '../../redis/funcs/getRedis.js';
5
+ // import config from '../config.js';
6
+
7
+ async function init(client) {
8
+ const textQuery = `select
9
+ (select json_object_agg(conrelid::regclass ,(SELECT attname FROM pg_attribute WHERE attrelid = c.conrelid and attnum = c.conkey[1]))
10
+ from pg_constraint c where contype='p' and connamespace::regnamespace::text not in ('sde')) as pk,
11
+ (SELECT json_object_agg(t.oid::text,pg_catalog.format_type(t.oid, NULL)) FROM pg_catalog.pg_type t) as "pgType"`;
12
+ const { pgType, pk } = await client.query(textQuery).then((d) => d.rows[0]);
13
+
14
+ const tlist = await client.query(`select array_agg((select nspname from pg_namespace where oid=relnamespace)||'.'||relname) tlist
15
+ from pg_class where relkind in ('r','v')`).then((d) => d.rows[0].tlist);
16
+
17
+ async function one(query, param = {}) {
18
+ const data = await client.query(query, Array.isArray(param) ? param : param.args || []);
19
+ const result = ((Array.isArray(data) ? data.pop() : data)?.rows || [])[0] || {};
20
+ return result;
21
+ }
22
+
23
+ async function queryCache(query) {
24
+ const rclient = getRedis({ db: 0 });
25
+ const hash = crypto.createHash('sha1').update(query).digest('base64');
26
+ const keyCache = `pg:${hash}`;
27
+ const cache = await rclient.get(keyCache);
28
+ if (cache) {
29
+ return JSON.parse(cache);
30
+ }
31
+ const data = await client.query(query);
32
+ rclient.set(keyCache, JSON.stringify(data), 'EX', 60 * 60);
33
+ return data;
34
+ }
35
+
36
+ Object.assign(client, {
37
+ one, pgType, pk, tlist, queryCache,
38
+ });
39
+ }
40
+
41
+ // export default client;
42
+ export default init;
@@ -1,2 +1,2 @@
1
- const pgClients = {};
2
- export default pgClients;
1
+ const pgClients = {};
2
+ export default pgClients;
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;