I'm new in typeorm I wrote a query that will innerjoinandselect a multitude of tables as follow:
async findAllTripsWithSpecificStatusForUser(id: string, pageIndex: number, pageSize: number, sort: 'ASC' | 'DESC'): Promise<[Trip[], number]>{
let sortBy:'ASC' | 'DESC' = 'DESC';
if (sort) {
sortBy = sort;
}
return await this.tripRepository.createQueryBuilder("trip")
.innerJoinAndSelect("trip.matches", "matches")
.where("trip.state = :state", {
state: 1
})
.andWhere("trip.state = :state", {
state: 3
})
.innerJoin("trip.clientAccount", "clientAccount")
.innerJoin("clientAccount.user", "user")
.innerJoin("user.reputation", "reputation")
.innerJoinAndSelect("matches.truck", "truck")
.innerJoinAndSelect("truck.transporterAccount", "transporterAccount")
.where("transporterAccount.id = :userUuid", {
userUuid: id
})
.skip(pageIndex * pageSize)
.take(pageSize)
.getManyAndCount();
}
Each user has many rating values in the reputation table. I want to select the average of those values and return them.
How can achieve this ? I think I need to use some kind of subquery ?