@opengis/fastify-table 1.2.84 → 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.84",
3
+ "version": "1.2.85",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -8,21 +8,9 @@ import getSelect from './getSelect.js';
8
8
  const limit = 50;
9
9
  const selectMeta = {};
10
10
 
11
- export default async function getSelectMeta({ name, pg = pgClients.client, nocache, table, column, key, val }) {
11
+ export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
12
12
  if (selectMeta[name] && !nocache) return selectMeta[name];
13
13
 
14
- if (table && column) {
15
- const original = {
16
- false: `with c(id,text) as (select ${column || 'row_number() over()'}, ${column}, count(*) from ${table} group by ${column} limit ${limit}) select * from c`,
17
- true: `with c(id,text) as (select ${column || 'row_number() over()'}, ${column} from ${table}) select * from c`,
18
- }[!!(key || val)];
19
- return {
20
- original,
21
- searchQuery: '(lower("text") ~ $1 )',
22
- pk: 'id',
23
- };
24
- }
25
-
26
14
  const cls = await getSelect(name, pg);
27
15
  const db = typeof cls?.db === 'string' ? { database: cls.db } : cls?.db;
28
16
  const pg1 = cls?.db ? getPG(db) : pg;
@@ -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,
@@ -42,7 +54,9 @@ export default async function suggest(req) {
42
54
  return { status: 400, message: 'param limit is invalid' };
43
55
  }
44
56
 
45
- const meta = await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache, table, column, key: query?.key, val: query?.val });
57
+ const meta = table && column
58
+ ? getTableColumnMeta(table, column, query?.key || query?.val)
59
+ : await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache });
46
60
 
47
61
  if (meta?.minLength && query.key && query.key.length < meta?.minLength) {
48
62
  return { message: `min length: ${meta.minLength}` };