Skip to main content

Refactor RolesService retrieveStorageContextByCode

In the current implementation, every time we have to upsert a privilege or retrieve a role by lookupKey, we call the method retrieveStorageContextByCode provided by the RolesService (microservice). This method perform the following steps:

  1. retrieve the role identified by lookupKey

  2. if the role has extendedRoleIds, it performs an additional query for each extended role id

  3. if the extended role has extendedRoleIds, repeat step 2 and 3 (recursion)

Note: steps 2 and 3 are executed sequentially

Currently the max depth for extended roles is 2:

organization-admin/owner → space-supervisor → space-folder, course-observer

This means executing up to 2 queries for each extended role.

In order to avoid this amount of queries, we can refactor the method by leveraging population ($lookup). This way we will perform a single query.

Note: if we want to support more than 2 levels of inheritance, we can adjust the method like this:

  1. retrieve role and populate up to the second level

  2. if second-level roles have extendedRoleIds, collect role ids and repeat step 1 on them (using find instead of findOne)

  3. repeat until second-level roles have no extendedRoleIds

Domain
AuthN-Z
Action
refactor
Department
Backend
Status: Completed1 comment

Log in to comment and vote

Comments1

  • Elisur Freyesus

    •

    Apr 26, 2025

    This makes sense @Ivan Ligotino because the recursive approach leads to an increasing number of sequential queries adding latency on the db.