@opengis/fastify-table 1.0.25 → 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.
- package/.eslintrc.cjs +42 -42
- package/Changelog.md +4 -0
- package/README.md +26 -26
- package/config.js +11 -11
- package/crud/controllers/deleteCrud.js +14 -14
- package/crud/controllers/utils/checkXSS.js +45 -45
- package/crud/controllers/utils/xssInjection.js +72 -72
- package/crud/funcs/dataDelete.js +15 -15
- package/crud/funcs/dataInsert.js +1 -1
- package/crud/funcs/dataUpdate.js +7 -3
- package/crud/funcs/getToken.js +27 -27
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/funcs/setToken.js +53 -53
- package/dblist/controllers/createItem.js +17 -17
- package/dblist/controllers/readItems.js +8 -8
- package/dblist/index.js +40 -40
- package/package.json +1 -1
- package/pg/funcs/autoIndex.js +89 -89
- package/pg/funcs/getMeta.js +27 -27
- package/pg/funcs/getPG.js +1 -1
- package/pg/funcs/init.js +42 -42
- package/pg/funcs/pgClients.js +2 -2
- package/redis/funcs/getRedis.js +1 -1
- package/server.js +14 -14
- package/table/controllers/search.js +41 -41
- package/table/funcs/getFilterSQL/util/getTableSql.js +1 -1
- package/test/config.example +18 -18
- package/test/funcs/crud.test.js +76 -76
- package/test/funcs/pg.test.js +34 -34
- package/test/funcs/redis.test.js +19 -19
- package/test/templates/cls/test.json +9 -9
- package/test/templates/form/cp_building.form.json +32 -32
- package/test/templates/select/account_id.json +3 -3
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -20
package/crud/funcs/setToken.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import config from '../../config.js';
|
|
4
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
5
|
-
|
|
6
|
-
const generateCodes = (ids, userToken) => {
|
|
7
|
-
const token = userToken || randomUUID();
|
|
8
|
-
const notNullIds = ids.filter((el) => el);
|
|
9
|
-
const obj = {};
|
|
10
|
-
const codes = notNullIds.reduce((acc, id) => {
|
|
11
|
-
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
12
|
-
acc[newToken] = id; obj[id] = newToken;
|
|
13
|
-
return acc;
|
|
14
|
-
}, {});
|
|
15
|
-
return { codes, obj };
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function setToken({
|
|
19
|
-
ids: idsOrigin, mode = 'r', uid, referer, array,
|
|
20
|
-
}) {
|
|
21
|
-
const rclient2 = getRedis({ db: 0 });
|
|
22
|
-
// const rclient5 = getRedis({ db: 0, funcs });
|
|
23
|
-
|
|
24
|
-
if (!uid) return { user: 'empty' };
|
|
25
|
-
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
26
|
-
|
|
27
|
-
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
28
|
-
// update/delete
|
|
29
|
-
|
|
30
|
-
if (mode === 'r') return null;
|
|
31
|
-
|
|
32
|
-
// TODO generate salt
|
|
33
|
-
const { codes, obj } = generateCodes(ids, uid);
|
|
34
|
-
|
|
35
|
-
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
36
|
-
|
|
37
|
-
rclient2.hmset(`${config.pg.database}:token:${{
|
|
38
|
-
e: 'exec', r: 'view', w: 'edit', a: 'add',
|
|
39
|
-
}[mode]}:${uid}`, codes);
|
|
40
|
-
|
|
41
|
-
// log token for debug. add extra data - uid, mode, date
|
|
42
|
-
/* const dt = new Date().toISOString();
|
|
43
|
-
const codesLog = Object.keys(codes).reduce((acc, key) => {
|
|
44
|
-
acc[key] = `{"referer": "${referer}" ,"uid":"${uid}","mode":"${mode}","date":"${dt}",${codes[key].substr(1)}`;
|
|
45
|
-
return acc;
|
|
46
|
-
}, {});
|
|
47
|
-
rclient5.hmset(`${config.pg.database}:token:edit`, codesLog); // 'EX', 64800 */
|
|
48
|
-
|
|
49
|
-
// TODO дополнительно писать в hset token -> uid
|
|
50
|
-
return array ? Object.values(obj) : obj;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export default setToken;
|
|
1
|
+
import { createHash, randomUUID } from 'crypto';
|
|
2
|
+
|
|
3
|
+
import config from '../../config.js';
|
|
4
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
5
|
+
|
|
6
|
+
const generateCodes = (ids, userToken) => {
|
|
7
|
+
const token = userToken || randomUUID();
|
|
8
|
+
const notNullIds = ids.filter((el) => el);
|
|
9
|
+
const obj = {};
|
|
10
|
+
const codes = notNullIds.reduce((acc, id) => {
|
|
11
|
+
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
12
|
+
acc[newToken] = id; obj[id] = newToken;
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
return { codes, obj };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function setToken({
|
|
19
|
+
ids: idsOrigin, mode = 'r', uid, referer, array,
|
|
20
|
+
}) {
|
|
21
|
+
const rclient2 = getRedis({ db: 0 });
|
|
22
|
+
// const rclient5 = getRedis({ db: 0, funcs });
|
|
23
|
+
|
|
24
|
+
if (!uid) return { user: 'empty' };
|
|
25
|
+
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
26
|
+
|
|
27
|
+
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
28
|
+
// update/delete
|
|
29
|
+
|
|
30
|
+
if (mode === 'r') return null;
|
|
31
|
+
|
|
32
|
+
// TODO generate salt
|
|
33
|
+
const { codes, obj } = generateCodes(ids, uid);
|
|
34
|
+
|
|
35
|
+
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
36
|
+
|
|
37
|
+
rclient2.hmset(`${config.pg.database}:token:${{
|
|
38
|
+
e: 'exec', r: 'view', w: 'edit', a: 'add',
|
|
39
|
+
}[mode]}:${uid}`, codes);
|
|
40
|
+
|
|
41
|
+
// log token for debug. add extra data - uid, mode, date
|
|
42
|
+
/* const dt = new Date().toISOString();
|
|
43
|
+
const codesLog = Object.keys(codes).reduce((acc, key) => {
|
|
44
|
+
acc[key] = `{"referer": "${referer}" ,"uid":"${uid}","mode":"${mode}","date":"${dt}",${codes[key].substr(1)}`;
|
|
45
|
+
return acc;
|
|
46
|
+
}, {});
|
|
47
|
+
rclient5.hmset(`${config.pg.database}:token:edit`, codesLog); // 'EX', 64800 */
|
|
48
|
+
|
|
49
|
+
// TODO дополнительно писать в hset token -> uid
|
|
50
|
+
return array ? Object.values(obj) : obj;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default setToken;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { randomUUID } from 'crypto';
|
|
2
|
-
import { existsSync } from 'fs';
|
|
3
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
4
|
-
|
|
5
|
-
import checkItem from './utils/checkItem.js';
|
|
6
|
-
|
|
7
|
-
export default async function insertItem({ body = {} }) {
|
|
8
|
-
const check = checkItem(body.data);
|
|
9
|
-
if (check?.error) return check;
|
|
10
|
-
|
|
11
|
-
Object.assign(body.data, { id: randomUUID() });
|
|
12
|
-
const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
13
|
-
data.push(body.data);
|
|
14
|
-
|
|
15
|
-
await writeFile('dblist.json', JSON.stringify(data));
|
|
16
|
-
return { data };
|
|
17
|
-
}
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
4
|
+
|
|
5
|
+
import checkItem from './utils/checkItem.js';
|
|
6
|
+
|
|
7
|
+
export default async function insertItem({ body = {} }) {
|
|
8
|
+
const check = checkItem(body.data);
|
|
9
|
+
if (check?.error) return check;
|
|
10
|
+
|
|
11
|
+
Object.assign(body.data, { id: randomUUID() });
|
|
12
|
+
const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
13
|
+
data.push(body.data);
|
|
14
|
+
|
|
15
|
+
await writeFile('dblist.json', JSON.stringify(data));
|
|
16
|
+
return { data };
|
|
17
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
|
|
4
|
-
export default async function readItemList(req) {
|
|
5
|
-
const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
6
|
-
// const { user = {} } = req.session?.passport || {};
|
|
7
|
-
return { data };
|
|
8
|
-
}
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
|
|
4
|
+
export default async function readItemList(req) {
|
|
5
|
+
const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
6
|
+
// const { user = {} } = req.session?.passport || {};
|
|
7
|
+
return { data };
|
|
8
|
+
}
|
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/package.json
CHANGED
package/pg/funcs/autoIndex.js
CHANGED
|
@@ -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;
|
package/pg/funcs/getMeta.js
CHANGED
|
@@ -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
|
@@ -5,7 +5,7 @@ import init from './init.js';
|
|
|
5
5
|
|
|
6
6
|
function getPG({
|
|
7
7
|
user, password, host, port, db, database, name: origin, funcs,
|
|
8
|
-
}) {
|
|
8
|
+
} = { name: 'client' }) {
|
|
9
9
|
if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
|
|
10
10
|
const name = origin || db || database;
|
|
11
11
|
if (pgClients[name]) return pgClients[name];
|
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;
|
package/pg/funcs/pgClients.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const pgClients = {};
|
|
2
|
-
export default pgClients;
|
|
1
|
+
const pgClients = {};
|
|
2
|
+
export default pgClients;
|
package/redis/funcs/getRedis.js
CHANGED
|
@@ -2,7 +2,7 @@ import Redis from 'ioredis';
|
|
|
2
2
|
import config from '../../config.js';
|
|
3
3
|
import redisClients from './redisClients.js';
|
|
4
4
|
|
|
5
|
-
function getRedis({ db }) {
|
|
5
|
+
function getRedis({ db } = { db: 0 }) {
|
|
6
6
|
if (!config.redis) return null;
|
|
7
7
|
if (redisClients[db]) return redisClients[db];
|
|
8
8
|
|
package/server.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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
|
-
const app = Fastify({ logger: false });
|
|
8
|
-
app.register(appService, config);
|
|
9
|
-
app.listen({ host: '0.0.0.0', port: process.env.PORT || 3000 }, (err) => {
|
|
10
|
-
if (err) {
|
|
11
|
-
app.log.error(err);
|
|
12
|
-
process.exit(1);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
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
|
+
const app = Fastify({ logger: false });
|
|
8
|
+
app.register(appService, config);
|
|
9
|
+
app.listen({ host: '0.0.0.0', port: process.env.PORT || 3000 }, (err) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
app.log.error(err);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
});
|