For some of my API routes, I have many many other functions being called to obtain data regarding parameters sent in and those functions each have their own database query call.
Example of a selection of code that uses this functionality.
const [server, activeUnit, unitList, callList] = await Promise.all([
this.patrolService.getServer(serverId),
this.patrolService.getPatrolUnit(user.userId),
this.patrolService.getPatrolUnits(serverId),
this.cadService.getActiveCalls(serverId)
]);
Example of my .getPatrolUnits(serverId):
async getPatrolUnits(serverId: number): Promise<PatrolUnit[]> {
const units = await this.patrolUnitRepository.findBy({ serverId: serverId });
for (let i = 0; i < units.length; ++i) {
units[i] = await this.getPatrolUnit(units[i].userId);
}
return [...units];
}
This loops through all units found in the original query, and then makes a function call using that userId to get more data about the user.
And inside of each .whatever(param), they call to the database which can sometimes result in other .whatever(params) being called inside of each of those. Is there a more streamlined way of doing this, or even more efficient?