When fetching data for my relations, I opted to use QueryBuilder. In order to validate certain get request parameters before the index, I established a common QueryBuilder instance as shown below.
// Common Get Query
const result = await this.reservationRepo
.createQueryBuilder()
.leftJoinAndSelect('Reservation.userId', 'userId')
.leftJoinAndSelect('Reservation.companyId', 'companyId')
.leftJoinAndSelect('Reservation.petId', 'petId')
.orderBy(`Reservation.${sortBy}`, order)
.where('Reservation.isArchived = :isArchived', { isArchived: false });
The validation logic I implemented looks like this;
//Check req starts is cancelled and return canceled data (getting data from db)
if (getPaginationParamDto.status === GetReservationTypes.CANCELLED) {
if (type === GetReservationTypes.COMPANY) {
result.where('Reservation.companyId = :companyId', { companyId: `${searchID}` });
} else {
result.where('Reservation.userId = :userId', { userId: `${searchID}` });
}
result.where('Reservation.status = :status`, { status: getPaginationParamDto.status });
However, I encountered an issue with my where clause:
isArchived: false
it does not seem to be functioning correctly.
.where('Reservation.isArchived = :isArchived', { isArchived: false });
To resolve this problem, I had to place my isArchived where query after my logic implementation. Although it now works, the condition is still not being applied properly. Assistance in this matter would be greatly appreciated. Thank you.