Skip to content

Commit ca7a140

Browse files
committed
feat(typegen): include table relationships
1 parent 5cfee6f commit ca7a140

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/server/routes/generators/typescript.ts

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default async (fastify: FastifyInstance) => {
2424
const { data: views, error: viewsError } = await pgMeta.views.list()
2525
const { data: materializedViews, error: materializedViewsError } =
2626
await pgMeta.materializedViews.list({ includeColumns: true })
27+
const { data: relationships, error: relationshipsError } = await pgMeta.relationships.list()
2728
const { data: functions, error: functionsError } = await pgMeta.functions.list()
2829
const { data: types, error: typesError } = await pgMeta.types.list({
2930
includeArrayTypes: true,
@@ -54,6 +55,11 @@ export default async (fastify: FastifyInstance) => {
5455
reply.code(500)
5556
return { error: materializedViewsError.message }
5657
}
58+
if (relationshipsError) {
59+
request.log.error({ error: relationshipsError, request: extractRequestForLogging(request) })
60+
reply.code(500)
61+
return { error: relationshipsError.message }
62+
}
5763
if (functionsError) {
5864
request.log.error({ error: functionsError, request: extractRequestForLogging(request) })
5965
reply.code(500)
@@ -74,6 +80,7 @@ export default async (fastify: FastifyInstance) => {
7480
tables,
7581
views,
7682
materializedViews,
83+
relationships,
7784
functions: functions.filter(
7885
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
7986
),

src/server/server.ts

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ if (EXPORT_DOCS) {
4141
const { data: views, error: viewsError } = await pgMeta.views.list()
4242
const { data: materializedViews, error: materializedViewsError } =
4343
await pgMeta.materializedViews.list({ includeColumns: true })
44+
const { data: relationships, error: relationshipsError } = await pgMeta.relationships.list()
4445
const { data: functions, error: functionsError } = await pgMeta.functions.list()
4546
const { data: types, error: typesError } = await pgMeta.types.list({
4647
includeArrayTypes: true,
@@ -60,6 +61,9 @@ if (EXPORT_DOCS) {
6061
if (materializedViewsError) {
6162
throw new Error(materializedViewsError.message)
6263
}
64+
if (relationshipsError) {
65+
throw new Error(relationshipsError.message)
66+
}
6367
if (functionsError) {
6468
throw new Error(functionsError.message)
6569
}
@@ -77,6 +81,7 @@ if (EXPORT_DOCS) {
7781
tables,
7882
views,
7983
materializedViews,
84+
relationships,
8085
functions: functions.filter(
8186
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
8287
),

src/server/templates/typescript.ts

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import prettier from 'prettier'
22
import type {
33
PostgresFunction,
44
PostgresMaterializedView,
5+
PostgresRelationship,
56
PostgresSchema,
67
PostgresTable,
78
PostgresType,
@@ -13,6 +14,7 @@ export const apply = ({
1314
tables,
1415
views,
1516
materializedViews,
17+
relationships,
1618
functions,
1719
types,
1820
arrayTypes,
@@ -21,6 +23,7 @@ export const apply = ({
2123
tables: (PostgresTable & { columns: unknown[] })[]
2224
views: (PostgresView & { columns: unknown[] })[]
2325
materializedViews: (PostgresMaterializedView & { columns: unknown[] })[]
26+
relationships: PostgresRelationship[]
2427
functions: PostgresFunction[]
2528
types: PostgresType[]
2629
arrayTypes: PostgresType[]
@@ -145,6 +148,18 @@ export interface Database {
145148
return output
146149
})}
147150
}
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+
]
148163
}`
149164
)
150165
}

test/server/typegen.ts

+21
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test('typegen', async () => {
3030
id?: number
3131
name?: string
3232
}
33+
Relationships: []
3334
}
3435
memes: {
3536
Row: {
@@ -56,6 +57,15 @@ test('typegen', async () => {
5657
name?: string
5758
status?: Database["public"]["Enums"]["meme_status"] | null
5859
}
60+
Relationships: [
61+
{
62+
foreignKeyName: "memes_category_fkey"
63+
columns: ["category"]
64+
referencedSchema: "public"
65+
referencedRelation: "category"
66+
referencedColumns: ["id"]
67+
}
68+
]
5969
}
6070
todos: {
6171
Row: {
@@ -74,6 +84,15 @@ test('typegen', async () => {
7484
id?: number
7585
"user-id"?: number
7686
}
87+
Relationships: [
88+
{
89+
foreignKeyName: "todos_user-id_fkey"
90+
columns: ["user-id"]
91+
referencedSchema: "public"
92+
referencedRelation: "users"
93+
referencedColumns: ["id"]
94+
}
95+
]
7796
}
7897
users: {
7998
Row: {
@@ -91,6 +110,7 @@ test('typegen', async () => {
91110
name?: string | null
92111
status?: Database["public"]["Enums"]["user_status"] | null
93112
}
113+
Relationships: []
94114
}
95115
users_audit: {
96116
Row: {
@@ -111,6 +131,7 @@ test('typegen', async () => {
111131
previous_value?: Json | null
112132
user_id?: number | null
113133
}
134+
Relationships: []
114135
}
115136
}
116137
Views: {

0 commit comments

Comments
 (0)