supabase / postgres-meta

A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries
https://supabase.com
Apache License 2.0
939 stars 126 forks source link

[typescript gen] Add `CompositeTypes` helper #797

Closed f-elix closed 3 months ago

f-elix commented 3 months ago

There are already the Tables, Enums, etc. helper types to easily extract types from a database schema, which is super convenient. I suggest adding a CompositeTypes helper to do the same thing for composite types.

I would open a PR myself but I no nothing about go and I couldn't find where the CLI generates those types, but the code is pretty similar to the other helpers:

export type CompositeTypes<
    PublicCompositeTypeNameOrOptions extends
        | keyof PublicSchema['CompositeTypes']
        | { schema: keyof Database },
    CompositeTypeName extends PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
        ? keyof Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes']
        : never = never
> = PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
    ? Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'][CompositeTypeName]
    : PublicCompositeTypeNameOrOptions extends keyof PublicSchema['CompositeTypes']
        ? PublicSchema['CompositeTypes'][PublicCompositeTypeNameOrOptions]
        : never;
sweatybridge commented 3 months ago

The code generation templates are in postgres-meta repo https://github.com/supabase/postgres-meta/blob/master/src/server/templates/typescript.ts. Feel free to tag me if you want to make a PR.

f-elix commented 3 months ago

Thank you so much! Opening a PR now.