@opengis/fastify-table 1.0.2 → 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/Changelog.md +8 -0
- package/crud/controllers/deleteCrud.js +4 -4
- package/crud/funcs/dataDelete.js +15 -0
- package/crud/funcs/getOpt.js +2 -2
- package/crud/funcs/setOpt.js +1 -1
- package/package.json +1 -1
- package/pg/funcs/getPG.js +2 -1
- package/redis/funcs/getRedis.js +2 -1
- package/redis/index.js +1 -0
- package/server.js +14 -0
- package/table/controllers/data.js +6 -3
- package/table/controllers/suggest.js +4 -1
- package/table/controllers/utils/getSelectMeta.js +4 -3
- package/test/templates/select/account_id.json +4 -0
- package/test/templates/select/account_id.sql +1 -0
- package/test/templates/table/gis.dataset.table.json +4 -26
- package/test/templates/table/service.json +18 -0
package/Changelog.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import dataDelete from '../funcs/dataDelete.js';
|
|
2
|
+
|
|
1
3
|
export default async function deleteCrud(req) {
|
|
2
|
-
const { funcs } = req;
|
|
3
4
|
const { table, id } = req.params || {};
|
|
4
5
|
if (!table) return { status: 404, message: 'table is required' };
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
const data = await funcs.pg.query(`delete from ${table} where ${pk}=$1`, [id]);
|
|
7
|
+
const data = await dataDelete({ table, id });
|
|
8
8
|
|
|
9
|
-
return { rowCount: data.rowCount };
|
|
9
|
+
return { rowCount: data.rowCount, msg: !data.rowCount ? data : null };
|
|
10
10
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
+
|
|
3
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
+
|
|
5
|
+
export default async function dataDelete({
|
|
6
|
+
table, id,
|
|
7
|
+
}) {
|
|
8
|
+
const pg = getPG({ name: 'client' });
|
|
9
|
+
const { pk } = await getMeta(table);
|
|
10
|
+
if (!pg.tlist.includes(table)) return 'table not exist';
|
|
11
|
+
const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
|
|
12
|
+
// console.log(updateDataset);
|
|
13
|
+
const res = await pg.one(delQuery, [id]) || {};
|
|
14
|
+
return res;
|
|
15
|
+
}
|
package/crud/funcs/getOpt.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
2
|
|
|
3
|
-
export default async function getOpt(token) {
|
|
4
|
-
const rclient = getRedis({ db: 0 });
|
|
3
|
+
export default async function getOpt(token, funcs) {
|
|
4
|
+
const rclient = getRedis({ db: 0, funcs });
|
|
5
5
|
|
|
6
6
|
const key = `opt:${token}`;
|
|
7
7
|
const data = await rclient.get(key);
|
package/crud/funcs/setOpt.js
CHANGED
|
@@ -10,7 +10,7 @@ export default async function setOpt(params) {
|
|
|
10
10
|
// const token = md5(params);
|
|
11
11
|
const key = `opt:${token}`;
|
|
12
12
|
|
|
13
|
-
const rclient = getRedis({ db: 0 });
|
|
13
|
+
const rclient = getRedis({ db: 0, funcs: params.funcs });
|
|
14
14
|
await rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
|
|
15
15
|
return token;
|
|
16
16
|
}
|
package/package.json
CHANGED
package/pg/funcs/getPG.js
CHANGED
|
@@ -4,8 +4,9 @@ import pgClients from '../pgClients.js';
|
|
|
4
4
|
import init from './init.js';
|
|
5
5
|
|
|
6
6
|
function getPG({
|
|
7
|
-
user, password, host, port, db, database, name: origin,
|
|
7
|
+
user, password, host, port, db, database, name: origin, funcs,
|
|
8
8
|
}) {
|
|
9
|
+
if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
|
|
9
10
|
const name = origin || db || database;
|
|
10
11
|
if (pgClients[name]) return pgClients[name];
|
|
11
12
|
|
package/redis/funcs/getRedis.js
CHANGED
|
@@ -2,7 +2,8 @@ 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, funcs }) {
|
|
6
|
+
if (funcs?.config) Object.assign(config, { ...funcs.config }); // unit test
|
|
6
7
|
if (!config.redis) return null;
|
|
7
8
|
if (redisClients[db]) return redisClients[db];
|
|
8
9
|
|
package/redis/index.js
CHANGED
|
@@ -11,6 +11,7 @@ async function plugin(fastify) {
|
|
|
11
11
|
const client = getRedis({ db: 0 });
|
|
12
12
|
client.getJSON = client.get;
|
|
13
13
|
fastify.decorate('rclient', client);
|
|
14
|
+
fastify.decorate('getRedis', getRedis);
|
|
14
15
|
// fastify.decorate('rclient2', client2);
|
|
15
16
|
fastify.addHook('onClose', close);
|
|
16
17
|
}
|
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
|
+
});
|
|
@@ -2,7 +2,7 @@ import getTemplate from './utils/getTemplate.js';
|
|
|
2
2
|
import getFilterSQL from '../funcs/getFilterSQL/index.js';
|
|
3
3
|
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
4
|
|
|
5
|
-
const maxLimit =
|
|
5
|
+
const maxLimit = 100;
|
|
6
6
|
export default async function data(req) {
|
|
7
7
|
const time = Date.now();
|
|
8
8
|
const {
|
|
@@ -33,14 +33,17 @@ export default async function data(req) {
|
|
|
33
33
|
|
|
34
34
|
const offset = query.page ? ` offset ${(query.page - 1) * limit}` : '';
|
|
35
35
|
// id, query, filter
|
|
36
|
+
const orderColumn = query.order || loadTable.order;
|
|
37
|
+
|
|
38
|
+
const order = cols.includes(orderColumn) ? `order by ${orderColumn} ${query.desc ? 'desc' : ''}` : '';
|
|
36
39
|
const where = [(params.id ? ` "${pk}" = $1` : null), keyQuery, loadTable.query, fData.q].filter((el) => el);
|
|
37
|
-
const q = `select ${pk ? `"${pk}" as id,` : ''} ${query.id || query.key ? '*' : cols || '*'} from ${table} t ${sqlTable} where ${where.join(' and ') || 'true'} ${offset} limit ${limit}`;
|
|
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}`;
|
|
38
41
|
|
|
39
42
|
if (query.sql) { return q; }
|
|
40
43
|
|
|
41
44
|
const { rows } = await pg.query(q, (params.id ? [params.id] : null) || (query.key && loadTable.key ? [query.key] : []));
|
|
42
45
|
|
|
43
|
-
const total = keyQuery || params.id ? rows.length : await pg.queryCache(`select count(*) from ${table} where ${where.join(' and ') || 'true'}`).then((el) => el?.rows[0]?.count);
|
|
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);
|
|
44
47
|
|
|
45
48
|
return {
|
|
46
49
|
time: Date.now() - time, total, pk, form, rows, meta, columns, filters,
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import getSelectMeta from './utils/getSelectMeta.js';
|
|
2
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
3
|
|
|
3
4
|
const limit = 50;
|
|
4
5
|
const headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Cache-Control': 'no-cache' };
|
|
5
6
|
|
|
6
7
|
export default async function suggest(req) {
|
|
7
|
-
const { params, query, pg } = req;
|
|
8
|
+
const { params, query, pg: pg1 } = req;
|
|
8
9
|
|
|
9
10
|
const lang = query.lang || 'ua';
|
|
10
11
|
const time = Date.now();
|
|
@@ -13,6 +14,7 @@ export default async function suggest(req) {
|
|
|
13
14
|
if (!selectName) return { headers, status: 400, message: 'name is required' };
|
|
14
15
|
|
|
15
16
|
const meta = await getSelectMeta({ name: selectName });
|
|
17
|
+
const pg = meta.db ? getPG(meta.db) : pg1;
|
|
16
18
|
if (!meta) return { headers, status: 404, message: 'Not found query select ' };
|
|
17
19
|
|
|
18
20
|
const { arr, searchQuery } = meta;
|
|
@@ -48,6 +50,7 @@ export default async function suggest(req) {
|
|
|
48
50
|
count: data.length,
|
|
49
51
|
total: meta.count - 0,
|
|
50
52
|
mode: 'sql',
|
|
53
|
+
db: meta.db,
|
|
51
54
|
sql: sqlSuggest,
|
|
52
55
|
data,
|
|
53
56
|
};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import getPG from '../../../pg/funcs/getPG.js';
|
|
3
3
|
|
|
4
4
|
import getSelect from './getSelect.js';
|
|
5
|
+
|
|
5
6
|
/*
|
|
6
7
|
function getTable(table) {
|
|
7
8
|
// eslint-disable-next-line class-methods-use-this
|
|
@@ -14,7 +15,6 @@ const selectMeta = {};
|
|
|
14
15
|
|
|
15
16
|
export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
16
17
|
if (selectMeta[name]) return selectMeta[name];
|
|
17
|
-
const pg = pg1 || getPG({ db: 'client' });
|
|
18
18
|
|
|
19
19
|
const cls = await getSelect(name);
|
|
20
20
|
|
|
@@ -22,6 +22,7 @@ export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
|
22
22
|
if (cls.arr) return cls;
|
|
23
23
|
if (!cls.sql) return null;
|
|
24
24
|
|
|
25
|
+
const pg = pg1 || getPG({ db: cls.db || 'client' });
|
|
25
26
|
const { sql: original } = cls;
|
|
26
27
|
const sql = `with c(id,text) as (${original} ) select * from c where id is not null `;
|
|
27
28
|
|
|
@@ -34,7 +35,7 @@ export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
|
34
35
|
const dataOrigin1 = await pg.query(`${original} limit 0`);
|
|
35
36
|
|
|
36
37
|
// const table = getTable(original);
|
|
37
|
-
const count = cls?.count ?? await pg.
|
|
38
|
+
const count = cls?.count ?? await pg.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
|
|
38
39
|
|
|
39
40
|
// column name
|
|
40
41
|
const cols = dataOrigin.fields.map((el) => el.name);
|
|
@@ -50,7 +51,7 @@ export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
|
50
51
|
type: type.join(','),
|
|
51
52
|
cols: cols.join(','),
|
|
52
53
|
originalCols: dataOrigin1.fields.map((el) => el.name).join(','),
|
|
53
|
-
|
|
54
|
+
db: cls.db,
|
|
54
55
|
original,
|
|
55
56
|
sql,
|
|
56
57
|
count,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
SELECT account_id, account_name FROM crm_acc.crm_account
|
|
@@ -10,34 +10,12 @@
|
|
|
10
10
|
}
|
|
11
11
|
],
|
|
12
12
|
"table": "gis.dataset",
|
|
13
|
+
"order": "dataset_name",
|
|
13
14
|
"filters": [
|
|
14
15
|
{
|
|
15
|
-
"ua": "
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"type": "Text"
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
"ua": "Рік будівництва",
|
|
22
|
-
"name": "cp_year",
|
|
23
|
-
"type": "Text"
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
"ua": "Замовник ПКД",
|
|
27
|
-
"data": "customer_name",
|
|
28
|
-
"name": "cp_pkd",
|
|
29
|
-
"type": "Text"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
"ua": "Наявність енергоаудиту",
|
|
33
|
-
"name": "cp_date_en_audit",
|
|
34
|
-
"type": "Date"
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
"ua": "Знак сертифікату",
|
|
38
|
-
"data": "cp_certificate_mark",
|
|
39
|
-
"name": "cp_certificate_mark",
|
|
40
|
-
"type": "Check"
|
|
16
|
+
"ua": "Назва набору",
|
|
17
|
+
"name": "dataset_name",
|
|
18
|
+
"type": "text"
|
|
41
19
|
}
|
|
42
20
|
]
|
|
43
21
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"table": "site.page_node",
|
|
3
|
+
"key": "alias",
|
|
4
|
+
"query": "t.category_id='3060540645924931148' and t.enabled and t.lang='ua'",
|
|
5
|
+
"orderby": "value_number",
|
|
6
|
+
"sql": [
|
|
7
|
+
{
|
|
8
|
+
"sql": "select object_id,value_number from site.page_node_data where attr='ord' and t.nid=object_id limit 1",
|
|
9
|
+
"name": "page_node_data_sql"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
12
|
+
"columns": [
|
|
13
|
+
"title",
|
|
14
|
+
"alias",
|
|
15
|
+
"teaser",
|
|
16
|
+
"image"
|
|
17
|
+
]
|
|
18
|
+
}
|