In some code I’m working on, I would like to have a list of all users matching a particular search query, and I’d like each user object to be annotated with the roles of which they are members.
I’m currently doing this by listing all the users in the roles that I care about, and then as I read the user list, I check manually if their user ID is present in each role. This works fine, however, it is inefficient for lots of users. Say I have a role with a few thousand users in it, but my user query only returns a few - I still have to query the thousands of users for the role. The alternative is that I make an API call to get each user’s role list, but if I have a lot of users, I will run out of API rate limit very quickly.
Is there a way to do this efficiently? Rules can edit tokens easily enough, but is it possible to craft a rule which appends roles to the user object?
My next attempt will be to annotate the user’s roles manually via app_metadata - which should be efficient, but I have to keep things up to date in there myself.
Thanks for any tips.