@opengis/fastify-table 1.0.3 → 1.0.4
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 +20 -20
- package/README.md +26 -26
- package/config.js +3 -3
- package/crud/controllers/deleteCrud.js +10 -10
- package/crud/controllers/insert.js +9 -9
- package/crud/controllers/update.js +10 -10
- package/crud/funcs/dataDelete.js +15 -0
- package/crud/funcs/dataInsert.js +24 -24
- package/crud/funcs/dataUpdate.js +20 -20
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/index.js +29 -29
- package/helper.js +28 -28
- package/index.js +22 -22
- package/package.json +22 -22
- package/pg/funcs/autoIndex.js +89 -89
- package/pg/funcs/getMeta.js +27 -27
- package/pg/funcs/init.js +42 -42
- package/pg/funcs/pgClients.js +2 -2
- package/pg/index.js +35 -35
- package/pg/pgClients.js +17 -17
- package/redis/client.js +8 -8
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -18
- package/server.js +14 -0
- package/table/controllers/data.js +51 -48
- package/table/controllers/filter.js +17 -17
- package/table/controllers/form.js +10 -10
- package/table/controllers/suggest.js +59 -56
- package/table/controllers/utils/getSelect.js +20 -20
- package/table/controllers/utils/getSelectMeta.js +66 -65
- package/table/controllers/utils/getTemplate.js +24 -24
- 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/index.js +14 -14
- package/test/api/crud.test.js +23 -23
- package/test/api/table.test.js +29 -29
- package/test/config.example +18 -18
- package/test/funcs/crud.test.js +37 -37
- package/test/funcs/pg.test.js +26 -26
- package/test/funcs/redis.test.js +19 -19
- package/test/funcs/table.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 +4 -0
- package/test/templates/select/account_id.sql +1 -0
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -42
- package/test/templates/table/service.json +18 -0
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/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/pg/index.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
// import fp from 'fastify-plugin';
|
|
2
|
-
//import pg from 'pg';
|
|
3
|
-
import pgClients from './funcs/pgClients.js';
|
|
4
|
-
import init from './funcs/init.js';
|
|
5
|
-
import autoIndex from './funcs/autoIndex.js';
|
|
6
|
-
import getMeta from './funcs/getMeta.js';
|
|
7
|
-
import getPG from './funcs/getPG.js';
|
|
8
|
-
|
|
9
|
-
function close() {
|
|
10
|
-
// console.log('pg close');
|
|
11
|
-
Object.keys(pgClients).forEach((el) => {
|
|
12
|
-
// console.log(el);
|
|
13
|
-
pgClients[el].end();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
// return pgClients.client.end();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function plugin(fastify, config) {
|
|
20
|
-
const client = getPG({ ...config.pg || {}, name: 'client' })
|
|
21
|
-
await init(client);
|
|
22
|
-
fastify.addHook('onRequest', async (req) => {
|
|
23
|
-
// req.funcs = fastify;
|
|
24
|
-
req.pg = client;
|
|
25
|
-
});
|
|
26
|
-
//pgClients.client = client;
|
|
27
|
-
|
|
28
|
-
// fastify.decorate('pg', client);
|
|
29
|
-
fastify.decorate('autoIndex', autoIndex);
|
|
30
|
-
fastify.decorate('getMeta', getMeta);
|
|
31
|
-
fastify.decorate('getPG', getPG);
|
|
32
|
-
fastify.addHook('onClose', close);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export default plugin;
|
|
1
|
+
// import fp from 'fastify-plugin';
|
|
2
|
+
//import pg from 'pg';
|
|
3
|
+
import pgClients from './funcs/pgClients.js';
|
|
4
|
+
import init from './funcs/init.js';
|
|
5
|
+
import autoIndex from './funcs/autoIndex.js';
|
|
6
|
+
import getMeta from './funcs/getMeta.js';
|
|
7
|
+
import getPG from './funcs/getPG.js';
|
|
8
|
+
|
|
9
|
+
function close() {
|
|
10
|
+
// console.log('pg close');
|
|
11
|
+
Object.keys(pgClients).forEach((el) => {
|
|
12
|
+
// console.log(el);
|
|
13
|
+
pgClients[el].end();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// return pgClients.client.end();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function plugin(fastify, config) {
|
|
20
|
+
const client = getPG({ ...config.pg || {}, name: 'client' })
|
|
21
|
+
await init(client);
|
|
22
|
+
fastify.addHook('onRequest', async (req) => {
|
|
23
|
+
// req.funcs = fastify;
|
|
24
|
+
req.pg = client;
|
|
25
|
+
});
|
|
26
|
+
//pgClients.client = client;
|
|
27
|
+
|
|
28
|
+
// fastify.decorate('pg', client);
|
|
29
|
+
fastify.decorate('autoIndex', autoIndex);
|
|
30
|
+
fastify.decorate('getMeta', getMeta);
|
|
31
|
+
fastify.decorate('getPG', getPG);
|
|
32
|
+
fastify.addHook('onClose', close);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default plugin;
|
package/pg/pgClients.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
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
|
-
await init(client);
|
|
15
|
-
pgClients.client = client;
|
|
16
|
-
}
|
|
17
|
-
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
|
+
await init(client);
|
|
15
|
+
pgClients.client = client;
|
|
16
|
+
}
|
|
17
|
+
export default pgClients;
|
package/redis/client.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import redisClients from './funcs/redisClients.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
|
|
4
|
-
if (!redisClients[0]) {
|
|
5
|
-
getRedis({ db: 0 });
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default redisClients[0];
|
|
1
|
+
import redisClients from './funcs/redisClients.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
|
|
4
|
+
if (!redisClients[0]) {
|
|
5
|
+
getRedis({ db: 0 });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default redisClients[0];
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const redisClients = {};
|
|
2
|
-
export default redisClients;
|
|
1
|
+
const redisClients = {};
|
|
2
|
+
export default redisClients;
|
package/redis/index.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
// import client from './client.js';
|
|
2
|
-
import getRedis from './funcs/getRedis.js';
|
|
3
|
-
// import client from './funcs/redisClients.js';
|
|
4
|
-
|
|
5
|
-
function close(fastify) {
|
|
6
|
-
fastify.rclient.quit();
|
|
7
|
-
// fastify.rclient2.quit();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async function plugin(fastify) {
|
|
11
|
-
const client = getRedis({ db: 0 });
|
|
12
|
-
client.getJSON = client.get;
|
|
13
|
-
fastify.decorate('rclient', client);
|
|
14
|
-
|
|
15
|
-
fastify.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
// import client from './client.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
// import client from './funcs/redisClients.js';
|
|
4
|
+
|
|
5
|
+
function close(fastify) {
|
|
6
|
+
fastify.rclient.quit();
|
|
7
|
+
// fastify.rclient2.quit();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function plugin(fastify) {
|
|
11
|
+
const client = getRedis({ db: 0 });
|
|
12
|
+
client.getJSON = client.get;
|
|
13
|
+
fastify.decorate('rclient', client);
|
|
14
|
+
fastify.decorate('getRedis', getRedis);
|
|
15
|
+
// fastify.decorate('rclient2', client2);
|
|
16
|
+
fastify.addHook('onClose', close);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default plugin;
|
package/server.js
ADDED
|
@@ -0,0 +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,48 +1,51 @@
|
|
|
1
|
-
import getTemplate from './utils/getTemplate.js';
|
|
2
|
-
import getFilterSQL from '../funcs/getFilterSQL/index.js';
|
|
3
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
-
|
|
5
|
-
const maxLimit = 100;
|
|
6
|
-
export default async function data(req) {
|
|
7
|
-
const time = Date.now();
|
|
8
|
-
const {
|
|
9
|
-
pg, params, query = {},
|
|
10
|
-
} = req;
|
|
11
|
-
|
|
12
|
-
const loadTable = await getTemplate('table', params.table);
|
|
13
|
-
|
|
14
|
-
if (!loadTable) { return { status: 404, message: 'not found' }; }
|
|
15
|
-
|
|
16
|
-
const {
|
|
17
|
-
table, columns, sql, filters, form, meta,
|
|
18
|
-
} = loadTable;
|
|
19
|
-
const { pk } = await getMeta(table);
|
|
20
|
-
|
|
21
|
-
const cols = columns.map((el) => el.name || el).join(',');
|
|
22
|
-
const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
23
|
-
|
|
24
|
-
const fData = query.filter ? await getFilterSQL({
|
|
25
|
-
filter: query.filter,
|
|
26
|
-
table,
|
|
27
|
-
json: 1,
|
|
28
|
-
}) : {};
|
|
29
|
-
|
|
30
|
-
const keyQuery = query.key && loadTable.key && !params.id ? `${loadTable.key}=$1` : null;
|
|
31
|
-
|
|
32
|
-
const limit = Math.min(maxLimit, +(query.limit || 10));
|
|
33
|
-
|
|
34
|
-
const offset = query.page ? ` offset ${(query.page - 1) * limit}` : '';
|
|
35
|
-
// id, query, filter
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
import getTemplate from './utils/getTemplate.js';
|
|
2
|
+
import getFilterSQL from '../funcs/getFilterSQL/index.js';
|
|
3
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
+
|
|
5
|
+
const maxLimit = 100;
|
|
6
|
+
export default async function data(req) {
|
|
7
|
+
const time = Date.now();
|
|
8
|
+
const {
|
|
9
|
+
pg, params, query = {},
|
|
10
|
+
} = req;
|
|
11
|
+
|
|
12
|
+
const loadTable = await getTemplate('table', params.table);
|
|
13
|
+
|
|
14
|
+
if (!loadTable) { return { status: 404, message: 'not found' }; }
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
table, columns, sql, filters, form, meta,
|
|
18
|
+
} = loadTable;
|
|
19
|
+
const { pk } = await getMeta(table);
|
|
20
|
+
|
|
21
|
+
const cols = columns.map((el) => el.name || el).join(',');
|
|
22
|
+
const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
23
|
+
|
|
24
|
+
const fData = query.filter ? await getFilterSQL({
|
|
25
|
+
filter: query.filter,
|
|
26
|
+
table,
|
|
27
|
+
json: 1,
|
|
28
|
+
}) : {};
|
|
29
|
+
|
|
30
|
+
const keyQuery = query.key && loadTable.key && !params.id ? `${loadTable.key}=$1` : null;
|
|
31
|
+
|
|
32
|
+
const limit = Math.min(maxLimit, +(query.limit || 10));
|
|
33
|
+
|
|
34
|
+
const offset = query.page ? ` offset ${(query.page - 1) * limit}` : '';
|
|
35
|
+
// id, query, filter
|
|
36
|
+
const orderColumn = query.order || loadTable.order;
|
|
37
|
+
|
|
38
|
+
const order = cols.includes(orderColumn) ? `order by ${orderColumn} ${query.desc ? 'desc' : ''}` : '';
|
|
39
|
+
const where = [(params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable.query, fData.q].filter((el) => el);
|
|
40
|
+
const q = `select ${pk ? `"${pk}" as id,` : ''} ${query.id || query.key ? '*' : cols || '*'} from ${table} t ${sqlTable} where ${where.join(' and ') || 'true'} ${offset} ${order} limit ${limit}`;
|
|
41
|
+
|
|
42
|
+
if (query.sql) { return q; }
|
|
43
|
+
|
|
44
|
+
const { rows } = await pg.query(q, (params.id ? [params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
|
|
45
|
+
|
|
46
|
+
const total = keyQuery || params.id ? rows.length : await pg.queryCache(`select count(*) from ${table} t where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
time: Date.now() - time, total, pk, form, rows, meta, columns, filters,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import getTemplate from './utils/getTemplate.js';
|
|
2
|
-
import getSelect from './utils/getSelect.js';
|
|
3
|
-
|
|
4
|
-
export default async function filter(req) {
|
|
5
|
-
const time = Date.now();
|
|
6
|
-
const {
|
|
7
|
-
params,
|
|
8
|
-
} = req;
|
|
9
|
-
const loadTable = await getTemplate('table', params.table);
|
|
10
|
-
if (!loadTable) { return { status: 404, message: 'not found' }; }
|
|
11
|
-
|
|
12
|
-
await Promise.all(loadTable?.filters.filter((el) => el.data).map(async (el) => {
|
|
13
|
-
const cls = await getSelect(el.data);
|
|
14
|
-
Object.assign(el, { options: cls?.arr });
|
|
15
|
-
}));
|
|
16
|
-
return { time: Date.now() - time, filters: loadTable?.filters || [] };
|
|
17
|
-
}
|
|
1
|
+
import getTemplate from './utils/getTemplate.js';
|
|
2
|
+
import getSelect from './utils/getSelect.js';
|
|
3
|
+
|
|
4
|
+
export default async function filter(req) {
|
|
5
|
+
const time = Date.now();
|
|
6
|
+
const {
|
|
7
|
+
params,
|
|
8
|
+
} = req;
|
|
9
|
+
const loadTable = await getTemplate('table', params.table);
|
|
10
|
+
if (!loadTable) { return { status: 404, message: 'not found' }; }
|
|
11
|
+
|
|
12
|
+
await Promise.all(loadTable?.filters.filter((el) => el.data).map(async (el) => {
|
|
13
|
+
const cls = await getSelect(el.data);
|
|
14
|
+
Object.assign(el, { options: cls?.arr });
|
|
15
|
+
}));
|
|
16
|
+
return { time: Date.now() - time, filters: loadTable?.filters || [] };
|
|
17
|
+
}
|