I have some tables like so:
users
id | name
-----------------------
1 Ben
2 Josh
groups
id | name
-----------------------
1 Group 1
2 Group 2
group_users
id | group_id | user_id
------------------------------------
1 1 1
forms
id | name
-----------------------
1 Contact Us
2 Questions
form_access
id | form_id | user_id | group_id
-------------------------------------------------------
1 1 1 null
2 2 null 1
Essentially, access to a form can be given to an entire group or a user individually. I’m looking for the most efficient way to get all the forms a user has access to. Right now, I’m using Supabase with Postgres and I thought creating a view might be the best way to go (supabase-js client isn’t too flexible on queries like this so I needed a view):
SELECT
form_access.user_id,
group_users.user_id as group_user_id,
forms.*
FROM
forms
INNER JOIN form_access ON form_access.form_id = forms.id
LEFT JOIN group_users ON group_users.group_id = form_access.group_id
And from there, I can query where a user_id or group_user_id is the authenticated user. I’m looking for a more efficient way to do this or a better approach though and hoping for some feedback. Additionally, I’ll need to paginate this data and also, given a form_id, ensure the user has access to a particular form.