supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
965 stars 129 forks source link

Database function RPC result has incorrect typing #401

Closed chris-at-fabius-labs closed 1 year ago

chris-at-fabius-labs commented 1 year ago

Bug report

Describe the bug

Using supabase-js@2.8.0, when I call a database function that is supposed to return an array of records, the typing of the data incorrectly appears as an array of arrays of records, but is actually just an array of records.

To Reproduce

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

  1. Create a database function that returns a table:
    CREATE OR REPLACE FUNCTION "getEvents"(
    "userId" uuid DEFAULT NULL,
    search text = '',
    "limit" int = 0,
    ascending boolean = true
    )
    RETURNS TABLE (
    "id" uuid,
    "name" text,
    "startDate" date,
    "startTime" time,
    "locationName" text,
    "timeZone" text,
    "organizer_id" uuid,
    "organizer_avatar" text,
    "organizer_firstName" text,
    "organizer_lastName" text
    ) as $$
    // ... skipping irrelevant details
    return plv8.execute(query, params);
    $$ language plv8;
  2. Generate types with the Supabase CLI, which yields:
    export interface Database {
    public: {
    Functions: {
      getEvents: {
        Args: {
          userId?: string;
          search?: string;
          limit?: number;
          ascending?: boolean;
        };
        Returns: {
          id: string;
          name: string;
          startDate: string;
          startTime: string;
          locationName: string;
          timeZone: string;
          organizer_id: string;
          organizer_avatar: string;
          organizer_firstName: string;
          organizer_lastName: string;
        }[];
      };
    };
    };
    }
  3. Call the database function using the Supabase JS library:
    const { data, error } = await supabaseClientInstance.rpc('getEvents', args);
  4. data property of response object is typed as a 2-dimensional array:
    const data: {
    id: string;
    name: string;
    startDate: string;
    startTime: string;
    locationName: string;
    timeZone: string;
    organizer_id: string;
    organizer_avatar: string;
    organizer_firstName: string;
    organizer_lastName: string;
    }[][] | null
  5. If I console.log(data), it prints as a 1-dimensional array.

Expected behavior

The type of the data property on the RPC response object should be correct.

System information

chris-at-fabius-labs commented 1 year ago

I am able to work-around this issue by calling data.flat(1), which has no real effect on the already-flat array, but satisfies TypeScript's type-checker that the array is definitely 1-dimensional.

soedirgo commented 1 year ago

Can you try npm update @supabase/postgrest-js (it's a dependency of supabase-js)? There was an issue with how .rpc() return types are handled, but it needs an update on both the CLI (which seems to be up-to-date here) and the client library.

soedirgo commented 1 year ago

Closing this now, but let us know if updating the client library doesn't resolve it.

chris-at-fabius-labs commented 1 year ago

Confirmed that updating @supabase/postgrest-js resolved the issue. Thank you!