Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow providing a config value to remove optional parameter generation #556

Open
pnappa opened this issue Dec 18, 2023 · 3 comments
Open

Comments

@pnappa
Copy link

pnappa commented Dec 18, 2023

Is your feature request related to a problem? Please describe.
It's fairly easy when refactoring nullable variables to miss somewhere that it's supposed to be changed. For non-nullable variables, there's a fairly reliable catch - as the variable is required to be provided to the query.run(params, conn) function, Typescript will raise an error.

However, if the variable is nullable, as of #482 nullable variables are now optional, so they do not raise an error if you provide a variable with the wrong name (typescript's excess property checking often does not catch these cases, never for me 😞).

Describe the solution you'd like
Provide a feature flag to the config file to remove the optional parameter type for nullable types. Something like noOptionalParameters: boolean.

I'm just testing if you'll like this feature, or if you'll want a different name for the flag. I can take responsibility for writing it, it's a very easy PR to make - prob <10 lines of code changes, and a few tests.

@maxweisel
Copy link

I'd really love this feature. I recently wrote an upsert query and missed a ! on one of parameters required for the insert part. Despite the column being not null in postgres, the generated typescript type key was marked optional and so it compiled cleanly and then the query failed at runtime for having a null value.

The majority of our columns are marked not null. I'd love a setting in pgtyped that assumes all parameters in the query are not optional unless they have a question mark like $parameter?.

That said, is there a reason pg-typed can't determine whether a parameter is required/optional from the query itself?

@meoyawn
Copy link

meoyawn commented Jun 10, 2024

in addition this would allow copy pasting the SQL into pg console for debugging. :user_id! doesn't work in console but :user_id does

@pnappa
Copy link
Author

pnappa commented Jun 11, 2024

Seems we've reached a critical mass for me to bother writing a PR 😄 See #582

I haven't been able to test it though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants