I have a postgreSQL database with an entities table with a permissions column of type jsonb. permissions is an array of permissions. Each permission has an array of principals and an operation. The actual JSON of the permissions column for a particular entity could look like for example like this:
[
{
"principals": [
"Administrators",
"Users"
],
"operation": "read"
},
{
"principals": [
"Administrators"
],
"operation": "write"
}
]
Now I want to retrieve all entities where either "Editors" or "Guests" have "read" permission. But the only syntax I can come up with is the one below where I query only the first permission (index=0) of the entities:
SELECT * FROM entities WHERE permissions->0->'principals' ?| array['Editors', 'Guests'] and permissions->0->>'operation' = 'read'
How do I modify this query to comprise all the permissions of an entity?