I'm a bit confused on how to resolve my requests multiple times without affecting the performance of my code in nodejs (v16.13.0)
It turns out that I am doing a monthly report and in this I need different data to be able to generate it. What I do is obtain the monthly data from the beginning to the end of the month, in addition to other queries that I make to the database, which I have in supabase and that Postgree uses, to unify all my data.
I make use of promises, specifically Promise.all, since in this way I speed up the development of my asynchronous requests and once they have all finished resolving I use the data. This is the structure I currently use:
// Arrangements where I will store the response of the requests
let usersxData: UsersxModalityxRolexJob[] = [];
let data_monthly: HoursActivityWeeklySummary[] = [];
let attendance_schedule: AttendanceSchedule[] = [];
let time_off_request: TimeOffRequestRpc[] = [];
let configs: IndicatorConfigs[] = [];
// Class where I have the methods that make the requests to supabase
const supabaseService = new SupabaseService();
// The list of requests to resolve
const promises = [
// Function that obtains a monthly summary in averages
supabaseService.getSummaryWeekRpcWihoutFreelancers(req.query.start_date, req.query.end_date).then(dataFromDB => {
data_monthly = dataFromDB as any;
}),
// User list
supabaseService.getUsersEntity(res).then(dataFromDB => {
usersxData = dataFromDB as any;
}),
// Schedule during the month
supabaseService.getAttendaceScheduleRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
attendance_schedule = dataFromDB as any;
}),
supabaseService.getTimeOffRequestRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
time_off_request = dataFromDB as any;
}),
supabaseService.getConfigs(res).then(dataFromDB => {
configs = dataFromDB;
})
];
// resolve all requests
Promise.all(promises);
// get the schedule
res.json(attendance_schedule)
But I'm at a point where I need to make a multiple request. How is that? Once I get my schedule I want to iterate over that array that brings a date to consult my database and bring those days
It means that first I make my requests and with the response of this I want to make an iterable request again where I send to consult the DB on specific dates
To better illustrate myself, make the following code
//Arrays where the response of the requests
let userxData: UsersxModalityxRolexJob[] = [];
let data_monthly: HoursActivityWeeklySummary[] = [];
let time_attendance: TimeAttendance[] = [];
let time_off_request: TimeOffRequestRpc[] = [];
leave the configuration: FlagConfigs[] = [];
// Class where I have the methods that make the requests to supabase
const supabaseService = new SupabaseService();
// The list of requests to resolve
constant promises = [
// Function that gets a monthly summary in averages
supabaseService.getSummaryWeekRpcWihoutFreelancers(req.query.start_date, req.query.end_date).then(dataFromDB => {
data_monthly = dataFromDB as any;
}),
// User list
supabaseService.getUsersEntity(res).then(dataFromDB => {
usersxData = dataFromDB like any;
}),
// Hours during the month
supabaseService.getAttendaceScheduleRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
Assistance_schedule = dataFromDB like any;
}),
supabaseService.getTimeOffRequestRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
time_off_request = dataFromDB as any;
}),
supabaseService.getConfigs(res).then(dataFromDB => {
settings = dataFromDB;
})
];
// resolve all requests
Promise.all(promises);
schedule_attendance.map(async element => {
let start_date = item.date_start.toString();
let end_date = element.date_end.toString();
let supabase = supabaseinstance();
leave query = '';
let {
data,
error
} = wait supabase.rpc < HoursActivityWeeklySummary > ("weekly activity", {
start_date,
end_date
})
.select(query ? query : '*').in('title', ['DEV', 'SAC', 'DESIÑO']).in('modality', ['FULLTIME', 'PARTTIME']).order('username', {
ascending: false
});
if (data) {
return data
} else {
console.log(error)
}
})
But I know and understand that the previous code from where you can see it is wrong, because the only thing that can cause it is that at some point in the request there is some kind of error. Also, I'm not respecting the structure of Promise.all as I need to declare promises in a higher scope where it can be used with it.
So how can I make multiple requests after I've made the main ones? What I'd like to see is an example of how this is done instead of getting the solution directly, which is why I'm not adding a demo of my current code.
UPDATE
export const generateMonthlyReport = async(req: Request and any, res: Response, next: NextFunction) => {
try {
let usersxData: UsersxModalityxRolexJob[] = [];
let data_monthly: HoursActivityWeeklySummary[] = [];
let time_attendance: TimeAttendance[] = [];
let time_off_request: TimeOffRequestRpc[] = [];
let settings: FlagConfigs[] = [];
let attendanceInMonthly: any[] = [];
const supabaseService = new SupabaseService();
const promises = [
supabaseService.getSummaryWeekRpcWihoutFreelancers(req.query.start_date, req.query.end_date).then(dataFromDB => {
data_monthly = dataFromDB as any;
}),
supabaseService.getUsersEntity(res).then(dataFromDB => {
usersxData = dataFromDB like any;
}),
supabaseService.getAttendaceScheduleRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
Assistance_schedule = dataFromDB like any;
}),
supabaseService.getTimeOffRequestRpc(req.query.start_date, req.query.end_date).then(dataFromDB => {
time_off_request = dataFromDB as any;
}),
supabaseService.getConfigs(res).then(dataFromDB => {
settings = dataFromDB;
})
];
await Promise.all(promises)
attendance_schedule.forEach(element => {
let start_date = element.date_start.toString();
let end_date = element.date_end.toString();
supabaseService.getTrackedByDateAndIDArray(start_date, end_date).then(item => {
console.log(item);
attendanceInMonthly.push(item)
})
})
res.json(assistanceInMonthly)
} catch (error) {
console.log(error);
res.status(500).json({
title: 'API-CIT Error',
message: 'Internal Server Error'
});
}
}