TypeORM queryBuilder : orderBy relation count

Viewed 76

I have a basic query to retrieve course entities that have a manyToMany relation with student as registered_students. I also would like to map a students_count property which I managed to achieve through loadRelationCountAndMap.

What I can't figure out is how to orderBy that students_count value that is not a column of the course entity :

return await this.courseRepository
      .createQueryBuilder('course')
      .where('course.teacherId = :teacherId', { teacherId: teacher.id })
      .leftJoin('course.registered_students', 'student')
      .addSelect('student.firstName')
      .addSelect('student.lastName')
      .loadRelationCountAndMap(
        'course.students_count',
        'course.registered_students',
        'students_count',
      )
      .getMany();

I understand I can't simply add something .orderBy('course.students_count', 'DESC') as it doesn't refer to a column but I can't find a simple way for that, even though it can appear trivial....

0 Answers
Related