Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Typescript: safer nullability in functions, domain support#573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ngasull wants to merge1 commit intosupabase:master
base:master
Choose a base branch
Loading
fromngasull:ts-null-in-functions-and-domains

Conversation

ngasull
Copy link

@ngasullngasull commentedApr 25, 2023
edited
Loading

What kind of change does this PR introduce?

Fixes#842

What is the current behavior?

CREATEDOMAINstrict_textAStextNOT NULL;CREATEFUNCTIONsome_function(arg strict_text)RETURNS table (nulltexttext, stricttext strict_text)LANGUAGE SQLAS $$SELECTNULL::text, arg$$;

Generated type withsupabase gen types typescript --local

exportinterfaceDatabase{public:{Functions:{some_function:{Args:{arg:unknown}Returns:{nulltext:stringstricttext:unknown}[]}}}}

What is the new behavior?

Generated types should be:

exportinterfaceDatabase{public:{Functions:{some_function:{Args:{arg:string}Returns:{nulltext:string|nullstricttext: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 inpgTypeToTsType.

Additional context

This is obviously abreaking change for most users using functions. It is however the safe way, exposing sound types.

jaredramirez, BarnabyShearer, hajin-chung, splatoonstat, toffee12312321, noname241198, and eugeneford reacted with heart emoji
Add support for domains and take their nullability into account.Centralize logic in `pgTypeToTsType`.
@ngasullngasull requested review froma team ascode ownersApril 25, 2023 09:08
@jumski
Copy link

Hey@ngasull Postgres domains have some troubles withNOT 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.

@ngasull
Copy link
Author

@jumski This PR only aims to improvefunction 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.

Donnerstagnacht and jumski reacted with thumbs up emoji

@Donnerstagnacht
Copy link

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.

@jumski
Copy link

@ngasull thanks for clarification,
TIL that domains can enforce NOT NULL when used to type function arguments.

Everything looks fine!

@eugeneford
Copy link

It would be incredibly amazing if we could have this done. When could we have this available?

Donnerstagnacht reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Typescript gen : invalid SQL Function types
4 participants
@ngasull@jumski@Donnerstagnacht@eugeneford

[8]ページ先頭

©2009-2025 Movatter.jp