'How to show generated SQL / raw SQL in TypeORM queryBuilder

I developed typeorm querybuilder. For the purpose of debugging, I'd like to show the generated SQL query.

I tested printSql() method, but it didn't show any SQL query.

const Result = await this.attendanceRepository
  .createQueryBuilder("attendance")
  .innerJoin("attendance.child", "child")
  .select(["attendance.childId","child.class","CONCAT(child.firstName, child.lastName)"])
  .where("attendance.id= :id", { id: id })
  .printSql()
  .getOne()

console.log(Result);

It returned the following:

Attendance { childId: 4, child: Child { class: 'S' } }

My desired result is to get the generated SQL query.

Is there any wrong point? Is there any good way to get the SQL query?



Solution 1:[1]

.getQuery() or .getSql()

const sql1 = await this.attendanceRepository
    .createQueryBuilder("attendance")
    .innerJoin("attendance.child", "child")
    .select(["attendance.childId","child.class","CONCAT(child.firstName, child.lastName)"])
    .where("attendance.id= :id", { id: id })
    .getQuery();
console.log(sql1);
const sql2 = await this.attendanceRepository
    .createQueryBuilder("attendance")
    .innerJoin("attendance.child", "child")
    .select(["attendance.childId","child.class","CONCAT(child.firstName, child.lastName)"])
    .where("attendance.id= :id", { id: id })
    .getSql();
console.log(sql2);    

Sources

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

Source: Stack Overflow

Solution Source
Solution 1