supabase / postgrest-js

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

Querying JSON columns with the special symbols #505

Open noskovvkirill opened 7 months ago

noskovvkirill commented 7 months ago

Bug report

Describe the bug

Hi! We are trying to query the data from our jsonb column that has a special symbol @. We are following the recommended spec by schema-dts

This is an example of code snippet we run:

   const { data, error } = await supabase
          .from('item')
          .select(
            `*, userId, media3Id(*), blockId!inner(*, media3Id!inner(*), consumptionId), newId, customData`,
          )
          .eq('media3Id->data->>schemaOrg2->>@type', schemaPrefix)
          .eq('userId', input.userId)
          .order('createdAt', { ascending: false });
        if (error) {

This is the result that we see

 unexpected "@" expecting "-", digit or field name (* or [a..z0..9_$])

Is there anything that we are missing?

Screenshot 2023-11-16 at 12 03 42

To Reproduce

  1. Insert data into the jsonb column formatted as schema-dts with @type field
  2. Query the data by @type using supabase-js client
steve-chavez commented 7 months ago

Thanks for the report. I can reproduce:

curl 'localhost:3000/bets?data_json->@type=eq.4'

{"code":"PGRST100","details":"unexpected \"@\" expecting \"-\", digit or field name (* or [a..z0..9_$])","hint":null,"message":"\"failed to parse tree path (data_json->@type)\" (line 1, column 12)"}
steve-chavez commented 7 months ago

As a workaround, you could do:

.eq('media3Id->data->>schemaOrg2', {"@type": 1})

I've confirmed this works:

curl 'localhost:3000/jsontest?json->a->b=eq.\{"@type":1\}'

[{"json":{"a": {"b": {"@type": 1}}},"integer":1}]
laurenceisla commented 6 months ago

Wrapping the key in quotations seems to work:

curl "http://localhost:3000/jsonvals?a->>\"@key\"=eq.1"
[{"@key":"1"}]

Doing this should work as another workaround:

.eq('media3Id->data->>schemaOrg2->>"@type"', schemaPrefix)