How to remove prefix from returned columns in typeorm querybuilder

Viewed 1599

I set following querybuilder in typeorm.

const sql = this.attendanceRepository
      .createQueryBuilder("attendance")
      .leftJoin("attendance.child", "child")
      .select("attendance")
      .addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
      .addSelect("child.class")
      .where("attendance.id= :id", { id: id})
      .getRawOne()
      const Result = await sql

That querybuilder produces following SQL

SELECT `attendance`.`id` AS `attendance_id`, `attendance`.`child_id` AS `attendance_child_id`,
`attendance`.`attendance_cd` AS `attendance_attendance_cd`, `attendance`.`come_home_time` AS `attendance_come_home_time`, `attendance`.`late_time` AS `attendance_late_time`, `attendance`.`memo` AS `attendance_memo`, `attendance`.`created_at` AS `attendance_created_at`,
`attendance`.`updated_at` AS `attendance_updated_at`, `attendance`.`deleted_at` AS `attendance_deleted_at`, `child`.`class` AS `child_class`, 
CONCAT_WS(' ', `child`.`first_name`, `child`.`middle_name`, `child`.`last_name`) AS `childName` 

FROM `attendances` `attendance` 
LEFT JOIN `children` `child` 
ON `child`.`id`=`attendance`.`child_id` 
WHERE ( `attendance`.`id`= ? ) 
AND ( `attendance`.`deleted_at` IS NULL )

It returned folloiwng data.

{
    "attendance_id": 2,
    "attendance_child_id": 4,
    "attendance_attendance_cd": 3,
    "attendance_come_home_time": "11:50:00",
    "attendance_late_time": "23:40:00",
    "attendance_memo": "test",
    "attendance_created_at": "2020-10-16T13:33:00.053Z",
    "attendance_updated_at": "2020-10-19T10:34:55.000Z",
    "attendance_deleted_at": null,
    "child_class": "S",
    "childName": "hikaru hikaru"
}

But my desired result is folloiwng

{
    "id": 2,
    "child_id": 4,
    "attendance_cd": 3,
    "come_home_time": "11:50:00",
    "late_time": "23:40:00",
    "memo": "test",
    "created_at": "2020-10-16T13:33:00.053Z",
    "updated_at": "2020-10-19T10:34:55.000Z",
    "deleted_at": null,
    "child_class": "S",
    "childName": "hikaru hikaru"
}

How can I remove prefix attendance_ ?

If someone has opinion, please let me know.

Thanks

4 Answers

please use .select("attendance.*")

I think you should use getOne instead getRawOne and it will auto mapped.

const sql = this.attendanceRepository
  .createQueryBuilder("attendance")
  .leftJoin("attendance.child", "child")
  .select("attendance")
  .addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
  .addSelect("child.class")
  .where("attendance.id= :id", { id: id})
  .getOne();

  const Result = await sql;

I hope it'll help you!

I don't know if you find a solution but I hope this one could help anybody with the same issue. You should just use getOne / getMany to have the result will be automapped with your entity. So instead of getting this one entity_id for instance, you'll only get id.

Related