I am currently studying TypeORM and I am struggling to grasp the functionality of the 'query' method.
Here is an example of some code:
export const lists = async (
roleFilter: RoleFilter,
filter: Filter,
dateFilter: BetweenDate,
) => {
const carLists = await getCars(roleFilter, filter);
if (carLists.length === 0) {
return { totalCount: 0, lists: [] };
}
const carIds = carLists.map(car => car.id); // **2 Extracting only IDs here
const isCheckParam: number[] = [];
let isCheckSQL = '';
if (filter.isCheck !== null) {
....
}
const typeParamArray: string[] = [];
const typeSQLArray: string[] = [];
if (filter.impact === 1) {
typeParamArray.push('impact');
typeSQLArray.push('car_alarm.type = ?');
}
....
const typeSQL =
typeSQLArray.length === 0 ? '' : `(${typeSQLArray.join(' OR ')}) AND`;
const countQuery = `
SELECT
count(*) AS cnt
FROM
car_alarm
WHERE ${typeSQL} ${isCheckSQL} car_alarm.carId IN (?) AND DATE(alarmAt) BETWEEN ? AND ?
const countQuery = `
SELECT
count(*) AS cnt
FROM
car_alarm
WHERE ${typeSQL} ${isCheckSQL} car_alarm.carId IN (?) AND DATE(alarmAt) BETWEEN ? AND ?
`;
const countResult = await getManager().query(countQuery, [
...typeParamArray,
...isCheckParam,
carIds,
dateFilter.start,
dateFilter.end
]);
The use of getManager().query() in this section is essential.
const countResult = await getManager().query(countQuery, [
...typeParamArray,
...isCheckParam,
carIds,
dateFilter.start,
dateFilter.end
]);
Can someone provide me with an explanation on how the 'query()' function operates? I have been unsuccessful in finding any examples or explanations regarding its usage.