adelsz / pgtyped

pgTyped - Typesafe SQL in TypeScript
https://pgtyped.dev
MIT License
2.91k stars 94 forks source link

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

Open pnappa opened 8 months ago

pnappa commented 8 months ago

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 https://github.com/adelsz/pgtyped/pull/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 commented 5 months ago

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 commented 2 months ago

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 commented 2 months ago

Seems we've reached a critical mass for me to bother writing a PR 😄 See https://github.com/adelsz/pgtyped/pull/582

I haven't been able to test it though.