I need to select some "work types" from a channel. These "channels" are in "work groups" and both channels and work groups need to have permissions to view them (tables are ChannelUsers and WorkGroupUsers to check this). The views that can be selected are as such:
this.views = {
"*" : ["*"],
"index" : [
"Timers.worktype_id",
"WorkTypes.worktypename",
"Timers.start",
"Timers.end",
"Users.username",
"Timers.closed",
"Users.username",
"Users.id AS user_id",
"(Timers.end - Timers.start) AS timer_length"
],
"get" : [
"Timers.worktype_id",
"Timers.id AS timer_id",
"Timers.start",
"Timers.end",
"Timers.user_id",
"Timers.work_id",
"Timers.closed",
"ChannelUsers.user_id AS user_id",
"Users.username"
]
The query, however is getting pretty grossly long. Being my first webapp ever I'm not used to chaining this many joins together. Is this at all acceptable? Should I be using some more efficient method of checking authorisation?
let checkQuery = `
SELECT
${view}
FROM Timers
INNER JOIN WorkTypes
ON Timers.worktype_id=WorkTypes.id
AND WorkTypes.channel_id=?
INNER JOIN ChannelUsers
ON ChannelUsers.channel_id=WorkTypes.channel_id
AND ChannelUsers.user_id=?
INNER JOIN Channels
ON Channels.id=ChannelUsers.channel_id
INNER JOIN Users
ON Users.id=?
INNER JOIN WorkGroupUsers
ON WorkGroupUsers.workgroup_id=Channels.workgroup_id
AND WorkGroupUsers.user_id=?
INNER JOIN WorkGroups
ON WorkGroupUsers.workgroup_id=WorkGroups.id;
`;
checkQuery = mysql.format(checkQuery, [_channelID, _user.id, _user.id, _user.id]);
I've heard some conflicting opinions on "views" and "temporary tables" but nothing I'm certain could help here. Any opinions would be greatly appreciated!
(The views are separate as part of a system for "building" many-to-one relationships into the resulting object, i.e building all posts a user has made by their ID into the method result, I cut it here for simplicity).