Within my database, there exists a relationship between Users and Roles. A User can have multiple Roles assigned to them. These Roles are connected to Activities in such a way that they can be associated with many different activities. This association allows any User with a specific Role to perform the corresponding Activity.
When using Sequelize, what is the most effective method to query whether a particular User has at least one Role granting permission to carry out a specific activity identified by an activity ID? The goal is to receive a true/false response indicating whether the User is authorized to perform the Activity before proceeding.
If we were to run a raw SQL query for this scenario, it might look something like:
SELECT EXISTS(SELECT *
FROM "Activity_Role"
WHERE "ActivityId" = 'READ_FILE'
AND "RoleId" = ANY(SELECT "RoleId"
FROM "User_Role"
WHERE "UserId" = '12321')
LIMIT 1);
Is there a more elegant solution to achieve this without relying on a raw query?