supabase/postgres-meta

[typescript gen] Add `CompositeTypes` helper

f-elix opened this issue · 2 comments

f-elix commented

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;

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

Thank you so much! Opening a PR now.