postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

.returns() should not allow .single() to be chained

Open whollacsek opened this issue 2 years ago • 3 comments

Bug report

Describe the bug

.returns() should not allow .single() to be chained otherwise it'll produce incorrect types.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to '…'
  2. Click on '…'
  3. Scroll down to '…'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: [e.g. macOS, Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of supabase-js: [e.g. 6.0.2]
  • Version of Node.js: [e.g. 10.10.0]

Additional context

Add any other context about the problem here.

whollacsek avatar Mar 17 '23 05:03 whollacsek

You'll want to pass an array type to .returns() here so the types work properly on .single(). We also fall back to never if you pass a non-array type to .returns() so this shouldn't be an issue:

https://github.com/supabase/postgrest-js/blob/9fb56c7e73cd4b7e81e2c8ee4ce5dab55a2f0147/src/PostgrestTransformBuilder.ts#L136-L138

Let me know if you think this is insufficient.

soedirgo avatar Mar 21 '23 10:03 soedirgo

Hi thanks for the explanation, however I feel as a user it's better to have .returns and .single to be mutually exclusive as it guarantee there's only one way of doing things which leads to less confusion.

On Tue, Mar 21, 2023, 17:30 Bobbie Soedirgo @.***> wrote:

You'll want to pass an array type to .returns() here so the types work properly on .single(). We also fall back to never if you pass a non-array type to .returns() so this shouldn't be an issue:

https://github.com/supabase/postgrest-js/blob/9fb56c7e73cd4b7e81e2c8ee4ce5dab55a2f0147/src/PostgrestTransformBuilder.ts#L136-L138

Let me know if you think this is insufficient.

— Reply to this email directly, view it on GitHub https://github.com/supabase/postgrest-js/issues/412#issuecomment-1477597470, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGJQUAQVFDAJLVRX4PIGQ3W5F7MFANCNFSM6AAAAAAWAWXMXM . You are receiving this because you authored the thread.Message ID: @.***>

whollacsek avatar Mar 21 '23 11:03 whollacsek

So for clarity's sake, this is what you should do?

const postId = 1;
await supabase.from('posts').select('name, comments(description)').eq('id', postId).limit(1).returns<Post[]>().single();

Because that's not very intuitive. I wrote it as .single().returns<Post>() because I expected .returns to be the last in the chain. If this works it's okay, but some documentation on this would be very helpful. And a solution where the order I'd prefer would be even better ;-)

rienheuver avatar May 24 '23 13:05 rienheuver