7

I have the following select query:

return supabase
    .from("Content")
    .select(`*, Users (userId, firstname)`)
    .eq("Users.userId", "Content.userId")
    .eq("contentId", contentId)
    .limit(1)
    .single();

the response object has this structure:

{
  contentId: string;
  content: json;
  Users:
    | ({
        userId: string;
      } & {
        firstname: string | null;
      })
    | ({
        userId: string;
      } & {
        firstname: string | null;
      })[]
    | null
    | undefined;
};

I know for a fact that Users join will always return a single element, can I avoid the typescript definition to be an element or an array? I want it to just be an element (or null, or undefined).

1 Answer 1

17

Turns out you can specify the Typescript type in the select statement:

type ReturnType = Content["Row"] & {
  User: Users;
};

 return supabase
    .from("Content")
    .select(
      `*,
      User:Users(*)`
    )
    .eq("short_id", shortId)
    .returns<ReturnType>()
    .single();
1
  • 1
    Google brought me here with supabase typescript database schema list return arrray - pretty dumb search request but I did find what I was looking for. I was not aware of the use of returns there.
    – alexander
    Jul 19, 2023 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.