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

ParserError on return type with aggregate functions

Open DuncanLHS opened this issue 10 months ago • 1 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

There appears to be a problem with the return TS types when using aggregate functions and referenced tables. Instead of getting the expected return type, I'm getting ParserError<'error'>

Example with my function (I believe this is correct and it does run successfully):

export async function getAllProjectsSummary(request: Request) {
  const { supabase } = createSupabaseServerClient({ request });
  const { data, error } = await supabase.from("projects").select(
    "project_no, project_name, project_media(count()), documents(count())",
  );
  if (error) {
    return { error: error.message };
  }
  return data;
}

The data return type here is ParserError<"Expected identifier at `)), documents(count())`">[]

To Reproduce

  1. Create 2 tables with a one-to-many relationship between the first and second.
  2. Run a select postgREST query on table 1 including including an aggregate function on related columns in table 2

Expected behavior

Expected data return type to reflect the query.

i.e.

data: {
    project_no: string;
    project_name: string;
    project_media: {
        count: {}[];
    }[];
    documents: {
        count: {}[];
    }[];
}[]

System information

  • supabase-js: 2.41.1

Additional context

Interestingly using count(*) or count('*') generates the correct return type so it would appear the query builder just isn't handling the brackets correctly for aggregate functions and is expecting something between them as it would for referenced tables. These of course generate an error as they're incorrect syntax for the count function.

A very quick check on a simpler query seems to yield the same error even without referencing related tables.

DuncanLHS avatar Mar 28 '24 15:03 DuncanLHS

Appears to have been fixed in postgrest-js v1.13. Dependency needs updating here.

DuncanLHS avatar Mar 28 '24 15:03 DuncanLHS