supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
967 stars 130 forks source link

Extend query parser w/basic support for `->`/`->>` JSON operators #380

Closed mqp closed 1 year ago

mqp commented 1 year ago

Postgres supports accessing properties in JSON and JSONB columns by using the -> and ->> operators. Postgrest supports this too. However, trying to use this in the Supabase select API makes Typescript barf:

/* schema: users: { Row: { data: Json } } } */
const { data } = await db.from('users').select('data->username, data->>numLikes')

// inferred type of `data` is `ParserError<'Unexpected input: ->username, data->>numLikes'>[] | null`

This small extension to the query parser types makes simple column->foo->bar->>baz property chains work properly, e.g. the above query will infer a type of { username: Json, numLikes: string }.

Note that this doesn't currently work in combination with embedded fields or renaming -- the query parser types would realistically need to be refactored to accomplish that without a serious amount of cut-and-pasted code. I aimed to just hit the common case and scratch my own itch with minimal effort here.

mqp commented 1 year ago

Do you have any interest in this kind of work? I have a project that makes heavy use of JSON columns, and I need to decide whether to try to contribute JSON column QoL improvements upstream like this, or whether I should maintain a fork.

soedirgo commented 1 year ago

Hey, sorry for the late response! Yes, we definitely need this - will take a look later today.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 1.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

mqp commented 1 year ago

Hey, sorry for the late response! Yes, we definitely need this - will take a look later today.

<3