File tree Expand file tree Collapse file tree 4 files changed +97
-6
lines changed Expand file tree Collapse file tree 4 files changed +97
-6
lines changed Original file line number Diff line number Diff line change 87
87
AND pg_class .relnamespace = pg_namespace .oid
88
88
AND pg_attribute .attrelid = pg_class .oid
89
89
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'
90
108
)
91
109
SELECT
92
110
* ,
@@ -135,7 +153,24 @@ SELECT
135
153
WHERE
136
154
pk_list .table_id = tables .table_id
137
155
) 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
141
176
FROM tables
Original file line number Diff line number Diff line change @@ -9,6 +9,19 @@ export namespace Tables {
9
9
is_typed : boolean
10
10
bytes : number
11
11
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
12
25
}
13
26
14
27
}
Original file line number Diff line number Diff line change 87
87
AND pg_class .relnamespace = pg_namespace .oid
88
88
AND pg_attribute .attrelid = pg_class .oid
89
89
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'
90
108
)
91
109
SELECT
92
110
* ,
@@ -135,7 +153,24 @@ SELECT
135
153
WHERE
136
154
pk_list .table_id = tables .table_id
137
155
) 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
141
176
FROM tables
Original file line number Diff line number Diff line change @@ -145,6 +145,14 @@ describe('/tables', async () => {
145
145
const datum = tables . data . find ( ( x ) => x . table_id == 'public.users' )
146
146
assert . equal ( datum . grants . length > 0 , true )
147
147
} )
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
+ } )
148
156
149
157
it ( 'GET with system tables' , async ( ) => {
150
158
const res = await axios . get ( `${ URL } /tables?includeSystemSchemas=true` )
You can’t perform that action at this time.
0 commit comments