File tree 4 files changed +48
-0
lines changed
4 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ export default async (fastify: FastifyInstance) => {
24
24
const { data : views , error : viewsError } = await pgMeta . views . list ( )
25
25
const { data : materializedViews , error : materializedViewsError } =
26
26
await pgMeta . materializedViews . list ( { includeColumns : true } )
27
+ const { data : relationships , error : relationshipsError } = await pgMeta . relationships . list ( )
27
28
const { data : functions , error : functionsError } = await pgMeta . functions . list ( )
28
29
const { data : types , error : typesError } = await pgMeta . types . list ( {
29
30
includeArrayTypes : true ,
@@ -54,6 +55,11 @@ export default async (fastify: FastifyInstance) => {
54
55
reply . code ( 500 )
55
56
return { error : materializedViewsError . message }
56
57
}
58
+ if ( relationshipsError ) {
59
+ request . log . error ( { error : relationshipsError , request : extractRequestForLogging ( request ) } )
60
+ reply . code ( 500 )
61
+ return { error : relationshipsError . message }
62
+ }
57
63
if ( functionsError ) {
58
64
request . log . error ( { error : functionsError , request : extractRequestForLogging ( request ) } )
59
65
reply . code ( 500 )
@@ -74,6 +80,7 @@ export default async (fastify: FastifyInstance) => {
74
80
tables,
75
81
views,
76
82
materializedViews,
83
+ relationships,
77
84
functions : functions . filter (
78
85
( { return_type } ) => ! [ 'trigger' , 'event_trigger' ] . includes ( return_type )
79
86
) ,
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ if (EXPORT_DOCS) {
41
41
const { data : views , error : viewsError } = await pgMeta . views . list ( )
42
42
const { data : materializedViews , error : materializedViewsError } =
43
43
await pgMeta . materializedViews . list ( { includeColumns : true } )
44
+ const { data : relationships , error : relationshipsError } = await pgMeta . relationships . list ( )
44
45
const { data : functions , error : functionsError } = await pgMeta . functions . list ( )
45
46
const { data : types , error : typesError } = await pgMeta . types . list ( {
46
47
includeArrayTypes : true ,
@@ -60,6 +61,9 @@ if (EXPORT_DOCS) {
60
61
if ( materializedViewsError ) {
61
62
throw new Error ( materializedViewsError . message )
62
63
}
64
+ if ( relationshipsError ) {
65
+ throw new Error ( relationshipsError . message )
66
+ }
63
67
if ( functionsError ) {
64
68
throw new Error ( functionsError . message )
65
69
}
@@ -77,6 +81,7 @@ if (EXPORT_DOCS) {
77
81
tables,
78
82
views,
79
83
materializedViews,
84
+ relationships,
80
85
functions : functions . filter (
81
86
( { return_type } ) => ! [ 'trigger' , 'event_trigger' ] . includes ( return_type )
82
87
) ,
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import prettier from 'prettier'
2
2
import type {
3
3
PostgresFunction ,
4
4
PostgresMaterializedView ,
5
+ PostgresRelationship ,
5
6
PostgresSchema ,
6
7
PostgresTable ,
7
8
PostgresType ,
@@ -13,6 +14,7 @@ export const apply = ({
13
14
tables,
14
15
views,
15
16
materializedViews,
17
+ relationships,
16
18
functions,
17
19
types,
18
20
arrayTypes,
@@ -21,6 +23,7 @@ export const apply = ({
21
23
tables : ( PostgresTable & { columns : unknown [ ] } ) [ ]
22
24
views : ( PostgresView & { columns : unknown [ ] } ) [ ]
23
25
materializedViews : ( PostgresMaterializedView & { columns : unknown [ ] } ) [ ]
26
+ relationships : PostgresRelationship [ ]
24
27
functions : PostgresFunction [ ]
25
28
types : PostgresType [ ]
26
29
arrayTypes : PostgresType [ ]
@@ -145,6 +148,18 @@ export interface Database {
145
148
return output
146
149
} ) }
147
150
}
151
+ Relationships: [
152
+ ${ relationships
153
+ . filter ( ( relationship ) => relationship . schema === table . schema && relationship . relation === table . name )
154
+ . map ( ( relationship ) => `{
155
+ foreignKeyName: ${ JSON . stringify ( relationship . foreign_key_name ) }
156
+ columns: ${ JSON . stringify ( relationship . columns ) }
157
+ referencedSchema: ${ JSON . stringify ( relationship . referenced_schema ) }
158
+ referencedRelation: ${ JSON . stringify ( relationship . referenced_relation ) }
159
+ referencedColumns: ${ JSON . stringify ( relationship . referenced_columns ) }
160
+ }` )
161
+ }
162
+ ]
148
163
}`
149
164
)
150
165
}
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ test('typegen', async () => {
30
30
id?: number
31
31
name?: string
32
32
}
33
+ Relationships: []
33
34
}
34
35
memes: {
35
36
Row: {
@@ -56,6 +57,15 @@ test('typegen', async () => {
56
57
name?: string
57
58
status?: Database["public"]["Enums"]["meme_status"] | null
58
59
}
60
+ Relationships: [
61
+ {
62
+ foreignKeyName: "memes_category_fkey"
63
+ columns: ["category"]
64
+ referencedSchema: "public"
65
+ referencedRelation: "category"
66
+ referencedColumns: ["id"]
67
+ }
68
+ ]
59
69
}
60
70
todos: {
61
71
Row: {
@@ -74,6 +84,15 @@ test('typegen', async () => {
74
84
id?: number
75
85
"user-id"?: number
76
86
}
87
+ Relationships: [
88
+ {
89
+ foreignKeyName: "todos_user-id_fkey"
90
+ columns: ["user-id"]
91
+ referencedSchema: "public"
92
+ referencedRelation: "users"
93
+ referencedColumns: ["id"]
94
+ }
95
+ ]
77
96
}
78
97
users: {
79
98
Row: {
@@ -91,6 +110,7 @@ test('typegen', async () => {
91
110
name?: string | null
92
111
status?: Database["public"]["Enums"]["user_status"] | null
93
112
}
113
+ Relationships: []
94
114
}
95
115
users_audit: {
96
116
Row: {
@@ -111,6 +131,7 @@ test('typegen', async () => {
111
131
previous_value?: Json | null
112
132
user_id?: number | null
113
133
}
134
+ Relationships: []
114
135
}
115
136
}
116
137
Views: {
You can’t perform that action at this time.
0 commit comments