Let's say I am using materialized paths to store management chains:
Table: User
id name management_chain
1 Senior VP {1}
2 Middle Manager {1,2}
3 Cubicle Slave {1,2,3}
4 Janitor {1,2,4}
How do I construct a query given a user id that returns all of his direct reports, eg given the middle manager, it should return Cubicle Slave and Janitor, given the Senior VP it should return the Middle Manager. Put another way, what would be a good way to get all records where the management_chain
contains the id queried for at a position that is second to last (given that the last item represent the user's own id).
In other words, how do I represent the following SQL:
SELECT *
FROM USER u
WHERE u.management_chain @> {stored_variable, u.id}
My current JS:
var collection = Users.forge()
.query('where', 'management_chain', '@>', [req.user.id, id]);
Which errors out with
ReferenceError: id is not defined