I have the following unrelated TypeORM entities (for various reasons, I cannot use One/Many relationships on them):
@Entity()
export class Book {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
@Entity()
export class BookCategory {
@PrimaryGeneratedColumn()
id: number;
@Column()
bookId: number;
@Column()
name: string;
}
With a single book, two assigned categories and the following query:
const query = this.bookRepository
.createQueryBuilder('book')
.leftJoinAndSelect(BookCategory, 'category', 'book.id = category.bookId');
return query.execute();
I get the following response:
[
{
"book_id": 1,
"book_name": "Lorem Ipsum",
"category_id": 1,
"category_name": "non-fiction"
},
{
"book_id": 1,
"book_name": "Lorem Ipsum",
"category_id": 2,
"category_name": "documentary"
}
]
As you see, I get 2 objects. Is there any way I can get the following with a TypeORM query?
[
{
"id": 1,
"name": "Lorem Ipsum",
"categories": ["non-fiction", "documentary"]
}
]
I.e. I'm trying to bind a query result to a virtual property categories
. Is that possible?