supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.02k stars 201 forks source link

Typescript gen : invalid SQL Function types #1030

Open ngasull opened 1 year ago

ngasull commented 1 year ago

Bug report

Describe the bug

For both input and input types, typescript generation of SQL functions is invalid in some cases :

I think that this bug is not caused by a PostgreSQL limitation as views produce the correct typing : primitives are nullable, domains generate as their underlying type and when domains are marked NOT NULL they are even non-nullable in typescript.

To Reproduce

CREATE DOMAIN strict_text AS text NOT NULL;

CREATE FUNCTION some_function(arg strict_text)
RETURNS table (nulltext text, stricttext strict_text)
LANGUAGE SQL AS $$
   SELECT NULL::uuid, arg
$$;

Generated type with supabase gen types typescript --local

export interface Database {
  public: {
    Functions: {
      some_function: {
        Args: {
          arg: unknown
        }
        Returns: {
          nulltext: string
          stricttext: unknown
        }[]
      }
    }
  }
}

Expected behavior

Generated types should be

export interface Database {
  public: {
    Functions: {
      some_function: {
        Args: {
          arg: string
        }
        Returns: {
          nulltext: string | null
          stricttext: string
        }[]
      }
    }
  }
}

System information

Additional context

I :heart: the work being done and the mindset at Supabase. I believe strict typing is crucial to the success of a "backend in the DB" and I could help with a PR if I'm being given some pointers to the code responsible of the TS generation :wave:

sweatybridge commented 1 year ago

Thanks for the detailed bug report. The typescript generation code is in postgres-meta repo if you are keen to take a look.

ngasull commented 1 year ago

Just pushed a PR for this issue, feel free to have a look! :slightly_smiling_face:

mwoss commented 10 months ago

That would be a great addition to the client library tho :3 I would love to see it live in the near future!