Typescript: safer nullability in functions, domain support
What kind of change does this PR introduce?
Fixes https://github.com/supabase/postgres-meta/issues/842
What is the current behavior?
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::text, arg
$$;
Generated type with supabase gen types typescript --local
export interface Database {
public: {
Functions: {
some_function: {
Args: {
arg: unknown
}
Returns: {
nulltext: string
stricttext: unknown
}[]
}
}
}
}
What is the new behavior?
Generated types should be:
export interface Database {
public: {
Functions: {
some_function: {
Args: {
arg: string
}
Returns: {
nulltext: string | null
stricttext: string
}[]
}
}
}
}
Please review tests for more details on new expected behavior.
- Stop accepting non-nullable types in generated function arguments and return types.
- Add support for domains and take their nullability into account.
- Centralize nullability and array logic in
pgTypeToTsType.
Additional context
This is obviously a breaking change for most users using functions. It is however the safe way, exposing sound types.
Hey @ngasull Postgres domains have some troubles with NOT NULL constraint:
CREATE DOMAIN in Postgres cannot safely enforce a NOT NULL constraint because domains are type aliases and do not directly validate constraints at the table level. While the domain itself can define a NOT NULL constraint, the database schema still allows null values in columns of that domain type unless the column explicitly defines NOT NULL. This behavior creates inconsistency: a domain's NOT NULL constraint is ignored unless explicitly reinforced on each column, leading to potential silent failures in enforcing the constraint.
But there is definitely a problem with serializing domain types with "gen-types" - they default to undefined.
@jumski This PR only aims to improve function generated types, no impact on tables. So the changes are still relevant.
Domains add a layer of safety over types at PostgreSQL runtime. Primitive types are unsafe (nullable) and it makes sense generate them as nullable unless they are a domain defined as NOT NULL.
Hi @soedirgo , just a quick ping to see if you (or another maintainer) might have a chance to take a look at this PR when you have time. It touches on issues discussed in #841 and #842 and it is open since 2 years but still relevant.
@ngasull thanks for clarification, TIL that domains can enforce NOT NULL when used to type function arguments.
Everything looks fine!
It would be incredibly amazing if we could have this done. When could we have this available?
We need this change! there's so much potential for runtime errors with inaccurate typing