@supabase/postgrest-typegen 0.2.1 → 0.2.3
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/README.md +13 -5
- package/dist/collation.d.ts +2 -0
- package/dist/collation.js +15 -0
- package/dist/generation/index.d.ts +1 -1
- package/dist/generation/python.js +21 -15
- package/dist/generation/swift.js +9 -2
- package/dist/generation/typescript.d.ts +18 -7
- package/dist/generation/typescript.js +102 -79
- package/dist/introspection/index.js +6 -23
- package/dist/introspection/relationships.d.ts +4 -4
- package/dist/introspection/relationships.js +4 -11
- package/dist/introspection/sql/columns.sql.d.ts +2 -7
- package/dist/introspection/sql/columns.sql.js +23 -17
- package/dist/introspection/sql/common.d.ts +7 -15
- package/dist/introspection/sql/foreign_tables.sql.d.ts +2 -6
- package/dist/introspection/sql/foreign_tables.sql.js +0 -5
- package/dist/introspection/sql/functions.sql.d.ts +2 -5
- package/dist/introspection/sql/functions.sql.js +0 -33
- package/dist/introspection/sql/materialized_views.sql.d.ts +2 -4
- package/dist/introspection/sql/materialized_views.sql.js +0 -5
- package/dist/introspection/sql/primary_keys.sql.d.ts +2 -4
- package/dist/introspection/sql/primary_keys.sql.js +0 -4
- package/dist/introspection/sql/schemas.sql.d.ts +2 -6
- package/dist/introspection/sql/schemas.sql.js +2 -6
- package/dist/introspection/sql/table.sql.d.ts +2 -4
- package/dist/introspection/sql/table.sql.js +0 -5
- package/dist/introspection/sql/table_relationships.sql.d.ts +2 -2
- package/dist/introspection/sql/types.sql.d.ts +5 -5
- package/dist/introspection/sql/types.sql.js +6 -17
- package/dist/introspection/sql/views.sql.d.ts +2 -4
- package/dist/introspection/sql/views.sql.js +0 -5
- package/dist/introspection/sql/views_key_dependencies.sql.d.ts +2 -2
- package/dist/introspection/sql/views_key_dependencies.sql.js +64 -75
- package/dist/sort.js +32 -16
- package/dist/types.d.ts +4 -4
- package/dist/types.js +6 -4
- package/package.json +4 -4
- package/src/collation.ts +16 -0
- package/src/generation/index.ts +1 -0
- package/src/generation/python.ts +28 -15
- package/src/generation/swift.ts +9 -2
- package/src/generation/typescript.ts +161 -104
- package/src/introspection/index.ts +7 -32
- package/src/introspection/relationships.ts +10 -19
- package/src/introspection/sql/columns.sql.ts +25 -26
- package/src/introspection/sql/common.ts +7 -17
- package/src/introspection/sql/foreign_tables.sql.ts +2 -13
- package/src/introspection/sql/functions.sql.ts +2 -44
- package/src/introspection/sql/materialized_views.sql.ts +2 -11
- package/src/introspection/sql/primary_keys.sql.ts +2 -10
- package/src/introspection/sql/schemas.sql.ts +4 -14
- package/src/introspection/sql/table.sql.ts +2 -11
- package/src/introspection/sql/table_relationships.sql.ts +2 -4
- package/src/introspection/sql/types.sql.ts +6 -26
- package/src/introspection/sql/views.sql.ts +2 -11
- package/src/introspection/sql/views_key_dependencies.sql.ts +83 -77
- package/src/sort.ts +37 -18
- package/src/types.ts +6 -4
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_SYSTEM_SCHEMAS, filterByList } from "./sql/helpers.js";
|
|
2
1
|
import { TABLE_RELATIONSHIPS_SQL } from "./sql/table_relationships.sql.js";
|
|
3
2
|
import { VIEWS_KEY_DEPENDENCIES_SQL } from "./sql/views_key_dependencies.sql.js";
|
|
4
3
|
/**
|
|
@@ -13,12 +12,7 @@ export function expandViewRelationships(allTableM2oAndO2oRelationships, viewsKey
|
|
|
13
12
|
return allTableM2oAndO2oRelationships.flatMap((r) => {
|
|
14
13
|
const expandKeyDepCols = (colDeps) => {
|
|
15
14
|
const tableColumns = colDeps.map(({ table_column }) => table_column);
|
|
16
|
-
|
|
17
|
-
const cartesianProduct = (allEntries) => {
|
|
18
|
-
return allEntries.reduce((results, entries) => results
|
|
19
|
-
.map((result) => entries.map((entry) => result.concat(entry)))
|
|
20
|
-
.reduce((subResults, result) => subResults.concat(result), []), [[]]);
|
|
21
|
-
};
|
|
15
|
+
const cartesianProduct = (lists) => lists.reduce((combinations, entries) => combinations.flatMap((combination) => entries.map((entry) => [...combination, entry])), [[]]);
|
|
22
16
|
const viewColumnsPermutations = cartesianProduct(colDeps.map((cd) => cd.view_columns));
|
|
23
17
|
return viewColumnsPermutations.map((viewColumns) => ({
|
|
24
18
|
tableColumns,
|
|
@@ -72,12 +66,11 @@ export function expandViewRelationships(allTableM2oAndO2oRelationships, viewsKey
|
|
|
72
66
|
}
|
|
73
67
|
/**
|
|
74
68
|
* List all relationships (table↔table plus the view-derived ones) for the
|
|
75
|
-
*
|
|
76
|
-
*
|
|
69
|
+
* schemas selected by `schemaFilter`, the `IN (…)`/`NOT IN (…)` fragment
|
|
70
|
+
* `introspect()` builds with the system schemas excluded. Errors from the
|
|
77
71
|
* injected `Queryable` propagate by throwing — callers adapt.
|
|
78
72
|
*/
|
|
79
|
-
export async function listRelationships(db,
|
|
80
|
-
const schemaFilter = filterByList(includedSchemas, excludedSchemas, DEFAULT_SYSTEM_SCHEMAS);
|
|
73
|
+
export async function listRelationships(db, schemaFilter) {
|
|
81
74
|
const { rows: allTableM2oAndO2oRelationships } = await db.query(TABLE_RELATIONSHIPS_SQL({ schemaFilter }));
|
|
82
75
|
const { rows: viewsKeyDependencies } = await db.query(VIEWS_KEY_DEPENDENCIES_SQL({ schemaFilter }));
|
|
83
76
|
const allViewM2oAndO2oRelationships = expandViewRelationships(allTableM2oAndO2oRelationships, viewsKeyDependencies);
|
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const COLUMNS_SQL: (props:
|
|
3
|
-
tableIdFilter?: string;
|
|
4
|
-
tableIdentifierFilter?: string;
|
|
5
|
-
columnNameFilter?: string;
|
|
6
|
-
idsFilter?: string;
|
|
7
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const COLUMNS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const COLUMNS_SQL = (props) => /* SQL */ `
|
|
3
2
|
-- Adapted from information_schema.columns
|
|
4
3
|
|
|
@@ -36,30 +35,43 @@ SELECT
|
|
|
36
35
|
WHEN 'd' THEN 'BY DEFAULT'
|
|
37
36
|
ELSE NULL
|
|
38
37
|
END AS identity_generation,
|
|
39
|
-
|
|
38
|
+
-- 's' = stored, 'v' = virtual (PostgreSQL 18+); neither accepts writes.
|
|
39
|
+
a.attgenerated IN ('s', 'v') AS is_generated,
|
|
40
40
|
NOT (
|
|
41
41
|
a.attnotnull
|
|
42
42
|
OR t.typtype = 'd' AND t.typnotnull
|
|
43
43
|
) AS is_nullable,
|
|
44
44
|
(
|
|
45
45
|
c.relkind IN ('r', 'p')
|
|
46
|
-
-- Columns of views made writable by INSTEAD OF triggers
|
|
47
|
-
-- through PostgREST even though the view is
|
|
48
|
-
-- must not degrade to \`?: never\` in generated
|
|
49
|
-
--
|
|
50
|
-
--
|
|
51
|
-
--
|
|
52
|
-
--
|
|
46
|
+
-- Columns of views made writable by INSTEAD OF triggers or unconditional
|
|
47
|
+
-- INSTEAD rules can be written through PostgREST even though the view is
|
|
48
|
+
-- not auto-updatable, so they must not degrade to \`?: never\` in generated
|
|
49
|
+
-- Insert/Update types. pg_column_is_updatable cannot express this: it
|
|
50
|
+
-- requires the relation to support both UPDATE and DELETE, so a view with
|
|
51
|
+
-- only an INSTEAD OF INSERT or only an INSTEAD OF UPDATE trigger reports
|
|
52
|
+
-- every column as non-updatable regardless of include_triggers. Triggers
|
|
53
|
+
-- and rules rewrite whole rows, so their presence makes every column
|
|
54
|
+
-- writable (tgtype bits: 64 = INSTEAD, 4 = INSERT, 16 = UPDATE;
|
|
55
|
+
-- pg_rewrite ev_type: '2' = UPDATE, '3' = INSERT; ev_qual is '<>' for
|
|
56
|
+
-- unconditional rules, and only unconditional INSTEAD rules make a view
|
|
57
|
+
-- writable). Which events a view accepts is gated per view via
|
|
53
58
|
-- is_insert_enabled / is_update_enabled.
|
|
54
59
|
OR c.relkind IN ('v', 'f') AND (
|
|
55
|
-
pg_column_is_updatable(c.oid, a.attnum,
|
|
60
|
+
pg_column_is_updatable(c.oid, a.attnum, FALSE)
|
|
56
61
|
OR EXISTS (
|
|
57
62
|
SELECT 1 FROM pg_trigger tg
|
|
58
63
|
WHERE tg.tgrelid = c.oid
|
|
59
64
|
AND tg.tgtype & 64 <> 0
|
|
60
|
-
AND tg.tgtype &
|
|
65
|
+
AND tg.tgtype & 20 <> 0
|
|
61
66
|
AND NOT tg.tgisinternal
|
|
62
67
|
)
|
|
68
|
+
OR EXISTS (
|
|
69
|
+
SELECT 1 FROM pg_rewrite rw
|
|
70
|
+
WHERE rw.ev_class = c.oid
|
|
71
|
+
AND rw.is_instead
|
|
72
|
+
AND rw.ev_type IN ('2', '3')
|
|
73
|
+
AND rw.ev_qual :: text = '<>'
|
|
74
|
+
)
|
|
63
75
|
)
|
|
64
76
|
) AS is_updatable,
|
|
65
77
|
uniques.table_id IS NOT NULL AS is_unique,
|
|
@@ -118,10 +130,6 @@ FROM
|
|
|
118
130
|
) AS check_constraints ON check_constraints.table_id = c.oid AND check_constraints.ordinal_position = a.attnum
|
|
119
131
|
WHERE
|
|
120
132
|
${props.schemaFilter ? `nc.nspname ${props.schemaFilter} AND` : ""}
|
|
121
|
-
${props.idsFilter ? `(c.oid || '.' || a.attnum) ${props.idsFilter} AND` : ""}
|
|
122
|
-
${props.columnNameFilter ? `(c.relname || '.' || a.attname) ${props.columnNameFilter} AND` : ""}
|
|
123
|
-
${props.tableIdFilter ? `c.oid ${props.tableIdFilter} AND` : ""}
|
|
124
|
-
${props.tableIdentifierFilter ? `nc.nspname || '.' || c.relname ${props.tableIdentifierFilter} AND` : ""}
|
|
125
133
|
NOT pg_is_other_temp_schema(nc.oid)
|
|
126
134
|
AND a.attnum > 0
|
|
127
135
|
AND NOT a.attisdropped
|
|
@@ -134,6 +142,4 @@ WHERE
|
|
|
134
142
|
'SELECT, INSERT, UPDATE, REFERENCES'
|
|
135
143
|
)
|
|
136
144
|
)
|
|
137
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
138
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
139
145
|
`;
|
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
offset?: number;
|
|
9
|
-
};
|
|
10
|
-
export type SQLQueryPropsWithSchemaFilter = SQLQueryProps & {
|
|
11
|
-
schemaFilter?: string;
|
|
12
|
-
};
|
|
13
|
-
export type SQLQueryPropsWithSchemaFilterAndIdsFilter = SQLQueryProps & {
|
|
14
|
-
schemaFilter?: string;
|
|
15
|
-
idsFilter?: string;
|
|
1
|
+
/** The only option the introspection passes to its SQL builders. */
|
|
2
|
+
export type SchemaFilterProps = {
|
|
3
|
+
/**
|
|
4
|
+
* An `IN (…)` or `NOT IN (…)` fragment from `filterByList`, or the empty
|
|
5
|
+
* string for no filter.
|
|
6
|
+
*/
|
|
7
|
+
schemaFilter: string;
|
|
16
8
|
};
|
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const FOREIGN_TABLES_SQL: (props:
|
|
3
|
-
schemaFilter?: string;
|
|
4
|
-
idsFilter?: string;
|
|
5
|
-
tableIdentifierFilter?: string;
|
|
6
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const FOREIGN_TABLES_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const FOREIGN_TABLES_SQL = (props) => /* SQL */ `
|
|
3
2
|
SELECT
|
|
4
3
|
c.oid :: int8 AS id,
|
|
@@ -10,9 +9,5 @@ FROM
|
|
|
10
9
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
11
10
|
WHERE
|
|
12
11
|
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
|
|
13
|
-
${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ""}
|
|
14
|
-
${props.tableIdentifierFilter ? `(n.nspname || '.' || c.relname) ${props.tableIdentifierFilter} AND` : ""}
|
|
15
12
|
c.relkind = 'f'
|
|
16
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
17
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
18
13
|
`;
|
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const FUNCTIONS_SQL: (props:
|
|
3
|
-
nameFilter?: string;
|
|
4
|
-
args?: string[];
|
|
5
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const FUNCTIONS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// oxlint-disable typescript/restrict-template-expressions -- verbatim port of postgres-meta's
|
|
2
|
-
// builder, which interpolates the `args` string[] into the SQL (relying on Array#toString). The
|
|
3
|
-
// expression sits inside a template literal where a targeted disable comment would corrupt the SQL.
|
|
4
|
-
import { literal } from "./pg-format.js";
|
|
5
1
|
export const FUNCTIONS_SQL = (props) => /* SQL */ `
|
|
6
2
|
-- CTE with sane arg_modes, arg_names, and arg_types.
|
|
7
3
|
-- All three are always of the same length.
|
|
@@ -29,33 +25,6 @@ with functions as (
|
|
|
29
25
|
${props.schemaFilter ? `join pg_namespace n on p.pronamespace = n.oid` : ""}
|
|
30
26
|
where
|
|
31
27
|
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
|
|
32
|
-
${props.idsFilter ? `p.oid ${props.idsFilter} AND` : ""}
|
|
33
|
-
${props.nameFilter ? `p.proname ${props.nameFilter} AND` : ""}
|
|
34
|
-
${props.args === undefined
|
|
35
|
-
? ""
|
|
36
|
-
: props.args.length > 0
|
|
37
|
-
? `p.proargtypes::text = ${props.args.length
|
|
38
|
-
? `(
|
|
39
|
-
SELECT STRING_AGG(type_oid::text, ' ') FROM (
|
|
40
|
-
SELECT (
|
|
41
|
-
split_args.arr[
|
|
42
|
-
array_length(
|
|
43
|
-
split_args.arr,
|
|
44
|
-
1
|
|
45
|
-
)
|
|
46
|
-
]::regtype::oid
|
|
47
|
-
) AS type_oid FROM (
|
|
48
|
-
SELECT STRING_TO_ARRAY(
|
|
49
|
-
UNNEST(
|
|
50
|
-
ARRAY[${props.args}]
|
|
51
|
-
),
|
|
52
|
-
' '
|
|
53
|
-
) AS arr
|
|
54
|
-
) AS split_args
|
|
55
|
-
) args
|
|
56
|
-
)`
|
|
57
|
-
: "''"} AND`
|
|
58
|
-
: ""}
|
|
59
28
|
p.prokind = 'f'
|
|
60
29
|
)
|
|
61
30
|
select
|
|
@@ -143,6 +112,4 @@ from
|
|
|
143
112
|
group by
|
|
144
113
|
t1.oid
|
|
145
114
|
) f_args on f_args.oid = f.oid
|
|
146
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
147
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
148
115
|
`;
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const MATERIALIZED_VIEWS_SQL: (props:
|
|
3
|
-
materializedViewIdentifierFilter?: string;
|
|
4
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const MATERIALIZED_VIEWS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const MATERIALIZED_VIEWS_SQL = (props) => /* SQL */ `
|
|
3
2
|
select
|
|
4
3
|
c.oid::int8 as id,
|
|
@@ -11,9 +10,5 @@ from
|
|
|
11
10
|
join pg_namespace n on n.oid = c.relnamespace
|
|
12
11
|
where
|
|
13
12
|
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
|
|
14
|
-
${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ""}
|
|
15
|
-
${props.materializedViewIdentifierFilter ? `(n.nspname || '.' || c.relname) ${props.materializedViewIdentifierFilter} AND` : ""}
|
|
16
13
|
c.relkind = 'm'
|
|
17
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
18
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
19
14
|
`;
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const PRIMARY_KEYS_SQL: (props:
|
|
3
|
-
tableIdentifierFilter?: string;
|
|
4
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const PRIMARY_KEYS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const PRIMARY_KEYS_SQL = (props) => /* SQL */ `
|
|
3
2
|
SELECT
|
|
4
3
|
c.oid :: int8 AS table_id,
|
|
@@ -12,7 +11,6 @@ FROM
|
|
|
12
11
|
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey)
|
|
13
12
|
WHERE
|
|
14
13
|
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
|
|
15
|
-
${props.tableIdentifierFilter ? `n.nspname || '.' || c.relname ${props.tableIdentifierFilter} AND` : ""}
|
|
16
14
|
i.indisprimary
|
|
17
15
|
AND c.relkind IN ('r', 'p')
|
|
18
16
|
AND NOT pg_is_other_temp_schema(n.oid)
|
|
@@ -27,6 +25,4 @@ WHERE
|
|
|
27
25
|
ORDER BY
|
|
28
26
|
c.oid,
|
|
29
27
|
array_position(i.indkey, a.attnum)
|
|
30
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
31
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
32
28
|
`;
|
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const SCHEMAS_SQL: (props:
|
|
3
|
-
nameFilter?: string;
|
|
4
|
-
idsFilter?: string;
|
|
5
|
-
includeSystemSchemas?: boolean;
|
|
6
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const SCHEMAS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const SCHEMAS_SQL = (props) => /* SQL */ `
|
|
3
2
|
-- Adapted from information_schema.schemata
|
|
4
3
|
select
|
|
@@ -10,15 +9,12 @@ from
|
|
|
10
9
|
pg_roles u
|
|
11
10
|
where
|
|
12
11
|
n.nspowner = u.oid
|
|
13
|
-
${props.
|
|
14
|
-
|
|
15
|
-
${!props.includeSystemSchemas ? `and not pg_catalog.starts_with(n.nspname, 'pg_')` : ""}
|
|
12
|
+
${props.schemaFilter ? `and n.nspname ${props.schemaFilter}` : ""}
|
|
13
|
+
and not pg_catalog.starts_with(n.nspname, 'pg_')
|
|
16
14
|
and (
|
|
17
15
|
pg_has_role(n.nspowner, 'USAGE')
|
|
18
16
|
or has_schema_privilege(n.oid, 'CREATE, USAGE')
|
|
19
17
|
)
|
|
20
18
|
and not pg_catalog.starts_with(n.nspname, 'pg_temp_')
|
|
21
19
|
and not pg_catalog.starts_with(n.nspname, 'pg_toast_temp_')
|
|
22
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
23
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
24
20
|
`;
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const TABLES_SQL: (props:
|
|
3
|
-
tableIdentifierFilter?: string;
|
|
4
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const TABLES_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const TABLES_SQL = (props) => /* SQL */ `
|
|
3
2
|
SELECT
|
|
4
3
|
c.oid :: int8 AS id,
|
|
@@ -24,8 +23,6 @@ FROM
|
|
|
24
23
|
JOIN pg_class c ON nc.oid = c.relnamespace
|
|
25
24
|
WHERE
|
|
26
25
|
${props.schemaFilter ? `nc.nspname ${props.schemaFilter} AND` : ""}
|
|
27
|
-
${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ""}
|
|
28
|
-
${props.tableIdentifierFilter ? `nc.nspname || '.' || c.relname ${props.tableIdentifierFilter} AND` : ""}
|
|
29
26
|
c.relkind IN ('r', 'p')
|
|
30
27
|
AND NOT pg_is_other_temp_schema(nc.oid)
|
|
31
28
|
AND (
|
|
@@ -36,6 +33,4 @@ WHERE
|
|
|
36
33
|
)
|
|
37
34
|
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES')
|
|
38
35
|
)
|
|
39
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
40
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
41
36
|
`;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const TABLE_RELATIONSHIPS_SQL: (props:
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const TABLE_RELATIONSHIPS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Every type of every schema, including the row types of tables and views
|
|
3
|
+
* and the array types, since a column of any schema may use them.
|
|
4
|
+
*/
|
|
5
|
+
export declare const TYPES_SQL = "\nselect\n t.oid::int8 as id,\n t.typname as name,\n n.nspname as schema,\n format_type (t.oid, null) as format,\n coalesce(t_enums.enums, '[]') as enums,\n coalesce(t_attributes.attributes, '[]') as attributes,\n obj_description (t.oid, 'pg_type') as comment,\n nullif(t.typrelid::int8, 0) as type_relation_id\nfrom\n pg_type t\n left join pg_namespace n on n.oid = t.typnamespace\n left join (\n select\n enumtypid,\n jsonb_agg(enumlabel order by enumsortorder) as enums\n from\n pg_enum\n group by\n enumtypid\n ) as t_enums on t_enums.enumtypid = t.oid\n left join (\n select\n oid,\n jsonb_agg(\n jsonb_build_object('name', a.attname, 'type_id', a.atttypid::int8)\n order by a.attnum asc\n ) as attributes\n from\n pg_class c\n join pg_attribute a on a.attrelid = c.oid\n where\n c.relkind = 'c' and not a.attisdropped\n group by\n c.oid\n ) as t_attributes on t_attributes.oid = t.typrelid\n where\n (\n t.typrelid = 0\n or (\n select\n c.relkind in ('c', 'r', 'v', 'm', 'p', 'f')\n from\n pg_class c\n where\n c.oid = t.typrelid\n )\n )\n";
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Every type of every schema, including the row types of tables and views
|
|
3
|
+
* and the array types, since a column of any schema may use them.
|
|
4
|
+
*/
|
|
5
|
+
export const TYPES_SQL = /* SQL */ `
|
|
3
6
|
select
|
|
4
7
|
t.oid::int8 as id,
|
|
5
8
|
t.typname as name,
|
|
@@ -41,25 +44,11 @@ from
|
|
|
41
44
|
t.typrelid = 0
|
|
42
45
|
or (
|
|
43
46
|
select
|
|
44
|
-
c.relkind
|
|
47
|
+
c.relkind in ('c', 'r', 'v', 'm', 'p', 'f')
|
|
45
48
|
from
|
|
46
49
|
pg_class c
|
|
47
50
|
where
|
|
48
51
|
c.oid = t.typrelid
|
|
49
52
|
)
|
|
50
53
|
)
|
|
51
|
-
${!props.includeArrayTypes
|
|
52
|
-
? `and not exists (
|
|
53
|
-
select
|
|
54
|
-
from
|
|
55
|
-
pg_type el
|
|
56
|
-
where
|
|
57
|
-
el.oid = t.typelem
|
|
58
|
-
and el.typarray = t.oid
|
|
59
|
-
)`
|
|
60
|
-
: ""}
|
|
61
|
-
${props.schemaFilter ? `and n.nspname ${props.schemaFilter}` : ""}
|
|
62
|
-
${props.idsFilter ? `and t.oid ${props.idsFilter}` : ""}
|
|
63
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
64
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
65
54
|
`;
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const VIEWS_SQL: (props:
|
|
3
|
-
viewIdentifierFilter?: string;
|
|
4
|
-
}) => string;
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const VIEWS_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { literal } from "./pg-format.js";
|
|
2
1
|
export const VIEWS_SQL = (props) => /* SQL */ `
|
|
3
2
|
SELECT
|
|
4
3
|
c.oid :: int8 AS id,
|
|
@@ -18,9 +17,5 @@ FROM
|
|
|
18
17
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
19
18
|
WHERE
|
|
20
19
|
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
|
|
21
|
-
${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ""}
|
|
22
|
-
${props.viewIdentifierFilter ? `(n.nspname || '.' || c.relname) ${props.viewIdentifierFilter} AND` : ""}
|
|
23
20
|
c.relkind = 'v'
|
|
24
|
-
${props.limit ? `limit ${literal(props.limit)}` : ""}
|
|
25
|
-
${props.offset ? `offset ${literal(props.offset)}` : ""}
|
|
26
21
|
`;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const VIEWS_KEY_DEPENDENCIES_SQL: (props:
|
|
1
|
+
import type { SchemaFilterProps } from "./common.ts";
|
|
2
|
+
export declare const VIEWS_KEY_DEPENDENCIES_SQL: (props: SchemaFilterProps) => string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { literal } from "./pg-format.js";
|
|
1
2
|
export const VIEWS_KEY_DEPENDENCIES_SQL = (props) => /* SQL */ `
|
|
2
3
|
-- Adapted from
|
|
3
4
|
-- https://github.com/PostgREST/postgrest/blob/f9f0f79fa914ac00c11fbf7f4c558e14821e67e2/src/PostgREST/SchemaCache.hs#L820
|
|
@@ -43,81 +44,7 @@ views as (
|
|
|
43
44
|
transform_json as (
|
|
44
45
|
select
|
|
45
46
|
view_id, view_schema, view_name,
|
|
46
|
-
|
|
47
|
-
-- to allow simple diffs, with less whitespace noise
|
|
48
|
-
replace(
|
|
49
|
-
replace(
|
|
50
|
-
replace(
|
|
51
|
-
replace(
|
|
52
|
-
replace(
|
|
53
|
-
replace(
|
|
54
|
-
replace(
|
|
55
|
-
regexp_replace(
|
|
56
|
-
replace(
|
|
57
|
-
replace(
|
|
58
|
-
replace(
|
|
59
|
-
replace(
|
|
60
|
-
replace(
|
|
61
|
-
replace(
|
|
62
|
-
replace(
|
|
63
|
-
replace(
|
|
64
|
-
replace(
|
|
65
|
-
replace(
|
|
66
|
-
replace(
|
|
67
|
-
view_definition::text,
|
|
68
|
-
-- This conversion to json is heavily optimized for performance.
|
|
69
|
-
-- The general idea is to use as few regexp_replace() calls as possible.
|
|
70
|
-
-- Simple replace() is a lot faster, so we jump through some hoops
|
|
71
|
-
-- to be able to use regexp_replace() only once.
|
|
72
|
-
-- This has been tested against a huge schema with 250+ different views.
|
|
73
|
-
-- The unit tests do NOT reflect all possible inputs. Be careful when changing this!
|
|
74
|
-
-- -----------------------------------------------
|
|
75
|
-
-- pattern | replacement | flags
|
|
76
|
-
-- -----------------------------------------------
|
|
77
|
-
-- <> in pg_node_tree is the same as null in JSON, but due to very poor performance of json_typeof
|
|
78
|
-
-- we need to make this an empty array here to prevent json_array_elements from throwing an error
|
|
79
|
-
-- when the targetList is null.
|
|
80
|
-
-- We'll need to put it first, to make the node protection below work for node lists that start with
|
|
81
|
-
-- null: (<> ..., too. This is the case for coldefexprs, when the first column does not have a default value.
|
|
82
|
-
'<>' , '()'
|
|
83
|
-
-- , is not part of the pg_node_tree format, but used in the regex.
|
|
84
|
-
-- This removes all , that might be part of column names.
|
|
85
|
-
), ',' , ''
|
|
86
|
-
-- The same applies for { and }, although those are used a lot in pg_node_tree.
|
|
87
|
-
-- We remove the escaped ones, which might be part of column names again.
|
|
88
|
-
), E'\\\\{' , ''
|
|
89
|
-
), E'\\\\}' , ''
|
|
90
|
-
-- The fields we need are formatted as json manually to protect them from the regex.
|
|
91
|
-
), ' :targetList ' , ',"targetList":'
|
|
92
|
-
), ' :resno ' , ',"resno":'
|
|
93
|
-
), ' :resorigtbl ' , ',"resorigtbl":'
|
|
94
|
-
), ' :resorigcol ' , ',"resorigcol":'
|
|
95
|
-
-- Make the regex also match the node type, e.g. \`{QUERY ...\`, to remove it in one pass.
|
|
96
|
-
), '{' , '{ :'
|
|
97
|
-
-- Protect node lists, which start with \`({\` or \`((\` from the greedy regex.
|
|
98
|
-
-- The extra \`{\` is removed again later.
|
|
99
|
-
), '((' , '{(('
|
|
100
|
-
), '({' , '{({'
|
|
101
|
-
-- This regex removes all unused fields to avoid the need to format all of them correctly.
|
|
102
|
-
-- This leads to a smaller json result as well.
|
|
103
|
-
-- Removal stops at \`,\` for used fields (see above) and \`}\` for the end of the current node.
|
|
104
|
-
-- Nesting can't be parsed correctly with a regex, so we stop at \`{\` as well and
|
|
105
|
-
-- add an empty key for the followig node.
|
|
106
|
-
), ' :[^}{,]+' , ',"":' , 'g'
|
|
107
|
-
-- For performance, the regex also added those empty keys when hitting a \`,\` or \`}\`.
|
|
108
|
-
-- Those are removed next.
|
|
109
|
-
), ',"":}' , '}'
|
|
110
|
-
), ',"":,' , ','
|
|
111
|
-
-- This reverses the "node list protection" from above.
|
|
112
|
-
), '{(' , '('
|
|
113
|
-
-- Every key above has been added with a \`,\` so far. The first key in an object doesn't need it.
|
|
114
|
-
), '{,' , '{'
|
|
115
|
-
-- pg_node_tree has \`()\` around lists, but JSON uses \`[]\`
|
|
116
|
-
), '(' , '['
|
|
117
|
-
), ')' , ']'
|
|
118
|
-
-- pg_node_tree has \` \` between list items, but JSON uses \`,\`
|
|
119
|
-
), ' ' , ','
|
|
120
|
-
)::json as view_definition
|
|
47
|
+
${nodeTreeToJson("view_definition::text")}::json as view_definition
|
|
121
48
|
from views
|
|
122
49
|
),
|
|
123
50
|
target_entries as(
|
|
@@ -193,3 +120,65 @@ group by sch.nspname, tbl.relname, rep.view_schema, rep.view_name, pks_fks.conn
|
|
|
193
120
|
-- make sure we only return key for which all columns are referenced in the view - no partial PKs or FKs
|
|
194
121
|
having ncol = array_length(array_agg(row(col.attname, view_columns) order by pks_fks.ord), 1)
|
|
195
122
|
`;
|
|
123
|
+
/**
|
|
124
|
+
* The rewrites, innermost first, that turn the `pg_node_tree` text of a view
|
|
125
|
+
* definition into JSON exposing only `targetList`, `resno`, `resorigtbl` and
|
|
126
|
+
* `resorigcol`. Ported from PostgREST's schema cache query, which keeps them
|
|
127
|
+
* as plain `replace` calls with a single `regexp_replace` for speed; the
|
|
128
|
+
* order matters, and the comments carry PostgREST's reasoning.
|
|
129
|
+
*/
|
|
130
|
+
const NODE_TREE_TO_JSON_REWRITES = [
|
|
131
|
+
// `<>` is pg_node_tree's null. json_typeof is too slow to special-case it,
|
|
132
|
+
// so it becomes an empty list, which also keeps the node protection below
|
|
133
|
+
// working for lists that start with null, such as coldefexprs whose first
|
|
134
|
+
// column has no default.
|
|
135
|
+
{ pattern: "<>", replacement: "()" },
|
|
136
|
+
// `,`, `{` and `}` are used by the JSON conversion; drop the ones that may
|
|
137
|
+
// be part of column names (`{` and `}` are escaped there).
|
|
138
|
+
{ pattern: ",", replacement: "" },
|
|
139
|
+
{ pattern: "\\{", replacement: "" },
|
|
140
|
+
{ pattern: "\\}", replacement: "" },
|
|
141
|
+
// Format the fields we need as JSON keys so the regex below leaves them.
|
|
142
|
+
{ pattern: " :targetList ", replacement: ',"targetList":' },
|
|
143
|
+
{ pattern: " :resno ", replacement: ',"resno":' },
|
|
144
|
+
{ pattern: " :resorigtbl ", replacement: ',"resorigtbl":' },
|
|
145
|
+
{ pattern: " :resorigcol ", replacement: ',"resorigcol":' },
|
|
146
|
+
// Make the regex also match the node type, e.g. `{QUERY ...`.
|
|
147
|
+
{ pattern: "{", replacement: "{ :" },
|
|
148
|
+
// Protect node lists, which start with `({` or `((`, from the greedy regex.
|
|
149
|
+
// The extra `{` is removed again below.
|
|
150
|
+
{ pattern: "((", replacement: "{((" },
|
|
151
|
+
{ pattern: "({", replacement: "{({" },
|
|
152
|
+
// Remove every unused field. Removal stops at `,` for the kept fields, at
|
|
153
|
+
// `}` for the end of the node and at `{` for a nested node, leaving an empty
|
|
154
|
+
// key for the following node.
|
|
155
|
+
{ pattern: " :[^}{,]+", replacement: ',"":', isRegex: true },
|
|
156
|
+
// The regex also added those empty keys before `}` and `,`.
|
|
157
|
+
{ pattern: ',"":}', replacement: "}" },
|
|
158
|
+
{ pattern: ',"":,', replacement: "," },
|
|
159
|
+
// Undo the node list protection.
|
|
160
|
+
{ pattern: "{(", replacement: "(" },
|
|
161
|
+
// Every key was added with a leading `,`; the first key of an object does
|
|
162
|
+
// not need it.
|
|
163
|
+
{ pattern: "{,", replacement: "{" },
|
|
164
|
+
// pg_node_tree lists use `( )` with space separated items; JSON uses `[ ]`
|
|
165
|
+
// with commas.
|
|
166
|
+
{ pattern: "(", replacement: "[" },
|
|
167
|
+
{ pattern: ")", replacement: "]" },
|
|
168
|
+
{ pattern: " ", replacement: "," },
|
|
169
|
+
];
|
|
170
|
+
/**
|
|
171
|
+
* Render {@link NODE_TREE_TO_JSON_REWRITES} applied to `expression` as nested
|
|
172
|
+
* SQL calls: the opening calls stacked, then each rewrite's arguments on the
|
|
173
|
+
* line that closes it.
|
|
174
|
+
*/
|
|
175
|
+
function nodeTreeToJson(expression) {
|
|
176
|
+
const lines = NODE_TREE_TO_JSON_REWRITES.toReversed().map((rewrite) => ` ${rewrite.isRegex ? "regexp_replace" : "replace"}(`);
|
|
177
|
+
lines.push(` ${expression},`);
|
|
178
|
+
NODE_TREE_TO_JSON_REWRITES.forEach((rewrite, index) => {
|
|
179
|
+
const flags = rewrite.isRegex ? ", 'g'" : "";
|
|
180
|
+
const closing = index === NODE_TREE_TO_JSON_REWRITES.length - 1 ? ")" : "),";
|
|
181
|
+
lines.push(` ${literal(rewrite.pattern)}, ${literal(rewrite.replacement)}${flags}${closing}`);
|
|
182
|
+
});
|
|
183
|
+
return lines.join("\n");
|
|
184
|
+
}
|