@opengis/fastify-table 1.2.83 → 1.2.85

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.2.83",
3
+ "version": "1.2.85",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -0,0 +1,21 @@
1
+ import { dblist, getRedis } from '../../../../utils.js';
2
+
3
+ import formatData from '../utils/formatData.js';
4
+
5
+ const rclient = getRedis();
6
+
7
+ export default async function readItemList(req) {
8
+ const rows = formatData(dblist);
9
+
10
+ const uid = req.session?.passport?.user?.uid // login db
11
+ || req.session?.passport?.user?.username // login passwd
12
+ || '2';
13
+
14
+ const key = `current-db:${uid}`;
15
+ const ttl = await rclient.ttl(key);
16
+ const currentId = await rclient.get(key);
17
+ rclient.setex(key, 60 * 60 * 10000, currentId);
18
+
19
+ const { originalMaxAge, expires } = req.session?.cookie || {};
20
+ return { ttl, current: currentId || rows[0]?.id, rows, user: { ...req.user, originalMaxAge, expires, uid }, };
21
+ }
@@ -0,0 +1,25 @@
1
+ import { dblist, getRedis } from '../../../../utils.js';
2
+
3
+ const rclient = getRedis();
4
+
5
+ export default async function setItem(req) {
6
+ const { params = {} } = req;
7
+ const { id } = params;
8
+
9
+ if (!id) {
10
+ return { error: 'not enough params', status: 400 };
11
+ }
12
+
13
+ const current = dblist.find((el) => [el.id, el.key].includes(id));
14
+ if (!current?.database) {
15
+ return { error: 'invalid param id', status: 400 };
16
+ }
17
+
18
+ const uid = req.session?.passport?.user?.uid // login db
19
+ || req.session?.passport?.user?.username // login passwd
20
+ || '2';
21
+
22
+ await rclient.setex(`current-db:${uid}`, 60 * 60 * 10000, id);
23
+
24
+ return { current: id };
25
+ }
@@ -0,0 +1,18 @@
1
+ import readItemList from './controllers/readItems.js';
2
+ import setItem from './controllers/setItem.js';
3
+
4
+ export default async function plugin(fastify) {
5
+
6
+ fastify.route({
7
+ method: 'GET',
8
+ url: '/db-list',
9
+ config: { policy: ['site'] },
10
+ handler: readItemList,
11
+ });
12
+ fastify.route({
13
+ method: 'GET',
14
+ url: '/db-list/:id',
15
+ config: { policy: ['site'] },
16
+ handler: setItem,
17
+ });
18
+ }
@@ -0,0 +1,7 @@
1
+ const showKeys = ['id', 'key', 'title'];
2
+
3
+ export default function formatData(data = []) {
4
+ return data?.length
5
+ ? data.map((el) => Object.keys(el).filter((key) => showKeys.includes(key)).reduce((acc, curr) => Object.assign(acc, { [curr]: el[curr] }), {}))
6
+ : [];
7
+ }
@@ -9,6 +9,18 @@ const headers = {
9
9
  'Cache-Control': 'no-cache',
10
10
  };
11
11
 
12
+ function getTableColumnMeta(table, column, filtered) {
13
+ const original = {
14
+ true: `with c(id,text) as (select ${column}, ${column} from ${table} group by ${column}) select id, text from c`,
15
+ false: `with c(id,text) as (select ${column}, ${column}, count(*) from ${table} group by ${column} limit ${limit}) select * from c`,
16
+ }[!!filtered];
17
+ return {
18
+ original,
19
+ searchQuery: '(lower("text") ~ $1 )',
20
+ pk: 'id',
21
+ };
22
+ }
23
+
12
24
  export default async function suggest(req) {
13
25
  const {
14
26
  params, query, pg: pg1, user,
@@ -43,10 +55,7 @@ export default async function suggest(req) {
43
55
  }
44
56
 
45
57
  const meta = table && column
46
- ? {
47
- original: `with c(id,text) as (select ${column || 'row_number() over()'}, ${column}, count(*) from ${tableName} group by ${column} limit ${limit}) select * from c`,
48
- searchQuery: '(lower("text") ~ $1 )',
49
- }
58
+ ? getTableColumnMeta(table, column, query?.key || query?.val)
50
59
  : await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache });
51
60
 
52
61
  if (meta?.minLength && query.key && query.key.length < meta?.minLength) {