@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,gBAyE5D"}
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({ name: "client" });
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 table1 = opt?.table || opt;
15
- const table = pg.pk?.[table1] ? table1 : table1?.replace?.(/"/g, "");
16
- if (pg?.options?.database && data[pg.options.database]?.[table] && !nocache)
17
- return data[pg.options.database][table];
18
- if (!pg?.tlist?.includes(table) &&
19
- !pg?.tlist?.includes(table?.replace?.(/"/g, ""))) {
20
- throw BadRequestError(`table not found: ${table}`);
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 { fields = [] } = await pg.query(`select * from ${table} limit 0`);
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) as pks1
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" and c.conrelid::regclass = $1::regclass`, [table])
28
- .then((el) => el.rows[0].pks1 || {});
29
- const pk = table.startsWith("public.")
30
- ? pks1[table.replace("public.", "")]
31
- : pks1[table] || pg.pk[table];
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`, [table])
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(pg.relkinds?.[table]),
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][table] = res;
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,iBA6NtC;AAGD,eAAe,IAAI,CAAC"}
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"}
@@ -78,6 +78,8 @@ async function init(client) {
78
78
  throw new Error("empty pg client");
79
79
  try {
80
80
  const data = await client.query(q, args);
81
+ if (config.trace)
82
+ console.log(q, args);
81
83
  return data;
82
84
  }
83
85
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.4.18",
3
+ "version": "2.4.19",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [