@opengis/fastify-table 1.2.84 → 1.2.86

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.86",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -31,7 +31,7 @@
31
31
  "@fastify/http-proxy": "9.5.0",
32
32
  "@fastify/sensible": "^5.0.0",
33
33
  "@fastify/url-data": "5.4.0",
34
- "@opengis/fastify-hb": "1.4.8",
34
+ "@opengis/fastify-hb": "1.7.0",
35
35
  "fastify": "^4.26.1",
36
36
  "fastify-plugin": "^4.0.0",
37
37
  "ioredis": "5.3.2",
@@ -11,7 +11,8 @@ import init from './init.js';
11
11
  import getDBParams from './getDBParams.js';
12
12
 
13
13
  function getPG(param) {
14
- const dbListParams = dblist.find(el => el.database === (param?.db || param?.database || param));
14
+ const dbListParams = dblist.find(el => el.key === param?.key)
15
+ || dblist.find(el => el.database === (param?.db || param?.database || param));
15
16
  const {
16
17
  user, password, host, port, db, database, name: origin,
17
18
  } = dbListParams ?? (typeof param === 'string' ? getDBParams(param) : param || {});
@@ -11,7 +11,8 @@ import init from './init.js';
11
11
  import getDBParams from './getDBParams.js';
12
12
 
13
13
  async function getPGAsync(param) {
14
- const dbListParams = dblist.find(el => el.database === (param?.db || param?.database || param));
14
+ const dbListParams = dblist.find(el => el.key === param?.key)
15
+ || dblist.find(el => el.database === (param?.db || param?.database || param));
15
16
  const {
16
17
  user, password, host, port, db, database, name: origin,
17
18
  } = dbListParams ?? (typeof param === 'string' ? getDBParams(param) : param || {});
@@ -1,4 +1,5 @@
1
1
  import getTemplate from '../getTemplate.js';
2
+ import getSelect from '../getSelect.js';
2
3
  import pgClients from '../../../pg/pgClients.js';
3
4
  import config from '../../../../../config.js';
4
5
 
@@ -96,7 +97,8 @@ export default async function getFilterSQL({
96
97
  Object.assign(el, { extra: { table: extraDataTable, input: extraColumns.find(item => item?.name === el?.name)?.type } });
97
98
  }
98
99
  if (!el?.data) return el;
99
- const cls = await getTemplate(['cls', 'select'], el.data);
100
+ // const cls = await getTemplate(['cls', 'select'], el.data); // only git cls
101
+ const cls = await getSelect(el.data, pg); // git + db cls
100
102
 
101
103
  if (Array.isArray(cls) && cls?.length) {
102
104
  Object.assign(el, { cls: el.data, options: cls, select: `select code, name from admin.cls where parent='${el.data}'` });
@@ -23,7 +23,7 @@ export default function formatValue({
23
23
  const matchNull = { null: 'is null', notnull: 'is not null' }[value];
24
24
  // null = true is null, not false
25
25
  const matchBoolean = fieldType === 'boolean' ? { true: 'is true', false: 'is false' }[value] : null;
26
- const matchMulti = fieldType.includes('[]') ? `::text[] && '{${value}}'::text[]` : null;
26
+ const matchMulti = fieldType?.includes('[]') ? `::text[] && '{${value}}'::text[]` : null;
27
27
  const match = matchNull || matchBoolean || matchMulti || `::text=any('{${value}}'::text[])`;
28
28
 
29
29
  // geometry
@@ -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}` };