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

feat(types): add json path type inference #592

Merged
merged 7 commits into from
Jan 20, 2025

Conversation

avallete
Copy link
Member

@avallete avallete commented Jan 12, 2025

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:

type CustomJsonType = {
  foo: string
  bar: {
    baz: number
  }
  en: 'ONE' | 'TWO' | 'THREE'
}
table: {
 data: CustomJsonType | null
}

The following behavior is observed:

select(`data`) -> {data: CustomJsonType | null}
select(`data->bar`) -> {bar: Json}
select(`data->>bar`) -> {bar: string}
select(`data->>en`) -> {bar: string}
select(`data->bar->baz`) -> {baz: Json}

What is the new behavior?

select(`data`) -> {data: CustomJsonType | null}
select(`data->bar`) -> { bar:  { baz: number } }
// Preserve the postgrest casting behaviour due to ->> accessor
select(`data->>bar`) -> {bar: string}
// Preserve the union of string
select(`data->>en`) -> {bar:  'ONE' | 'TWO' | 'THREE'}
// Preserve the accessed ressource type
select(`data->bar->baz`) -> {baz: number}
// Fallback on the default behavior for non-matching properties
select(`data->bar->nope`) -> {nope: Json}
select(`data->bar->>nope`) -> {nope: string}

Additional context

Add any other context or screenshots.

src/select-query-parser/parser.ts Show resolved Hide resolved
src/select-query-parser/utils.ts Show resolved Hide resolved
src/select-query-parser/utils.ts Show resolved Hide resolved
test/index.test-d.ts Show resolved Hide resolved
@kamilogorek
Copy link
Member

That's already a black-magic level, but validated behavior on our live project and everything works as expected.

@kamilogorek
Copy link
Member

Gentle ping @soedirgo

@soedirgo
Copy link
Member

Seems to not work on embedded tables:

    const result = await postgrest
      .from('messages')
      .select('users(data->bar)')

(I updated the types for public.users.data with CustomUserDataType)

But that can be handled in a separate PR 👍

@avallete
Copy link
Member Author

Seems to not work on embedded tables:

    const result = await postgrest
      .from('messages')
      .select('users(data->bar)')

(I updated the types for public.users.data with CustomUserDataType)

But that can be handled in a separate PR 👍

Good catch, it was an issue with the parser and the closing parenthesis, I fixed it here: a9842b2

And added appropriate tests for coverage 👍

@avallete avallete merged commit 7e985d4 into master Jan 20, 2025
2 checks passed
Copy link

🎉 This PR is included in version 1.18.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants