@opengis/fastify-table 2.0.18 → 2.0.20

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.
@@ -1,11 +1,40 @@
1
- import { readFile } from "node:fs/promises";
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import config from "../../../../config.js";
2
3
  import loadTemplate from "./loadTemplate.js";
3
- import { existsSync } from "node:fs";
4
- async function getColumnCLSData(name) {
5
- const exists = existsSync("module/cls.json");
6
- const json = exists
7
- ? JSON.parse((await readFile("module/cls.json", "utf-8")) || "{}")
8
- : {};
4
+ import getTemplates from "./getTemplates.js";
5
+ import getTemplateSync from "./getTemplateSync.js";
6
+ const cls = getTemplates("cls")
7
+ .map((el) => el[0])
8
+ .reduce((acc, curr) => ({ ...acc, [curr]: curr }), {});
9
+ const select = getTemplates("select")
10
+ .map((el) => el[0])
11
+ .reduce((acc, curr) => ({ ...acc, [curr]: curr }), {});
12
+ const exists = existsSync("module/cls.json");
13
+ const json = exists
14
+ ? JSON.parse(readFileSync("module/cls.json", "utf-8") || "{}")
15
+ : {};
16
+ Object.assign(json, cls, select);
17
+ function getColumnCLSData(table, name) {
18
+ const type = select[`${table}.${name}`] || select[name || ""]
19
+ ? "select"
20
+ : json[`${table}.${name}`] || json[name || ""]
21
+ ? "cls"
22
+ : null;
23
+ if (!type)
24
+ return null;
25
+ // name = table + column or name = column
26
+ if (!name?.includes(".") && (json[`${table}.${name}`] || json[name || ""])) {
27
+ const clsName = json[`${table}.${name}`] || json[name || ""];
28
+ const result = { name: clsName, type };
29
+ if (type === "select") {
30
+ const data = getTemplateSync("select", clsName);
31
+ Object.assign(result, {
32
+ sql: typeof data === "string" ? data : data?.sql,
33
+ });
34
+ }
35
+ return result;
36
+ }
37
+ // name = table
9
38
  return name
10
39
  ? Object.keys(json)
11
40
  .filter((key) => key.startsWith(`${name}.`))
@@ -15,11 +44,11 @@ async function getColumnCLSData(name) {
15
44
  }), {})
16
45
  : json;
17
46
  }
18
- export default async function getColumnCLS(name) {
19
- const key = [name, "column-cls"].filter(Boolean).join("-");
20
- if (loadTemplate[key]) {
47
+ export default function getColumnCLS(table, name) {
48
+ const key = [name, table, "column-cls"].filter(Boolean).join("-");
49
+ if (loadTemplate[key] && !config.local) {
21
50
  return loadTemplate[key];
22
51
  }
23
- loadTemplate[key] = await getColumnCLSData(name);
52
+ loadTemplate[key] = getColumnCLSData(table, name);
24
53
  return loadTemplate[key];
25
54
  }
@@ -7,8 +7,8 @@ export default function getTemplateDir(type) {
7
7
  const arr = Array.isArray(type)
8
8
  ? type.map((el) => getTemplatePath(el)).flat()
9
9
  : getTemplatePath(type);
10
- if (!loadTemplate[type]) {
11
- loadTemplate[type] = arr.filter((el) => fs.existsSync(el[1]));
10
+ if (!loadTemplate[type.toString()]) {
11
+ loadTemplate[type.toString()] = arr.filter((el) => fs.existsSync(el[1]));
12
12
  }
13
- return loadTemplate[type];
13
+ return loadTemplate[type.toString()];
14
14
  }
@@ -1,5 +1,6 @@
1
1
  import { handlebars } from "../../../../helpers/index.js";
2
2
  import getTemplate from "../getTemplate.js";
3
+ import getTemplates from "../getTemplates.js";
3
4
  import getSelectVal from "./getSelectVal.js";
4
5
  import pgClients from "../../../pg/pgClients.js";
5
6
  export default async function metaFormat({ rows: original, table, cls = {}, htmls = {}, sufix = true, reassign = true, }, pg = pgClients.client) {
@@ -11,11 +12,21 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
11
12
  name: el,
12
13
  data: loadTable?.meta?.cls[el],
13
14
  }));
15
+ const list = getTemplates(["cls", "select"]).map((el) => el[0]);
16
+ const columnNameAsCls = Object.keys(original?.[0] || {})
17
+ .filter((key) => list.includes(`${table}.${key}`) || list.includes(key))
18
+ .map((key) => ({
19
+ name: key,
20
+ data: list.find((clsName) => [key, `${table}.${key}`].includes(clsName)),
21
+ }));
14
22
  const htmlCols = Object.keys(htmls || {})
15
23
  .map((key) => ({ name: key, html: htmls[key] }))
16
24
  .concat(loadTable?.columns?.filter?.((e) => e.name && e.html && e.format && ["html", "slot"].includes(e.format)) || []);
17
25
  if (!original?.length ||
18
- (!selectCols?.length && !metaCls?.length && !htmlCols?.length))
26
+ (!selectCols?.length &&
27
+ !metaCls?.length &&
28
+ !columnNameAsCls.length &&
29
+ !htmlCols?.length))
19
30
  return original;
