supabase / postgrest-js

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

Match at least one filter not working as expected #428

Open nCrafts opened 1 year ago

nCrafts commented 1 year ago

Describe the bug

I am trying to use the .or method to match at least one filter. It works with strings and also json objects, but doesn't seem to work for json arrays. However the syntax does work with the .filter method.

To Reproduce

I have a table table_name with a jsonb column named data. The content of this column is:

{
  "one": {
    "value": [
      {
        "label": "Red",
        "value": "red"
      }
    ]
  },
  "two": {
    "value": [
      {
        "label": "Green",
        "value": "green"
      }
    ]
  }
}

This query works as expected, fetching the one row:

let two = await supabase
.from("table_name")
.select("*")
.filter("data->one->value", "cs", '[{"label":"Red","value":"red"}]');
console.log(two);

However this query fails with an error:

let one = await supabase
.from("table_name")
.select("*")
.or('data->one->value.cs.[{"label":"Red","value":"red"}]');
console.log(one);

Error being:

{"code":"PGRST100","details":"unexpected \":\" expecting \"->>\", \"->\" or delimiter (.)","hint":null,"message":"\"failed to parse logic tree ((data->one->value.cs.[{\"label\":\"Red\",\"value\":\"red\"}]))\" (line 1, column 47)"}

If I replace the .or with this I no longer see an error, but it also doesn't fetch a row:

.or('data->one->value.cs.{"label":"Red","value":"red"}');

Expected behavior

I'd like to be able to use match one filter (or perhaps match all filters) with an array of objects. Something like:

let one = await supabase
.from("table_name")
.select("*")
.or('data->one->value.cs.[{"label":"Red","value":"red"}], data->two->value.cs.[{"label":"Green","value":"green"}]');
console.log(one);

System information

Alexays commented 9 months ago

Any news on that? Did you find a workaround @nCrafts?

nCrafts commented 9 months ago

@Alexays I did not. My use-case got more complex so I ended up writing a custom function instead.