Skip to content

Commit 19c9f3a

Browse files
committed
wip: returns vs returns setof rows 1
explore bug a bit: PostgREST/postgrest#4232
1 parent a1dff4b commit 19c9f3a

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

test/advanced_rpc.test.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,21 @@ describe('advanced rpc', () => {
154154
},
155155
})
156156
let result: Exclude<typeof res.data, null>
157-
let expected: z.infer<typeof UserProfileSchema>[]
157+
let expected: z.infer<typeof UserProfileSchema>
158158
expectType<TypeEqual<typeof result, typeof expected>>(true)
159159
expect(res).toMatchInlineSnapshot(`
160160
Object {
161161
"count": null,
162-
"data": Array [
163-
Object {
164-
"id": 1,
165-
"username": "supabot",
166-
},
167-
],
162+
"data": Object {
163+
"id": 1,
164+
"username": "supabot",
165+
},
168166
"error": null,
169167
"status": 200,
170168
"statusText": "OK",
171169
}
172170
`)
173-
UserProfileSchema.array().parse(res.data)
171+
UserProfileSchema.parse(res.data)
174172
})
175173

176174
test('function with scalar input', async () => {
@@ -505,16 +503,14 @@ describe('advanced rpc', () => {
505503
expect(res).toMatchInlineSnapshot(`
506504
Object {
507505
"count": null,
508-
"data": Array [
509-
Object {
510-
"id": 1,
506+
"data": Object {
507+
"id": 1,
508+
"username": "supabot",
509+
"users": Object {
510+
"catchphrase": "'cat' 'fat'",
511511
"username": "supabot",
512-
"users": Object {
513-
"catchphrase": "'cat' 'fat'",
514-
"username": "supabot",
515-
},
516512
},
517-
],
513+
},
518514
"error": null,
519515
"status": 200,
520516
"statusText": "OK",
@@ -536,7 +532,7 @@ describe('advanced rpc', () => {
536532
expect(res).toMatchInlineSnapshot(`
537533
Object {
538534
"count": null,
539-
"data": Array [],
535+
"data": null,
540536
"error": null,
541537
"status": 200,
542538
"statusText": "OK",
@@ -554,7 +550,7 @@ describe('advanced rpc', () => {
554550
expect(res2).toMatchInlineSnapshot(`
555551
Object {
556552
"count": null,
557-
"data": Array [],
553+
"data": null,
558554
"error": null,
559555
"status": 200,
560556
"statusText": "OK",
@@ -572,15 +568,13 @@ describe('advanced rpc', () => {
572568
expect(res3).toMatchInlineSnapshot(`
573569
Object {
574570
"count": null,
575-
"data": Array [
576-
Object {
571+
"data": Object {
572+
"username": "supabot",
573+
"users": Object {
574+
"catchphrase": "'cat' 'fat'",
577575
"username": "supabot",
578-
"users": Object {
579-
"catchphrase": "'cat' 'fat'",
580-
"username": "supabot",
581-
},
582576
},
583-
],
577+
},
584578
"error": null,
585579
"status": 200,
586580
"statusText": "OK",

test/db/00-schema.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,10 @@ create table public.cornercase (
170170

171171
-- Function that returns a single user profile for a user
172172
CREATE OR REPLACE FUNCTION public.get_user_profile(user_row users)
173-
RETURNS SETOF user_profiles
173+
RETURNS user_profiles
174174
LANGUAGE SQL STABLE
175-
ROWS 1
176175
AS $$
177-
SELECT * FROM public.user_profiles WHERE username = user_row.username;
176+
SELECT * FROM public.user_profiles WHERE username = user_row.username LIMIT 1;
178177
$$;
179178

180179
-- Same definition, but will be used with a type override to pretend this can't ever return null

test/embeded_functions_join.test.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,31 @@ describe('embeded functions select', () => {
357357
"username": "supabot",
358358
},
359359
Object {
360-
"user_called_profile": null,
360+
"user_called_profile": Object {
361+
"id": null,
362+
"username": null,
363+
},
361364
"username": "kiwicopple",
362365
},
363366
Object {
364-
"user_called_profile": null,
367+
"user_called_profile": Object {
368+
"id": null,
369+
"username": null,
370+
},
365371
"username": "awailas",
366372
},
367373
Object {
368-
"user_called_profile": null,
374+
"user_called_profile": Object {
375+
"id": null,
376+
"username": null,
377+
},
369378
"username": "jsonuser",
370379
},
371380
Object {
372-
"user_called_profile": null,
381+
"user_called_profile": Object {
382+
"id": null,
383+
"username": null,
384+
},
373385
"username": "dragarcia",
374386
},
375387
],
@@ -479,19 +491,27 @@ describe('embeded functions select', () => {
479491
"username": "supabot",
480492
},
481493
Object {
482-
"user_called_profile": null,
494+
"user_called_profile": Object {
495+
"username": null,
496+
},
483497
"username": "kiwicopple",
484498
},
485499
Object {
486-
"user_called_profile": null,
500+
"user_called_profile": Object {
501+
"username": null,
502+
},
487503
"username": "awailas",
488504
},
489505
Object {
490-
"user_called_profile": null,
506+
"user_called_profile": Object {
507+
"username": null,
508+
},
491509
"username": "jsonuser",
492510
},
493511
Object {
494-
"user_called_profile": null,
512+
"user_called_profile": Object {
513+
"username": null,
514+
},
495515
"username": "dragarcia",
496516
},
497517
],

test/types.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ export type Database = {
708708
Returns: {
709709
id: number
710710
username: string | null
711-
}[]
711+
}
712712
SetofOptions: {
713713
from: 'users'
714714
to: 'user_profiles'

0 commit comments

Comments
 (0)