'How to add round brackets to TypeORM's QueryBuilder .from(subquery) method?

I am using QueryBuilder and I need to make a from from SQL function. I am using subqueries, but there is a problem: when I create a SQL query, the round brackets are not created for upc subquery:

qb.leftJoin(
      subQuery => {
        if (sortOption === TherapistAdminSort.UPCOMING_SESSIONS) {
          subQuery
            .select('upcoming.id', 'therapistId')
            .addSelect('count(*)', 'upcomingSessionsCount')
            .from((upc) => {
              return upc
                .createQueryBuilder()
                .select('t.id', 'id')
                .addSelect('s.patientId', 'patientId')
                .from('therapists', 't')
                .withDeleted()
                .innerJoin(s => {
                  return s.select('*')
                    .from(`get_sessions_by_therapist_id(t.id, now())`, 's2');
                }, 's', 's."therapistId" = t.id')
                .groupBy('t.id')
                .addGroupBy('s."patientId"');
            }, 'upcoming')
            .groupBy('upcoming.id');
        }

        return subQuery;
      },
      'grouped',
      'therapist.id = grouped."therapistId"',
    )

This query below produces following LEFT JOIN:

LEFT JOIN (SELECT upcoming.id AS "therapistId", count(*) AS "upcomingSessionsCount"
                FROM SELECT t.id AS id, s.patientId AS "patientId"
                        FROM therapists t
                                INNER JOIN (SELECT * FROM get_sessions_by_therapist_id(t.id, now()) s2) s ON s."therapistId" = t.id 
                        GROUP BY t.id, s."patientId" upcoming
                    GROUP BY upcoming.id) grouped ON therapist.id = grouped."therapistId"

As you can see, there is no round brackets produced.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source