supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
1.04k stars 133 forks source link

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

Open whollacsek opened 1 year ago

whollacsek commented 1 year ago

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

Additional context

Add any other context about the problem here.

soedirgo commented 1 year ago

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.

whollacsek commented 1 year ago

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: @.***>

rienheuver commented 1 year ago

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 ;-)