I want to run a query towards PostgreSQL where any attribute in a JSON body matching a list of attribute names should equal a given value.
The input: names: ["foo", "bar"], value: 'hello'
should render a query such as SELECT ... WHERE data.foo = 'hello' OR data.bar = 'hello'
.
However, I want to use a single array parameter for the names, not manually expand it to $1, $2
etc depending on the number of attribute names in the array.
I know how to bind a dynamic attribute name like this:
query('SELECT "mytable.*" FROM "mytable" WHERE "mytable"."data"->>$1 = $2', ['foo', 'hello'])
... and I also know how to pass an array parameter which is used to supply a set of values using WHERE data.foo ANY ($1)
etc.
But, is it possible to send a single array parameter denoting a list of attribute names where at least one of those attributes must match the given value? Is there a function within PostgreSQL to say something like (pseudo-query to illustrate): query('SELECT * FROM mytable WHERE data-->any_of($1) = $2', [ ['foo', 'bar'], 'hello' ]