Skip to content

Commit d1e3ad9

Browse files
committed
fix: relationtype setof functions generation
1 parent 26265dd commit d1e3ad9

File tree

3 files changed

+20
-115
lines changed

3 files changed

+20
-115
lines changed

src/server/templates/typescript.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,32 @@ 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-
225-
// Only add SetofOptions for functions with table arguments (embedded functions)
226-
// or specific functions that RETURNS table-name
227-
if (fn.args.length === 1 && relationTypeByIds.get(fn.args[0].type_id)) {
228-
// Case 1: Standard embedded function with proper setof detection
229-
if (returnsSetOfTable && returnTableName) {
230-
setofOptionsInfo = `SetofOptions: {
231-
from: ${JSON.stringify(typesById.get(fn.args[0].type_id)?.format)}
224+
if (fn.args.length === 1) {
225+
const relationType = relationTypeByIds.get(fn.args[0].type_id)
226+
227+
// Only add SetofOptions for functions with table arguments (embedded functions)
228+
// or specific functions that RETURNS table-name
229+
if (relationType) {
230+
const sourceTable = relationType.format
231+
// Case 1: Standard embedded function with proper setof detection
232+
if (returnsSetOfTable && returnTableName) {
233+
setofOptionsInfo = `SetofOptions: {
234+
from: ${JSON.stringify(sourceTable)}
232235
to: ${JSON.stringify(returnTableName)}
233236
isOneToOne: ${Boolean(!returnsMultipleRows)}
234237
isSetofReturn: true
235238
}`
236-
}
237-
// Case 2: Handle RETURNS table-name those are always a one to one relationship
238-
else if (returnTableName && !returnsSetOfTable) {
239-
const sourceTable = typesById.get(fn.args[0].type_id)?.format
240-
const targetTable = returnTableName
241-
setofOptionsInfo = `SetofOptions: {
239+
}
240+
// Case 2: Handle RETURNS table-name those are always a one to one relationship
241+
else if (returnTableName && !returnsSetOfTable) {
242+
const targetTable = returnTableName
243+
setofOptionsInfo = `SetofOptions: {
242244
from: ${JSON.stringify(sourceTable)}
243245
to: ${JSON.stringify(targetTable)}
244246
isOneToOne: true
245247
isSetofReturn: false
246248
}`
249+
}
247250
}
248251
}
249252
// Case 3: Special case for functions without table arguments still returning a table

