I'm trying to migrate an existing RAW query to use typeORM's queryBuilder. For this I'm creating a class called utilityHelpers which contains methods that returns subqueries.
So far the code looks like this:
// Main query
const x = await this.createQueryBuilder('user')
.select([
...utilityHelpers.getUserListPageColumns(),
])
.leftJoin(utilityHelpers.getUserListAssociatedGroups(schoolId), 'gr', 'true')
.leftJoin(utilityHelpers.getUserListAssociatedAcademies(schoolId), 'user_ac', 'true')
.leftJoin(utilityHelpers.getUserListAssociatedFormVariants(), 'fv', 'true')
.leftJoin(utilityHelpers.getUserListAssociatedReferrers(), 'referrers', 'true')
.leftJoin(utilityHelpers.getUserListAssociatedUserStages(schoolId), 'userStages', 'true')
.leftJoin(utilityHelpers.getUserListAssociatedCountry(), 'userCountry', 'true')
.where(utilityHelpers.getWhereUserIsNew(filtersDictionary, schoolId))
.getRawOne()
This is the helper:
// utilityHelpers
class UtilityHelper {
// some code here
public class getWhereUserIsNew(filters: filters, schoolId: string) {
const filter = filters.isNew;
if(!filter || filter?.value === 'all') return '';
const subQuery = getRepository('userSchool')
.createQueryBuilder('user_school')
.where('"user_school"."isNew" = :isNewValue', { isNewValue: filter.value })
.andWhere('"user_school"."userId" = "user"."id"')
if (schoolId === 'all') {
subQuery.andWhere('"user_school"."schoolId" = :schoolId', { schoolId });
}
const rawQuery = subQuery.getSql();
subQuery.getQuery = () => `EXISTS (${rawQuery})`;
return subQuery;
}
// other code
}
When I use this helper in my main query I get this error:
name: EntityColumnNotFound
message: No entity column "parameterIndex" was found.