Vex · Scope policies

Row filter by role (match)

A match rule in a scope policy adds a WHERE condition on its column when a query is compiled; the query document is unchanged. Example: under the usher policy the query reads every booking, and under the member policy the same query gains member_id = $1, a condition on a column the query does not name.

Query
{
  from: ['bookings'],
  fields: ['bookings.show_night', 'bookings.seats'],
  sort: [{ field: 'bookings.show_night', dir: 'asc' }],
}
Policy for bookings, by role
{
  usher: { read: [] },
  member: {
    read: [{ match: 'member_id', to: 'memberId' }],
    insert: [{ set: 'member_id', to: 'memberId' }],
  },
}
SQL, usher policy
SELECT b1.show_night AS "show_night", b1.seats AS "seats"
FROM bookings AS b1
ORDER BY b1.show_night ASC
LIMIT 100
SQL, member policy
SELECT b1.show_night AS "show_night", b1.seats AS "seats"
FROM bookings AS b1
WHERE b1.member_id = $1
ORDER BY b1.show_night ASC
LIMIT 100
Parameters, member policy
[{ key: 'memberId', kind: 'scope', type: 'string' }]
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.