supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.

Home Page:https://supabase.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

whollacsek opened this issue · 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.

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:

single<
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never
>(): PostgrestBuilder<ResultOne> {

Let me know if you think this is insufficient.

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