Skip to content

Commit 40c06b8

Browse files
committed
fix: add setof from * for all relation functions
1 parent bc81acd commit 40c06b8

File tree

2 files changed

+132
-10
lines changed

2 files changed

+132
-10
lines changed

src/server/templates/typescript.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ export const apply = async ({
221221
)
222222
const returnsSetOfTable = fn.is_set_returning_function && fn.return_type_relation_id !== null
223223
const returnsMultipleRows = fn.prorows !== null && fn.prorows > 1
224+
// Case 1: if the function returns a table, we need to add SetofOptions to allow selecting sub fields of the table
225+
// Those can be used in rpc to select sub fields of a table
226+
if (returnTableName) {
227+
setofOptionsInfo = `SetofOptions: {
228+
from: "*"
229+
to: ${JSON.stringify(returnTableName)}
230+
isOneToOne: ${Boolean(!returnsMultipleRows)}
231+
isSetofReturn: ${fn.is_set_returning_function}
232+
}`
233+
}
234+
// Case 2: if the function has a single table argument, we need to add SetofOptions to allow selecting sub fields of the table
235+
// and set the right "from" and "to" values to allow selecting from a table row
224236
if (fn.args.length === 1) {
225237
const relationType = relationTypeByIds.get(fn.args[0].type_id)
226238

@@ -249,16 +261,6 @@ export const apply = async ({
249261
}
250262
}
251263
}
252-
// Case 3: Special case for functions without table arguments still returning a table
253-
// Those can be used in rpc to select sub fields of a table
254-
else if (returnTableName) {
255-
setofOptionsInfo = `SetofOptions: {
256-
from: "*"
257-
to: ${JSON.stringify(returnTableName)}
258-
isOneToOne: ${Boolean(!returnsMultipleRows)}
259-
isSetofReturn: ${fn.is_set_returning_function}
260-
}`
261-
}
262264

263265
return `${returnType}${fn.is_set_returning_function && returnsMultipleRows ? '[]' : ''}
264266
${setofOptionsInfo ? `${setofOptionsInfo}` : ''}`

test/server/typegen.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,12 @@ test('typegen: typescript', async () => {
632632
user_name: string | null
633633
user_status: Database["public"]["Enums"]["user_status"] | null
634634
}
635+
SetofOptions: {
636+
from: "*"
637+
to: "user_todos_summary_view"
638+
isOneToOne: true
639+
isSetofReturn: true
640+
}
635641
}
636642
get_todos_by_matview: {
637643
Args: { "": unknown }
@@ -685,6 +691,12 @@ test('typegen: typescript', async () => {
685691
id: number
686692
"user-id": number
687693
}[]
694+
SetofOptions: {
695+
from: "*"
696+
to: "todos"
697+
isOneToOne: false
698+
isSetofReturn: true
699+
}
688700
}
689701
get_todos_setof_rows:
690702
| {
@@ -785,6 +797,12 @@ test('typegen: typescript', async () => {
785797
name: string | null
786798
status: Database["public"]["Enums"]["user_status"] | null
787799
}[]
800+
SetofOptions: {
801+
from: "*"
802+
to: "users"
803+
isOneToOne: false
804+
isSetofReturn: true
805+
}
788806
}
789807
| {
790808
Args: { completed: boolean; todo_id: number }
@@ -837,6 +855,12 @@ test('typegen: typescript', async () => {
837855
id: number
838856
"user-id": number
839857
}[]
858+
SetofOptions: {
859+
from: "*"
860+
to: "todos"
861+
isOneToOne: false
862+
isSetofReturn: true
863+
}
840864
}
841865
test_internal_query: { Args: never; Returns: undefined }
842866
test_unnamed_row_composite: {
@@ -863,6 +887,12 @@ test('typegen: typescript', async () => {
863887
id: number
864888
"user-id": number
865889
}[]
890+
SetofOptions: {
891+
from: "*"
892+
to: "todos"
893+
isOneToOne: false
894+
isSetofReturn: true
895+
}
866896
}
867897
| {
868898
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -1712,6 +1742,12 @@ test('typegen w/ one-to-one relationships', async () => {
17121742
user_name: string | null
17131743
user_status: Database["public"]["Enums"]["user_status"] | null
17141744
}
1745+
SetofOptions: {
1746+
from: "*"
1747+
to: "user_todos_summary_view"
1748+
isOneToOne: true
1749+
isSetofReturn: true
1750+
}
17151751
}
17161752
get_todos_by_matview: {
17171753
Args: { "": unknown }
@@ -1765,6 +1801,12 @@ test('typegen w/ one-to-one relationships', async () => {
17651801
id: number
17661802
"user-id": number
17671803
}[]
1804+
SetofOptions: {
1805+
from: "*"
1806+
to: "todos"
1807+
isOneToOne: false
1808+
isSetofReturn: true
1809+
}
17681810
}
17691811
get_todos_setof_rows:
17701812
| {
@@ -1865,6 +1907,12 @@ test('typegen w/ one-to-one relationships', async () => {
18651907
name: string | null
18661908
status: Database["public"]["Enums"]["user_status"] | null
18671909
}[]
1910+
SetofOptions: {
1911+
from: "*"
1912+
to: "users"
1913+
isOneToOne: false
1914+
isSetofReturn: true
1915+
}
18681916
}
18691917
| {
18701918
Args: { completed: boolean; todo_id: number }
@@ -1917,6 +1965,12 @@ test('typegen w/ one-to-one relationships', async () => {
19171965
id: number
19181966
"user-id": number
19191967
}[]
1968+
SetofOptions: {
1969+
from: "*"
1970+
to: "todos"
1971+
isOneToOne: false
1972+
isSetofReturn: true
1973+
}
19201974
}
19211975
test_internal_query: { Args: never; Returns: undefined }
19221976
test_unnamed_row_composite: {
@@ -1943,6 +1997,12 @@ test('typegen w/ one-to-one relationships', async () => {
19431997
id: number
19441998
"user-id": number
19451999
}[]
2000+
SetofOptions: {
2001+
from: "*"
2002+
to: "todos"
2003+
isOneToOne: false
2004+
isSetofReturn: true
2005+
}
19462006
}
19472007
| {
19482008
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -2792,6 +2852,12 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
27922852
user_name: string | null
27932853
user_status: Database["public"]["Enums"]["user_status"] | null
27942854
}
2855+
SetofOptions: {
2856+
from: "*"
2857+
to: "user_todos_summary_view"
2858+
isOneToOne: true
2859+
isSetofReturn: true
2860+
}
27952861
}
27962862
get_todos_by_matview: {
27972863
Args: { "": unknown }
@@ -2845,6 +2911,12 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
28452911
id: number
28462912
"user-id": number
28472913
}[]
2914+
SetofOptions: {
2915+
from: "*"
2916+
to: "todos"
2917+
isOneToOne: false
2918+
isSetofReturn: true
2919+
}
28482920
}
28492921
get_todos_setof_rows:
28502922
| {
@@ -2945,6 +3017,12 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
29453017
name: string | null
29463018
status: Database["public"]["Enums"]["user_status"] | null
29473019
}[]
3020+
SetofOptions: {
3021+
from: "*"
3022+
to: "users"
3023+
isOneToOne: false
3024+
isSetofReturn: true
3025+
}
29483026
}
29493027
| {
29503028
Args: { completed: boolean; todo_id: number }
@@ -2997,6 +3075,12 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
29973075
id: number
29983076
"user-id": number
29993077
}[]
3078+
SetofOptions: {
3079+
from: "*"
3080+
to: "todos"
3081+
isOneToOne: false
3082+
isSetofReturn: true
3083+
}
30003084
}
30013085
test_internal_query: { Args: never; Returns: undefined }
30023086
test_unnamed_row_composite: {
@@ -3023,6 +3107,12 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
30233107
id: number
30243108
"user-id": number
30253109
}[]
3110+
SetofOptions: {
3111+
from: "*"
3112+
to: "todos"
3113+
isOneToOne: false
3114+
isSetofReturn: true
3115+
}
30263116
}
30273117
| {
30283118
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -3877,6 +3967,12 @@ test('typegen: typescript w/ postgrestVersion', async () => {
38773967
user_name: string | null
38783968
user_status: Database["public"]["Enums"]["user_status"] | null
38793969
}
3970+
SetofOptions: {
3971+
from: "*"
3972+
to: "user_todos_summary_view"
3973+
isOneToOne: true
3974+
isSetofReturn: true
3975+
}
38803976
}
38813977
get_todos_by_matview: {
38823978
Args: { "": unknown }
@@ -3930,6 +4026,12 @@ test('typegen: typescript w/ postgrestVersion', async () => {
39304026
id: number
39314027
"user-id": number
39324028
}[]
4029+
SetofOptions: {
4030+
from: "*"
4031+
to: "todos"
4032+
isOneToOne: false
4033+
isSetofReturn: true
4034+
}
39334035
}
39344036
get_todos_setof_rows:
39354037
| {
@@ -4030,6 +4132,12 @@ test('typegen: typescript w/ postgrestVersion', async () => {
40304132
name: string | null
40314133
status: Database["public"]["Enums"]["user_status"] | null
40324134
}[]
4135+
SetofOptions: {
4136+
from: "*"
4137+
to: "users"
4138+
isOneToOne: false
4139+
isSetofReturn: true
4140+
}
40334141
}
40344142
| {
40354143
Args: { completed: boolean; todo_id: number }
@@ -4082,6 +4190,12 @@ test('typegen: typescript w/ postgrestVersion', async () => {
40824190
id: number
40834191
"user-id": number
40844192
}[]
4193+
SetofOptions: {
4194+
from: "*"
4195+
to: "todos"
4196+
isOneToOne: false
4197+
isSetofReturn: true
4198+
}
40854199
}
40864200
test_internal_query: { Args: never; Returns: undefined }
40874201
test_unnamed_row_composite: {
@@ -4108,6 +4222,12 @@ test('typegen: typescript w/ postgrestVersion', async () => {
41084222
id: number
41094223
"user-id": number
41104224
}[]
4225+
SetofOptions: {
4226+
from: "*"
4227+
to: "todos"
4228+
isOneToOne: false
4229+
isSetofReturn: true
4230+
}
41114231
}
41124232
| {
41134233
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }

0 commit comments

Comments
 (0)