test/lib/types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ test('composite type attributes', async () => {
9595

9696
const res = await pgMeta.types.list()
9797
expect(res.data?.find(({ name }) => name === 'test_composite')).toMatchInlineSnapshot(
98-
{ id: expect.any(Number) },
99-
`
98+
{ id: expect.any(Number) }, `
10099
{
101100
"attributes": [
102101
{
@@ -114,10 +113,9 @@ test('composite type attributes', async () => {
114113
"id": Any<Number>,
115114
"name": "test_composite",
116115
"schema": "public",
117-
"type_relation_id": 16966,
116+
"type_relation_id": 16964,
118117
}
119-
`
120-
)
118+
`)
121119

122120
await pgMeta.query(`drop type test_composite;`)
123121
})

test/server/typegen.ts

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,6 @@ test('typegen: typescript', async () => {
627627
user_name: string | null
628628
user_status: Database["public"]["Enums"]["user_status"] | null
629629
}
630-
SetofOptions: {
631-
from: "*"
632-
to: "user_todos_summary_view"
633-
isOneToOne: true
634-
isSetofReturn: true
635-
}
636630
}
637631
get_todos_from_user:
638632
| {
@@ -672,12 +666,6 @@ test('typegen: typescript', async () => {
672666
id: number
673667
"user-id": number
674668
}[]
675-
SetofOptions: {
676-
from: "*"
677-
to: "todos"
678-
isOneToOne: false
679-
isSetofReturn: true
680-
}
681669
}
682670
get_todos_setof_rows:
683671
| {
@@ -778,12 +766,6 @@ test('typegen: typescript', async () => {
778766
name: string | null
779767
status: Database["public"]["Enums"]["user_status"] | null
780768
}[]
781-
SetofOptions: {
782-
from: "*"
783-
to: "users"
784-
isOneToOne: false
785-
isSetofReturn: true
786-
}
787769
}
788770
| {
789771
Args: { completed: boolean; todo_id: number }
@@ -854,12 +836,6 @@ test('typegen: typescript', async () => {
854836
id: number
855837
"user-id": number
856838
}[]
857-
SetofOptions: {
858-
from: "*"
859-
to: "todos"
860-
isOneToOne: false
861-
isSetofReturn: true
862-
}
863839
}
864840
| {
865841
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -1704,12 +1680,6 @@ test('typegen w/ one-to-one relationships', async () => {
17041680
user_name: string | null
17051681
user_status: Database["public"]["Enums"]["user_status"] | null
17061682
}
1707-
SetofOptions: {
1708-
from: "*"
1709-
to: "user_todos_summary_view"
1710-
isOneToOne: true
1711-
isSetofReturn: true
1712-
}
17131683
}
17141684
get_todos_from_user:
17151685
| {
@@ -1749,12 +1719,6 @@ test('typegen w/ one-to-one relationships', async () => {
17491719
id: number
17501720
"user-id": number
17511721
}[]
1752-
SetofOptions: {
1753-
from: "*"
1754-
to: "todos"
1755-
isOneToOne: false
1756-
isSetofReturn: true
1757-
}
17581722
}
17591723
get_todos_setof_rows:
17601724
| {
@@ -1855,12 +1819,6 @@ test('typegen w/ one-to-one relationships', async () => {
18551819
name: string | null
18561820
status: Database["public"]["Enums"]["user_status"] | null
18571821
}[]
1858-
SetofOptions: {
1859-
from: "*"
1860-
to: "users"
1861-
isOneToOne: false
1862-
isSetofReturn: true
1863-
}
18641822
}
18651823
| {
18661824
Args: { completed: boolean; todo_id: number }
@@ -1931,12 +1889,6 @@ test('typegen w/ one-to-one relationships', async () => {
19311889
id: number
19321890
"user-id": number
19331891
}[]
1934-
SetofOptions: {
1935-
from: "*"
1936-
to: "todos"
1937-
isOneToOne: false
1938-
isSetofReturn: true
1939-
}
19401892
}
19411893
| {
19421894
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -2781,12 +2733,6 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
27812733
user_name: string | null
27822734
user_status: Database["public"]["Enums"]["user_status"] | null
27832735
}
2784-
SetofOptions: {
2785-
from: "*"
2786-
to: "user_todos_summary_view"
2787-
isOneToOne: true
2788-
isSetofReturn: true
2789-
}
27902736
}
27912737
get_todos_from_user:
27922738
| {
@@ -2826,12 +2772,6 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
28262772
id: number
28272773
"user-id": number
28282774
}[]
2829-
SetofOptions: {
2830-
from: "*"
2831-
to: "todos"
2832-
isOneToOne: false
2833-
isSetofReturn: true
2834-
}
28352775
}
28362776
get_todos_setof_rows:
28372777
| {
@@ -2932,12 +2872,6 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
29322872
name: string | null
29332873
status: Database["public"]["Enums"]["user_status"] | null
29342874
}[]
2935-
SetofOptions: {
2936-
from: "*"
2937-
to: "users"
2938-
isOneToOne: false
2939-
isSetofReturn: true
2940-
}
29412875
}
29422876
| {
29432877
Args: { completed: boolean; todo_id: number }
@@ -3008,12 +2942,6 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
30082942
id: number
30092943
"user-id": number
30102944
}[]
3011-
SetofOptions: {
3012-
from: "*"
3013-
to: "todos"
3014-
isOneToOne: false
3015-
isSetofReturn: true
3016-
}
30172945
}
30182946
| {
30192947
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }
@@ -3863,12 +3791,6 @@ test('typegen: typescript w/ postgrestVersion', async () => {
38633791
user_name: string | null
38643792
user_status: Database["public"]["Enums"]["user_status"] | null
38653793
}
3866-
SetofOptions: {
3867-
from: "*"
3868-
to: "user_todos_summary_view"
3869-
isOneToOne: true
3870-
isSetofReturn: true
3871-
}
38723794
}
38733795
get_todos_from_user:
38743796
| {
@@ -3908,12 +3830,6 @@ test('typegen: typescript w/ postgrestVersion', async () => {
39083830
id: number
39093831
"user-id": number
39103832
}[]
3911-
SetofOptions: {
3912-
from: "*"
3913-
to: "todos"
3914-
isOneToOne: false
3915-
isSetofReturn: true
3916-
}
39173833
}
39183834
get_todos_setof_rows:
39193835
| {
@@ -4014,12 +3930,6 @@ test('typegen: typescript w/ postgrestVersion', async () => {
40143930
name: string | null
40153931
status: Database["public"]["Enums"]["user_status"] | null
40163932
}[]
4017-
SetofOptions: {
4018-
from: "*"
4019-
to: "users"
4020-
isOneToOne: false
4021-
isSetofReturn: true
4022-
}
40233933
}
40243934
| {
40253935
Args: { completed: boolean; todo_id: number }
@@ -4090,12 +4000,6 @@ test('typegen: typescript w/ postgrestVersion', async () => {
40904000
id: number
40914001
"user-id": number
40924002
}[]
4093-
SetofOptions: {
4094-
from: "*"
4095-
to: "todos"
4096-
isOneToOne: false
4097-
isSetofReturn: true
4098-
}
40994003
}
41004004
| {
41014005
Args: { "": Database["public"]["Tables"]["todos"]["Row"] }

0 commit comments

Comments
 (0)