-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
feat(types): add json path type inference #592
base: master
Are you sure you want to change the base?
Conversation
name: Name | ||
alias: PropertyName | ||
castType: PropertyType | ||
jsonPath: JsonPathToAccessor< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this to format and save the value "path accessor" information from the parser to the result inference. That way, the parser can still be agnostic to the database types, only caring about string parsing.
@@ -544,3 +544,35 @@ export type FindFieldMatchingRelationships< | |||
name: Field['name'] | |||
} | |||
: SelectQueryError<'Failed to find matching relation via name'> | |||
|
|||
export type JsonPathToAccessor<Path extends string> = Path extends `${infer P1}->${infer P2}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn something like data->some->>property
into some.property
? JsonPathToAccessor<P1> | ||
: Path | ||
|
||
export type JsonPathToType<T, Path extends string> = Path extends '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse some path like some.property
and try to match it with the type T provided.
const { data, error } = await postgrest.from('users').select('status').eq('status', 'ONLINE') | ||
if (error) { | ||
throw new Error(error.message) | ||
const result = await postgrest.from('users').select('status').eq('status', 'ONLINE') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here for some reason (maybe TS upgrade) using this syntax:
const { data, error } = await ...
if (error) {
throw XXX
}
// here data was still of type data[] | null
// because typescript seemed to fail to understand the branching
Changing to this syntax properly narrow down result.data
as non nullable once we checked the error.
That's already a black-magic level, but validated behavior on our live project and everything works as expected. |
Gentle ping @soedirgo |
Seems to not work on embedded tables:
(I updated the types for public.users.data with But that can be handled in a separate PR 👍 |
What kind of change does this PR introduce?
Allow users to override Json type with custom types, and take into account the "json accessor" to infer the proper resulting type.
What is the current behavior?
Given a table defined like so:
The following behavior is observed:
What is the new behavior?
Additional context
Add any other context or screenshots.