prostgles-server 4.0.38 → 4.0.40
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.
- package/dist/DboBuilder/ViewHandler.js +1 -1
- package/dist/DboBuilder/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.d.ts +3 -0
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.d.ts.map +1 -0
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.js +204 -0
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.js.map +1 -0
- package/dist/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder.js +2 -199
- package/dist/DboBuilder.js.map +1 -1
- package/lib/DboBuilder/ViewHandler.js +1 -1
- package/lib/DboBuilder/ViewHandler.ts +1 -1
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.d.ts +3 -0
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.d.ts.map +1 -0
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.js +203 -0
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +209 -0
- package/lib/DboBuilder.d.ts.map +1 -1
- package/lib/DboBuilder.js +2 -199
- package/lib/DboBuilder.ts +1 -206
- package/package.json +1 -1
- package/tests/client/PID.txt +1 -1
- package/tests/server/package-lock.json +1 -1
package/lib/DboBuilder.ts
CHANGED
|
@@ -9,6 +9,7 @@ import * as Bluebird from "bluebird";
|
|
|
9
9
|
import * as pgPromise from 'pg-promise';
|
|
10
10
|
import { runSQL } from "./DboBuilder/runSQL";
|
|
11
11
|
import pg = require('pg-promise/typescript/pg-subset');
|
|
12
|
+
import { getTablesForSchemaPostgresSQL } from "./DboBuilder/getTablesForSchemaPostgresSQL";
|
|
12
13
|
import {
|
|
13
14
|
ColumnInfo, SQLOptions,
|
|
14
15
|
DbJoinMaker,
|
|
@@ -787,212 +788,6 @@ async function getConstraints(db: DB, schema = "public", filter?: { table: strin
|
|
|
787
788
|
`);
|
|
788
789
|
}
|
|
789
790
|
|
|
790
|
-
async function getTablesForSchemaPostgresSQL({ db, runSQL }: DboBuilder, schema = "public"): Promise<TableSchema[]> {
|
|
791
|
-
const query =
|
|
792
|
-
`
|
|
793
|
-
SELECT
|
|
794
|
-
jsonb_build_object(
|
|
795
|
-
'insert', EXISTS (
|
|
796
|
-
SELECT 1
|
|
797
|
-
FROM information_schema.role_table_grants rg
|
|
798
|
-
WHERE rg.table_name = t.table_name
|
|
799
|
-
AND rg.privilege_type = 'INSERT'
|
|
800
|
-
),
|
|
801
|
-
'select', EXISTS (
|
|
802
|
-
SELECT 1
|
|
803
|
-
FROM information_schema.role_table_grants rg
|
|
804
|
-
WHERE rg.table_name = t.table_name
|
|
805
|
-
AND rg.privilege_type = 'SELECT'
|
|
806
|
-
),
|
|
807
|
-
'update', EXISTS (
|
|
808
|
-
SELECT 1
|
|
809
|
-
FROM information_schema.role_table_grants rg
|
|
810
|
-
WHERE rg.table_name = t.table_name
|
|
811
|
-
AND rg.privilege_type = 'UPDATE'
|
|
812
|
-
),
|
|
813
|
-
'delete', EXISTS (
|
|
814
|
-
SELECT 1
|
|
815
|
-
FROM information_schema.role_table_grants rg
|
|
816
|
-
WHERE rg.table_name = t.table_name
|
|
817
|
-
AND rg.privilege_type = 'DELETE'
|
|
818
|
-
)
|
|
819
|
-
) as privileges
|
|
820
|
-
, t.table_schema as schema, t.table_name as name
|
|
821
|
-
, cc.columns
|
|
822
|
-
, cc.table_oid as oid
|
|
823
|
-
, t.is_view
|
|
824
|
-
, t.view_definition
|
|
825
|
-
, array_to_json(vr.table_names) as parent_tables
|
|
826
|
-
, obj_description(cc.table_oid::regclass) as comment
|
|
827
|
-
FROM (
|
|
828
|
-
SELECT table_name,
|
|
829
|
-
table_schema,
|
|
830
|
-
is_view,
|
|
831
|
-
CASE WHEN is_view THEN pg_get_viewdef(format('%I.%I', table_schema, table_name)::REGCLASS, true) END as view_definition
|
|
832
|
-
FROM (
|
|
833
|
-
SELECT table_name, table_schema, table_type = 'VIEW' as is_view
|
|
834
|
-
FROM information_schema.tables
|
|
835
|
-
/* TODO - add support for materialized views
|
|
836
|
-
UNION ALL
|
|
837
|
-
SELECT table_name, table_schema
|
|
838
|
-
FROM (
|
|
839
|
-
SELECT relname as table_name, nspname as table_schema, true as is_view
|
|
840
|
-
FROM pg_catalog.pg_class AS _c
|
|
841
|
-
JOIN pg_catalog.pg_namespace AS _ns
|
|
842
|
-
ON _c.relnamespace = _ns.oid
|
|
843
|
-
WHERE relkind IN ( 'm' )
|
|
844
|
-
) materialized_views
|
|
845
|
-
*/
|
|
846
|
-
) tables_matviews
|
|
847
|
-
WHERE table_schema = ${asValue(schema)}
|
|
848
|
-
) t
|
|
849
|
-
INNER join (
|
|
850
|
-
SELECT ccc.table_oid, table_schema, table_name
|
|
851
|
-
, jsonb_agg((SELECT x FROM (
|
|
852
|
-
SELECT ccc.column_name as name,
|
|
853
|
-
ccc.data_type,
|
|
854
|
-
ccc.udt_name,
|
|
855
|
-
ccc.element_type,
|
|
856
|
-
ccc.element_udt_name,
|
|
857
|
-
ccc.is_pkey,
|
|
858
|
-
ccc.comment,
|
|
859
|
-
ccc.ordinal_position,
|
|
860
|
-
ccc.is_nullable = 'YES' as is_nullable,
|
|
861
|
-
ccc.references,
|
|
862
|
-
ccc.has_default,
|
|
863
|
-
ccc.column_default,
|
|
864
|
-
ccc.privileges
|
|
865
|
-
) as x) ORDER BY ccc.ordinal_position ) as columns
|
|
866
|
-
FROM (
|
|
867
|
-
SELECT c.table_schema, c.table_name, c.column_name, c.data_type, c.udt_name
|
|
868
|
-
, e.data_type as element_type
|
|
869
|
-
, e.udt_name as element_udt_name
|
|
870
|
-
, col_description(format('%I.%I', c.table_schema, c.table_name)::regclass::oid, c.ordinal_position) as comment
|
|
871
|
-
--, CASE WHEN fc.ftable IS NOT NULL THEN row_to_json((SELECT t FROM (SELECT fc.ftable, fc.fcols, fc.cols) t)) END as references
|
|
872
|
-
, fc.references
|
|
873
|
-
, c.is_identity = 'YES' OR EXISTS (
|
|
874
|
-
SELECT 1
|
|
875
|
-
FROM information_schema.table_constraints as tc
|
|
876
|
-
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
|
|
877
|
-
WHERE kcu.table_schema = c.table_schema AND kcu.table_name = c.table_name AND kcu.column_name = c.column_name AND tc.constraint_type IN ('PRIMARY KEY')
|
|
878
|
-
) as is_pkey
|
|
879
|
-
, c.ordinal_position
|
|
880
|
-
, COALESCE(c.column_default IS NOT NULL OR c.identity_generation = 'ALWAYS', false) as has_default
|
|
881
|
-
, c.column_default
|
|
882
|
-
, format('%I.%I', c.table_schema, c.table_name)::regclass::oid AS table_oid
|
|
883
|
-
, c.is_nullable
|
|
884
|
-
, cp.privileges
|
|
885
|
-
FROM information_schema.columns c
|
|
886
|
-
LEFT JOIN (SELECT * FROM information_schema.element_types ) e
|
|
887
|
-
ON ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
|
|
888
|
-
= (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier)
|
|
889
|
-
)
|
|
890
|
-
LEFT JOIN (
|
|
891
|
-
SELECT table_schema, table_name, column_name, json_agg(row_to_json((SELECT t FROM (SELECT cpp.privilege_type, cpp.is_grantable ) t))) as privileges
|
|
892
|
-
FROM information_schema.column_privileges cpp
|
|
893
|
-
GROUP BY table_schema, table_name, column_name
|
|
894
|
-
) cp
|
|
895
|
-
ON c.table_name = cp.table_name AND c.column_name = cp.column_name
|
|
896
|
-
LEFT JOIN (
|
|
897
|
-
--SELECT *
|
|
898
|
-
SELECT "table", ft.cols, jsonb_agg(row_to_json((SELECT t FROM (SELECT ftable, fcols, cols) t))) as references
|
|
899
|
-
FROM (
|
|
900
|
-
SELECT
|
|
901
|
-
(SELECT r.relname from pg_class r where r.oid = c.conrelid) as table,
|
|
902
|
-
(SELECT array_agg(attname::text) from pg_attribute
|
|
903
|
-
where attrelid = c.conrelid and ARRAY[attnum] <@ c.conkey) as cols,
|
|
904
|
-
(SELECT array_agg(attname::text) from pg_attribute
|
|
905
|
-
where attrelid = c.confrelid and ARRAY[attnum] <@ c.confkey) as fcols,
|
|
906
|
-
(SELECT r.relname from pg_class r where r.oid = c.confrelid) as ftable
|
|
907
|
-
FROM pg_constraint c
|
|
908
|
-
) ft
|
|
909
|
-
WHERE ft.table IS NOT NULL
|
|
910
|
-
AND ft.ftable IS NOT NULL
|
|
911
|
-
-- c.confrelid = 'users'::regclass::oid
|
|
912
|
-
GROUP BY "table", cols
|
|
913
|
-
) fc
|
|
914
|
-
ON fc.table = c.table_name
|
|
915
|
-
AND c.column_name::text = ANY(fc.cols)
|
|
916
|
-
) ccc
|
|
917
|
-
GROUP BY table_oid, table_schema, table_name
|
|
918
|
-
) cc
|
|
919
|
-
ON t.table_name = cc.table_name
|
|
920
|
-
AND t.table_schema = cc.table_schema
|
|
921
|
-
LEFT JOIN (
|
|
922
|
-
SELECT cl_r.relname as view_name, array_agg(DISTINCT cl_d.relname) AS table_names
|
|
923
|
-
FROM pg_rewrite AS r
|
|
924
|
-
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
|
|
925
|
-
JOIN pg_depend AS d ON r.oid=d.objid
|
|
926
|
-
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
|
|
927
|
-
WHERE cl_d.relkind IN ('r','v')
|
|
928
|
-
AND cl_d.relname <> cl_r.relname
|
|
929
|
-
GROUP BY cl_r.relname
|
|
930
|
-
) vr
|
|
931
|
-
ON t.table_name = vr.view_name
|
|
932
|
-
GROUP BY t.table_schema, t.table_name, t.is_view, t.view_definition, vr.table_names , cc.table_oid, cc.columns
|
|
933
|
-
ORDER BY schema, name
|
|
934
|
-
|
|
935
|
-
`;
|
|
936
|
-
// console.log(pgp.as.format(query, { schema }), schema);
|
|
937
|
-
let result: TableSchema[] = await db.any(query, { schema });
|
|
938
|
-
|
|
939
|
-
result = await Promise.all(result
|
|
940
|
-
.map(async tbl => {
|
|
941
|
-
|
|
942
|
-
/** Get view reference cols (based on parent table) */
|
|
943
|
-
let viewFCols: Pick<TableSchemaColumn, "name" | "references">[] = [];
|
|
944
|
-
if(tbl.is_view){
|
|
945
|
-
try {
|
|
946
|
-
const view_definition = tbl.view_definition?.endsWith(";")? tbl.view_definition.slice(0, -1) : tbl.view_definition;
|
|
947
|
-
const { fields } = await runSQL(`SELECT * FROM \n ( ${view_definition!} \n) t LIMIT 0`, {}, {}, undefined) as SQLResult<undefined>;
|
|
948
|
-
const ftables = result.filter(r => fields.some(f => f.tableID === r.oid));
|
|
949
|
-
ftables.forEach(ft => {
|
|
950
|
-
const fFields = fields.filter(f => f.tableID === ft.oid);
|
|
951
|
-
const pkeys = ft.columns.filter(c => c.is_pkey);
|
|
952
|
-
const fFieldPK = fFields.filter(ff => pkeys.some(p => p.name === ff.columnName));
|
|
953
|
-
const refCols = pkeys.length && fFieldPK.length === pkeys.length? fFieldPK : fFields.filter(ff => !["json", "jsonb", "xml"].includes(ff.udt_name));
|
|
954
|
-
const _fcols: typeof viewFCols = refCols.map(ff => {
|
|
955
|
-
const d: Pick<TableSchemaColumn, "name" | "references"> = {
|
|
956
|
-
name: ff.columnName!,
|
|
957
|
-
references: [{
|
|
958
|
-
ftable: ft.name,
|
|
959
|
-
fcols: [ff.columnName!],
|
|
960
|
-
cols: [ff.name]
|
|
961
|
-
}]
|
|
962
|
-
}
|
|
963
|
-
return d;
|
|
964
|
-
})
|
|
965
|
-
viewFCols = [
|
|
966
|
-
...viewFCols,
|
|
967
|
-
..._fcols
|
|
968
|
-
];
|
|
969
|
-
});
|
|
970
|
-
} catch(err){
|
|
971
|
-
console.error(err);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
tbl.columns = tbl.columns.map(col => {
|
|
976
|
-
if (col.has_default) {
|
|
977
|
-
/** Hide pkey default value */
|
|
978
|
-
col.column_default = (col.udt_name !== "uuid" && !col.is_pkey && !col.column_default.startsWith("nextval(")) ? col.column_default : null;
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
const viewFCol = viewFCols?.find(fc => fc.name === col.name)
|
|
982
|
-
if(viewFCol){
|
|
983
|
-
col.references = viewFCol.references;
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
return col;
|
|
987
|
-
|
|
988
|
-
})//.slice(0).sort((a, b) => a.name.localeCompare(b.name))
|
|
989
|
-
// .sort((a, b) => a.ordinal_position - b.ordinal_position)
|
|
990
|
-
|
|
991
|
-
return tbl;
|
|
992
|
-
}));
|
|
993
|
-
|
|
994
|
-
return result;
|
|
995
|
-
}
|
|
996
791
|
|
|
997
792
|
export function isPlainObject(o: any): o is Record<string, any> {
|
|
998
793
|
return Object(o) === o && Object.getPrototypeOf(o) === Object.prototype;
|
package/package.json
CHANGED
package/tests/client/PID.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
8094
|