20
31
  const rows = reassign ? original : JSON.parse(JSON.stringify(original));
21
32
  // html && slot (vue component) format
@@ -31,7 +42,10 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
31
42
  }));
32
43
  }));
33
44
  // cls & select format
34
- await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
45
+ await Promise.all(selectCols
46
+ .concat(metaCls)
47
+ .concat(columnNameAsCls)
48
+ ?.map(async (attr) => {
35
49
  const values = [
36
50
  ...new Set(rows?.map((el) => el[attr.name]).flat()),
37
51
  ].filter((el) => (typeof el === "boolean" ? true : el));
@@ -52,9 +66,7 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
52
66
  }
53
67
  else {
54
68
  Object.assign(el, {
55
- [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: val.color
56
- ? val
57
- : val.text || val,
69
+ [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: val.color ? val : val.text || val,
58
70
  });
59
71
  }
60
72
  });
@@ -18,6 +18,9 @@ export default async function filterAPI(req, reply, iscalled) {
18
18
  if (!table) {
19
19
  return { status: 404, message: "not found" };
20
20
  }
21
+ if (!pg.pk?.[table]) {
22
+ return { status: 404, message: "table not found" };
23
+ }
21
24
  const sqlTable = sql
22
25
  ?.filter?.((el) => !el?.disabled && el?.sql?.replace)
23
26
  ?.map?.((el, i) => ` left join lateral (${el.sql.replace("{{uid}}", user?.uid)}) ${el.name || `t${i}`} on 1=1 `)
@@ -1,6 +1,6 @@
1
1
  import path from "node:path";
2
2
  import { existsSync, readFileSync } from "node:fs";
3
- import { config, getPG, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal, logger, getSelect, } from "../../../../utils.js";
3
+ import { config, getPG, getTemplate, getSelectMeta, getMeta, applyHook, getSelectVal, logger, getSelect, metaFormat, getColumnCLS, } from "../../../../utils.js";
4
4
  const limit = 50;
5
5
  const headers = {
6
6
  "Access-Control-Allow-Origin": "*",
@@ -208,6 +208,27 @@ export default async function suggest(req) {
208
208
  // const { rows: dataNew1 } = dataNew.length < limit ? await pg.query(sqlSuggest, query.key ? [`${query.key}`] : []) : {};
209
209
  // const ids = dataNew.map((el) => el.id);
210
210
  const data = dataNew.filter((el) => el.id && el.text);
211
+ if (tableName && column) {
212
+ const { name = column, type = "select" } = getColumnCLS(tableName, column) || {};
213
+ await metaFormat({
214
+ rows: data,
215
+ table: tableName,
216
+ cls: { text: name },
217
+ sufix: true,
218
+ });
219
+ return {
220
+ time: Date.now() - time,
221
+ limit: Math.min(query.limit || meta.limit || limit, limit),
222
+ count: data.length,
223
+ total: meta.count - 0,
224
+ mode: type === "cls" ? "array" : "sql",
225
+ db: meta.db,
226
+ data: data.map((el) => ({
227
+ count: el.count,
228
+ ...(el.text_data || {}),
229
+ })),
230
+ };
231
+ }
211
232
  if (query.sel) {
212
233
  const clsData = await getSelectVal({
213
234
  pg,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.0.18",
3
+ "version": "2.0.20",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [