I am building an app for a business company, and they need to control who sees which reports by project and roles, the report can belong to one project and can be seen by many roles (employees roles).
so when the report is submitted it is tagged with the project and roles, like "project1" and {"manager","seller"} , so for example the employees who are working on project1 and are managers can see this report.
The way i do it now is very much depends on arrays, this is what i have:
reports table:
project (string)
roles (array of strings)
employees table:
projects (array of strings) // all the projects the employee working/worked on
roles (array of strings) // employees can have many roles
when querying the reports the employee can see, i do something like this:
select *
from reports
WHERE (employee.roles && report.roles) AND (report.project = ANY (employee.projects))
i use postgresql
the problem is i think this will not have a good performance (i'm not sure)
the only way i know to speed this query is making a GIN index on reports (roles) column, to make the overlap faster
beside performance this tip here, just made me worry:
Tip: Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements.
so is there a much better design to do this, or this will just works fine?