@opengis/fastify-table 2.0.19 → 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 {
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import config from "../../../../config.js";
|
|
2
3
|
import loadTemplate from "./loadTemplate.js";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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] =
|
|
52
|
+
loadTemplate[key] = getColumnCLSData(table, name);
|
|
24
53
|
return loadTemplate[key];
|
|
25
54
|
}
|
|
@@ -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,
|