Skip to content

Commit 6ad549c

Browse files
committed
feat: ✨ Adds "relationships" to the /tables endpoint
This change provides an array of relationships in both directions, the "source" and the "target". Each table will include this array, so that we can map relationships in both directions
1 parent c280001 commit 6ad549c

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

dist/lib/sql/tables/list.sql

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ WHERE
8787
AND pg_class.relnamespace = pg_namespace.oid
8888
AND pg_attribute.attrelid = pg_class.oid
8989
AND pg_attribute.attnum = any(pg_index.indkey)
90+
),
91+
relationships as (
92+
select
93+
(tc.table_schema || '.' || (tc.table_name)) as source_table_id,
94+
tc.table_schema as source_schema,
95+
tc.table_name as source_table_name,
96+
kcu.column_name as source_column_name,
97+
(ccu.table_schema || '.' || (ccu.table_name)) as target_table_id,
98+
ccu.table_schema AS target_table_schema,
99+
ccu.table_name AS target_table_name,
100+
ccu.column_name AS target_column_name,
101+
tc.constraint_name
102+
FROM
103+
information_schema.table_constraints AS tc
104+
JOIN information_schema.key_column_usage AS kcu USING (constraint_schema, constraint_name)
105+
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
106+
where
107+
tc.constraint_type = 'FOREIGN KEY'
90108
)
91109
SELECT
92110
*,
@@ -135,7 +153,24 @@ SELECT
135153
WHERE
136154
pk_list.table_id = tables.table_id
137155
) primary_keys
138-
),
139-
'[]'
140-
) AS primary_keys
156+
),
157+
'[]'
158+
) AS primary_keys,
159+
COALESCE(
160+
(
161+
SELECT
162+
array_to_json(array_agg(row_to_json(relationships)))
163+
FROM
164+
(
165+
SELECT
166+
*
167+
FROM
168+
relationships
169+
WHERE
170+
relationships.source_table_id = tables.table_id
171+
OR relationships.target_table_id = tables.table_id
172+
) relationships
173+
),
174+
'[]'
175+
) AS relationships
141176
FROM tables

src/lib/interfaces/tables.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ export namespace Tables {
99
is_typed: boolean
1010
bytes: number
1111
size: string
12+
relationships: Relationship[]
13+
}
14+
15+
export interface Relationship {
16+
source_table_id: string
17+
source_schema: string
18+
source_table_name: string
19+
source_column_name: string
20+
target_table_id: string
21+
target_table_schema: string
22+
target_table_name: string
23+
target_column_name: string
24+
constraint_name: string
1225
}
1326

1427
}

src/lib/sql/tables/list.sql

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ WHERE
8787
AND pg_class.relnamespace = pg_namespace.oid
8888
AND pg_attribute.attrelid = pg_class.oid
8989
AND pg_attribute.attnum = any(pg_index.indkey)
90+
),
91+
relationships as (
92+
select
93+
(tc.table_schema || '.' || (tc.table_name)) as source_table_id,
94+
tc.table_schema as source_schema,
95+
tc.table_name as source_table_name,
96+
kcu.column_name as source_column_name,
97+
(ccu.table_schema || '.' || (ccu.table_name)) as target_table_id,
98+
ccu.table_schema AS target_table_schema,
99+
ccu.table_name AS target_table_name,
100+
ccu.column_name AS target_column_name,
101+
tc.constraint_name
102+
FROM
103+
information_schema.table_constraints AS tc
104+
JOIN information_schema.key_column_usage AS kcu USING (constraint_schema, constraint_name)
105+
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
106+
where
107+
tc.constraint_type = 'FOREIGN KEY'
90108
)
91109
SELECT
92110
*,
@@ -135,7 +153,24 @@ SELECT
135153
WHERE
136154
pk_list.table_id = tables.table_id
137155
) primary_keys
138-
),
139-
'[]'
140-
) AS primary_keys
156+
),
157+
'[]'
158+
) AS primary_keys,
159+
COALESCE(
160+
(
161+
SELECT
162+
array_to_json(array_agg(row_to_json(relationships)))
163+
FROM
164+
(
165+
SELECT
166+
*
167+
FROM
168+
relationships
169+
WHERE
170+
relationships.source_table_id = tables.table_id
171+
OR relationships.target_table_id = tables.table_id
172+
) relationships
173+
),
174+
'[]'
175+
) AS relationships
141176
FROM tables

test/integration/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ describe('/tables', async () => {
145145
const datum = tables.data.find((x) => x.table_id == 'public.users')
146146
assert.equal(datum.grants.length > 0, true)
147147
})
148+
it('should return the relationships', async () => {
149+
const tables = await axios.get(`${URL}/tables`)
150+
const datum = tables.data.find((x) => x.table_id == 'public.users')
151+
const relationships = datum.relationships
152+
const relationship = relationships.find(x => x.source_table_id == 'public.todos')
153+
assert.equal(relationships.length > 0, true)
154+
assert.equal(true, relationship.target_table_id == 'public.users')
155+
})
148156

149157
it('GET with system tables', async () => {
150158
const res = await axios.get(`${URL}/tables?includeSystemSchemas=true`)

0 commit comments

Comments
 (0)