I want to change the name of a field with my QueryBuilder in the response body i.e a field called id, I want it to output as staff_id, I am having difficulties with that.
import { getRepository, Like } from 'typeorm';
import { RoomEntity } from '@entity/room.entity';
import { HttpException } from '@exceptions/HttpException';
import { isEmpty } from '@utils/util';
import { Room } from '@/interfaces/room.interface';
class RoomService {
public room = RoomEntity;
public async findQueryRoom(): Promise<Room[]> {
const rooms = await getRepository(this.room)
.createQueryBuilder('room')
.select(['room.id as staff_id', 'room.name'])
.getMany();
return rooms;
}
}
export default RoomService;
In the controller, I have:
import { NextFunction, Request, Response } from 'express';
import { Room } from '@interfaces/room.interface';
import RoomService from '@services/room.service';
class RoomsController {
public roomService = new RoomService();
public getRoomsByQuery = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const findAllRoomsData: Room[] = await this.roomService.findQueryRoom();
res.status(200).json({ data: findAllRoomsData, message: 'found all' });
} catch (error) {
next(error);
}
};
}
export default RoomsController;
The response I have in my postman does not include the staff_id field, I see
"data": [
{
"name": "Soba"
}
]
How can I solve this problem?