@opengis/bi
Version:
BI data visualization module
35 lines (29 loc) • 1.4 kB
JavaScript
import { pgClients } from "@opengis/fastify-table/utils.js";
export default async function dbTables({ pg = pgClients.client, query = {} }) {
const q = `select
t.table_schema ||'."'|| t.table_name ||'"' as table,
obj_description(to_regclass(t.table_schema ||'."'|| t.table_name||'"')) as description,
t.table_schema as schema,
(select reltuples from pg_class where oid = to_regclass(t.table_schema ||'."'|| t.table_name||'"') ) as total,
coalesce(isgeom,false) as isgeom
from information_schema.tables t
left join lateral(
select true as isgeom from information_schema.columns c
where c.table_name = t.table_name
and c.table_schema = t.table_schema and 'geometry'=c.udt_name limit 1
)c on 1=1
where t.table_type = 'BASE TABLE'
and t.table_schema not in ('public','log','admin','feature_ir','gis', 'setting')
and t.table_name not like '%.%'
and regexp_replace(t.table_name, '^[[:digit:]]', '', 'g') = t.table_name
and 1=(SELECT count(*) FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
WHERE nsp.nspname = t.table_schema AND rel.relname = t.table_name and contype='p'
)
and isgeom
order by total desc`;
if (query.sql) return q;
const { rows = [] } = await pg.queryCache(q, { time: 0 });
return { rows };
}