@opengis/fastify-table 1.0.9 → 1.0.11
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 +47 -47
- package/README.md +26 -26
- package/config.js +11 -11
- package/crud/controllers/deleteCrud.js +10 -10
- package/crud/controllers/insert.js +28 -28
- package/crud/controllers/update.js +29 -29
- 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 +24 -24
- package/crud/funcs/dataUpdate.js +3 -3
- package/crud/funcs/{getIdByToken.js → getToken.js} +27 -29
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/funcs/{setTokenById.js → setToken.js} +53 -55
- package/helper.js +28 -28
- package/index.js +32 -32
- package/package.json +22 -22
- package/pg/funcs/autoIndex.js +89 -89
- package/pg/funcs/getMeta.js +27 -27
- package/pg/funcs/getPG.js +3 -0
- package/pg/funcs/init.js +42 -42
- package/pg/funcs/pgClients.js +2 -2
- package/pg/index.js +35 -35
- package/pg/pgClients.js +20 -17
- package/policy/funcs/checkPolicy.js +74 -74
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/getRedis.js +1 -2
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server.js +14 -14
- package/table/controllers/data.js +57 -55
- package/table/controllers/filter.js +32 -24
- package/table/controllers/form.js +10 -10
- package/table/controllers/suggest.js +60 -60
- package/table/controllers/utils/getSelect.js +20 -20
- package/table/controllers/utils/getSelectMeta.js +66 -66
- package/table/funcs/getFilterSQL/index.js +75 -75
- package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
- package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
- package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/table/funcs/metaFormat/getSelectVal.js +20 -0
- package/table/funcs/metaFormat/index.js +24 -0
- package/table/index.js +25 -14
- package/test/api/crud.test.js +50 -50
- package/test/api/crud.xss.test.js +68 -70
- package/test/api/table.test.js +49 -49
- package/test/config.example +18 -18
- package/test/funcs/crud.test.js +76 -77
- package/test/funcs/pg.test.js +34 -32
- package/test/funcs/redis.test.js +19 -19
- package/test/templates/cls/itree.recommend.json +26 -0
- package/test/templates/cls/itree.type_plant.json +65 -0
- 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/contact_id.sql +1 -0
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -20
- package/test/templates/table/green_space.table.json +3 -3
- package/test/funcs/table.test.js +0 -48
package/crud/funcs/dataInsert.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
-
|
|
4
|
-
export default async function dataInsert({ table, data }) {
|
|
5
|
-
const pg = getPG({ name: 'client' });
|
|
6
|
-
if (!data) return null;
|
|
7
|
-
const { columns } = await getMeta(table);
|
|
8
|
-
if (!columns) return null;
|
|
9
|
-
|
|
10
|
-
const names = columns.map((el) => el.name);
|
|
11
|
-
const filterData = Object.keys(data)
|
|
12
|
-
.filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
|
|
13
|
-
|
|
14
|
-
const insertQuery = `insert into ${table}
|
|
15
|
-
|
|
16
|
-
( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
|
|
17
|
-
|
|
18
|
-
values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
|
|
19
|
-
|
|
20
|
-
returning *`;
|
|
21
|
-
await pg.query('DROP TRIGGER if exists dataset_before_update_insert ON gis.dataset');
|
|
22
|
-
const res = await pg.one(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]) || {};
|
|
23
|
-
return res;
|
|
24
|
-
}
|
|
1
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
+
|
|
4
|
+
export default async function dataInsert({ table, data }) {
|
|
5
|
+
const pg = getPG({ name: 'client' });
|
|
6
|
+
if (!data) return null;
|
|
7
|
+
const { columns } = await getMeta(table);
|
|
8
|
+
if (!columns) return null;
|
|
9
|
+
|
|
10
|
+
const names = columns.map((el) => el.name);
|
|
11
|
+
const filterData = Object.keys(data)
|
|
12
|
+
.filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
|
|
13
|
+
|
|
14
|
+
const insertQuery = `insert into ${table}
|
|
15
|
+
|
|
16
|
+
( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
|
|
17
|
+
|
|
18
|
+
values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
|
|
19
|
+
|
|
20
|
+
returning *`;
|
|
21
|
+
await pg.query('DROP TRIGGER if exists dataset_before_update_insert ON gis.dataset');
|
|
22
|
+
const res = await pg.one(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]) || {};
|
|
23
|
+
return res;
|
|
24
|
+
}
|
package/crud/funcs/dataUpdate.js
CHANGED
|
@@ -8,13 +8,13 @@ export default async function dataUpdate({
|
|
|
8
8
|
const pg = getPG({ name: 'client' });
|
|
9
9
|
const { columns, pk } = await getMeta(table);
|
|
10
10
|
|
|
11
|
-
const names = columns
|
|
11
|
+
const names = columns?.map((el) => el.name);
|
|
12
12
|
const filterData = Object.keys(data)
|
|
13
|
-
.filter((el) => data[el] && names
|
|
13
|
+
.filter((el) => data[el] && names?.includes(el)).map((el) => [el, data[el]]);
|
|
14
14
|
|
|
15
15
|
const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key[0]}"=$${i + 2}`).join(',')}
|
|
16
16
|
WHERE ${pk} = $1 returning *`;
|
|
17
17
|
// console.log(updateDataset);
|
|
18
|
-
const res = await pg.
|
|
18
|
+
const res = await pg.query(updateQuery, [id, ...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]).then(el => el?.rows?.[0]) || {};
|
|
19
19
|
return res;
|
|
20
20
|
}
|
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export default getIdByToken;
|
|
1
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
+
import config from '../../config.js';
|
|
3
|
+
|
|
4
|
+
function sprintf(str, ...args) {
|
|
5
|
+
return str.replace(/%s/g, () => args.shift());
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const keys = {
|
|
9
|
+
r: '%s:token:view:%s',
|
|
10
|
+
a: '%s:token:add:%s',
|
|
11
|
+
w: '%s:token:edit:%s',
|
|
12
|
+
e: '%s:token:exec:%s',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
async function getIdByToken({
|
|
16
|
+
uid, token, mode = 'r', json,
|
|
17
|
+
}) {
|
|
18
|
+
if (mode === 'r') return token;
|
|
19
|
+
|
|
20
|
+
const rclient = getRedis({ db: 0 });
|
|
21
|
+
|
|
22
|
+
const key = sprintf(keys[mode], config?.pg?.database, uid?.toString());
|
|
23
|
+
const id = await rclient.hget(key, token);
|
|
24
|
+
return json && id[0] === '{' ? JSON.parse(id) : id;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default getIdByToken;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { access } from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
const isFileExists = async (filepath) => {
|
|
4
|
-
try {
|
|
5
|
-
await access(filepath);
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
catch (err) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default isFileExists;
|
|
1
|
+
import { access } from 'fs/promises';
|
|
2
|
+
|
|
3
|
+
const isFileExists = async (filepath) => {
|
|
4
|
+
try {
|
|
5
|
+
await access(filepath);
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default isFileExists;
|
|
@@ -1,55 +1,53 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export default setTokenById;
|
|
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;
|
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,32 +1,32 @@
|
|
|
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 crudPlugin from './crud/index.js';
|
|
10
|
-
import policyPlugin from './policy/index.js';
|
|
11
|
-
|
|
12
|
-
async function plugin(fastify, opt) {
|
|
13
|
-
// console.log(opt);
|
|
14
|
-
config.pg = opt.pg;
|
|
15
|
-
config.redis = opt.redis;
|
|
16
|
-
|
|
17
|
-
// independent npm start / unit test
|
|
18
|
-
if (!fastify.config) {
|
|
19
|
-
fastify.decorate('config', config);
|
|
20
|
-
}
|
|
21
|
-
if (!fastify.funcs) {
|
|
22
|
-
fastify.decorateRequest('funcs', fastify);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
policyPlugin(fastify);
|
|
26
|
-
redisPlugin(fastify);
|
|
27
|
-
await pgPlugin(fastify, opt);
|
|
28
|
-
tablePlugin(fastify, opt);
|
|
29
|
-
crudPlugin(fastify, opt);
|
|
30
|
-
}
|
|
31
|
-
export default fp(plugin);
|
|
32
|
-
// 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 crudPlugin from './crud/index.js';
|
|
10
|
+
import policyPlugin from './policy/index.js';
|
|
11
|
+
|
|
12
|
+
async function plugin(fastify, opt) {
|
|
13
|
+
// console.log(opt);
|
|
14
|
+
config.pg = opt.pg;
|
|
15
|
+
config.redis = opt.redis;
|
|
16
|
+
|
|
17
|
+
// independent npm start / unit test
|
|
18
|
+
if (!fastify.config) {
|
|
19
|
+
fastify.decorate('config', config);
|
|
20
|
+
}
|
|
21
|
+
if (!fastify.funcs) {
|
|
22
|
+
fastify.decorateRequest('funcs', fastify);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
policyPlugin(fastify);
|
|
26
|
+
redisPlugin(fastify);
|
|
27
|
+
await pgPlugin(fastify, opt);
|
|
28
|
+
tablePlugin(fastify, opt);
|
|
29
|
+
crudPlugin(fastify, opt);
|
|
30
|
+
}
|
|
31
|
+
export default fp(plugin);
|
|
32
|
+
// export { rclient };
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@opengis/fastify-table",
|
|
3
|
-
"version": "1.0.
|
|
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.11",
|
|
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/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;
|