Skip to content

Commit

Permalink
Merge pull request #629 from supabase/fix/sort-relationships-by-refer…
Browse files Browse the repository at this point in the history
…enced-table

fix(typegen): sort relationships by (fkey name, referenced table)
  • Loading branch information
soedirgo authored Nov 1, 2023
2 parents b269dd3 + 72fe5c6 commit 61772da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ export interface Database {
relationship.schema === table.schema &&
relationship.relation === table.name
)
.sort(({ foreign_key_name: a }, { foreign_key_name: b }) =>
a.localeCompare(b)
.sort(
(a, b) =>
a.foreign_key_name.localeCompare(b.foreign_key_name) ||
a.referenced_relation.localeCompare(b.referenced_relation)
)
.map(
(relationship) => `{
Expand Down
2 changes: 2 additions & 0 deletions test/db/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ create table user_details (
user_id int8 references users(id) primary key,
details text
);

create view a_view as select id from users;
40 changes: 40 additions & 0 deletions test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ test('typegen', async () => {
"user-id"?: number
}
Relationships: [
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
isOneToOne: false
referencedRelation: "a_view"
referencedColumns: ["id"]
},
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
Expand Down Expand Up @@ -115,6 +122,13 @@ test('typegen', async () => {
user_id?: number
}
Relationships: [
{
foreignKeyName: "user_details_user_id_fkey"
columns: ["user_id"]
isOneToOne: true
referencedRelation: "a_view"
referencedColumns: ["id"]
},
{
foreignKeyName: "user_details_user_id_fkey"
columns: ["user_id"]
Expand Down Expand Up @@ -172,6 +186,18 @@ test('typegen', async () => {
}
}
Views: {
a_view: {
Row: {
id: number | null
}
Insert: {
id?: number | null
}
Update: {
id?: number | null
}
Relationships: []
}
todos_matview: {
Row: {
details: string | null
Expand All @@ -186,6 +212,13 @@ test('typegen', async () => {
referencedRelation: "users"
referencedColumns: ["id"]
},
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
isOneToOne: false
referencedRelation: "a_view"
referencedColumns: ["id"]
},
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
Expand Down Expand Up @@ -219,6 +252,13 @@ test('typegen', async () => {
referencedRelation: "users"
referencedColumns: ["id"]
},
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
isOneToOne: false
referencedRelation: "a_view"
referencedColumns: ["id"]
},
{
foreignKeyName: "todos_user-id_fkey"
columns: ["user-id"]
Expand Down

0 comments on commit 61772da

Please sign in to comment.