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

Improve error reporting on insert/upsert calls #519

Open
olee opened this issue Feb 20, 2024 · 0 comments
Open

Improve error reporting on insert/upsert calls #519

olee opened this issue Feb 20, 2024 · 0 comments

Comments

@olee
Copy link

olee commented Feb 20, 2024

I noticed that the postgrest client has issues with error highlighting on insert/upsert calls when there is a type missmatch between the expected and provided properties.
I was able to trace this back to the overload defined on insert / upsert which is causing typescript to have problems with determining the correct signature if there's an error in the payload and therefore error-highlighting the whole function call:

image

As an example, if I define a function without any overload, the error highlights works way better:
image

And if I define my test function with an overload the same way as it is done in the client, it breaks again:
image

A solution would be either to unify both calls into a single signature (which would just mean that defaultToNull option would be defined unnecessarily for non-bulk operations) or to just split it into insert & insertBulk / upsert & upsertBulk or something like that.


Original post (less relevant than actual issue) As far as I can see, functions like `insert` and `upsert` in the query builder use a generic type `Row` for the values to insert when in fact this type should not be necessary at all.

So instead of this:

insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row,
options?: {
count?: 'exact' | 'planned' | 'estimated'
}
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships>

It should work better if it is defined like this instead:

 insert( 
   values: Relation extends { Insert: unknown } ? Relation['Insert'] : never,
   options?: { 
     count?: 'exact' | 'planned' | 'estimated' 
   } 
 ): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> 

This would improve type discovery a bit and improve error reporting.

The reason I say it's not required is because the generic type is only used for the input and not used anywhere on the output.

@olee olee changed the title Remove unnecessary generics in query builder functions Improve error reporting on insert/upsert calls Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant