@opengis/fastify-table 2.4.18 → 2.4.19
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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getMeta.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/pg/funcs/getMeta.ts"],"names":[],"mappings":"AAMA,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"getMeta.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/pg/funcs/getMeta.ts"],"names":[],"mappings":"AAMA,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,gBAmG5D"}
|
|
@@ -3,7 +3,7 @@ import getPG from "./getPG.js";
|
|
|
3
3
|
import { BadRequestError } from "../../../../errors.js";
|
|
4
4
|
const data = {};
|
|
5
5
|
export default async function getMeta(opt, nocache) {
|
|
6
|
-
const pg = opt?.pg || getPG(
|
|
6
|
+
const pg = opt?.pg || getPG();
|
|
7
7
|
if (!pg) {
|
|
8
8
|
throw BadRequestError("pg client not found");
|
|
9
9
|
}
|
|
@@ -11,24 +11,42 @@ export default async function getMeta(opt, nocache) {
|
|
|
11
11
|
if (!pg.pk || nocache) {
|
|
12
12
|
await init(pg);
|
|
13
13
|
}
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
const tableOriginal = opt?.table || opt;
|
|
15
|
+
const [schemaName, tableName] = tableOriginal?.replace?.(/"/g, "").split(".");
|
|
16
|
+
const tableSafe = `${schemaName}."${tableName}"`;
|
|
17
|
+
const tableUnsafe = `${schemaName}.${tableName}`;
|
|
18
|
+
if (pg?.options?.database &&
|
|
19
|
+
data[pg.options.database]?.[tableUnsafe] &&
|
|
20
|
+
!nocache) {
|
|
21
|
+
return data[pg.options.database][tableUnsafe];
|
|
21
22
|
}
|
|
22
|
-
const
|
|
23
|
+
const tableRows = await pg
|
|
24
|
+
.query(`SELECT
|
|
25
|
+
nspname || '.' || relname as name,
|
|
26
|
+
relkind
|
|
27
|
+
FROM pg_class a
|
|
28
|
+
JOIN pg_namespace b
|
|
29
|
+
ON b.oid = a.relnamespace
|
|
30
|
+
WHERE relkind IN ('r', 'v', 'm')`)
|
|
31
|
+
.then((r) => r.rows || []);
|
|
32
|
+
const tlist = tableRows.map((el) => el.name);
|
|
33
|
+
if (!tlist.includes(tableUnsafe)) {
|
|
34
|
+
throw BadRequestError(`table not found: ${tableOriginal}`);
|
|
35
|
+
}
|
|
36
|
+
const relkinds = tableRows.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.relkind }), {});
|
|
37
|
+
const fields = await pg
|
|
38
|
+
.query(`select * from ${tableSafe} limit 0`)
|
|
39
|
+
.then((r) => r.fields || []);
|
|
40
|
+
// remove quotes from table names
|
|
23
41
|
const pks1 = await pg
|
|
24
|
-
.query(`SELECT json_object_agg(c.conrelid::regclass, a.attname)
|
|
42
|
+
.query(`SELECT json_object_agg(replace(c.conrelid::regclass::text,'"',''), a.attname)
|
|
25
43
|
FROM pg_constraint c
|
|
26
44
|
left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1]
|
|
27
|
-
WHERE c.contype='p'::"char"
|
|
28
|
-
.then((
|
|
29
|
-
const pk =
|
|
30
|
-
? pks1[
|
|
31
|
-
: pks1[
|
|
45
|
+
WHERE c.contype='p'::"char"`)
|
|
46
|
+
.then((r) => r.rows[0].json_object_agg || {});
|
|
47
|
+
const pk = tableUnsafe.startsWith("public.")
|
|
48
|
+
? pks1[tableUnsafe.replace("public.", "")]
|
|
49
|
+
: pks1[tableUnsafe];
|
|
32
50
|
const geomColumns = fields.filter((el) => pg.pgType?.[el.dataTypeID] === "geometry");
|
|
33
51
|
const geomAttr = geomColumns.find((el) => el.name === "geom_4326") || geomColumns[0];
|
|
34
52
|
const dbColumns = await pg
|
|
@@ -36,21 +54,20 @@ export default async function getMeta(opt, nocache) {
|
|
|
36
54
|
attname,
|
|
37
55
|
json_build_object(
|
|
38
56
|
'title', pg_catalog.col_description(attrelid,attnum)
|
|
39
|
-
/*,'type', atttypid::regtype */
|
|
40
57
|
)
|
|
41
58
|
) from pg_catalog.pg_attribute a
|
|
42
|
-
where attrelid=$1::regclass and attnum>0`, [
|
|
59
|
+
where attrelid=$1::regclass and attnum>0`, [tableSafe])
|
|
43
60
|
.then((el) => el.rows?.[0]?.json_object_agg || {});
|
|
44
61
|
fields.forEach((el) => Object.assign(el, { ...(dbColumns[el.name] || {}) }));
|
|
45
62
|
const res = {
|
|
46
63
|
pk,
|
|
47
64
|
columns: fields,
|
|
48
65
|
geom: geomAttr?.name,
|
|
49
|
-
view: ["v", "m"].includes(
|
|
66
|
+
view: ["v", "m"].includes(relkinds?.[tableSafe]),
|
|
50
67
|
};
|
|
51
68
|
if (!data[pg.options.database]) {
|
|
52
69
|
data[pg.options.database] = {};
|
|
53
70
|
}
|
|
54
|
-
data[pg.options.database][
|
|
71
|
+
data[pg.options.database][tableUnsafe] = res;
|
|
55
72
|
return res;
|
|
56
73
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/pg/funcs/init.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIpD,iBAAe,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/pg/funcs/init.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIpD,iBAAe,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,iBA8NtC;AAGD,eAAe,IAAI,CAAC"}
|