Reputation: 47
I have the following database structure: 2 table DB structure
The questions table is linked with the answers table through answers.questionId
references questions.id
.
The self-referenced table is referenced through answers.parentId
references answers.id
.
For example, I have the following data:
Questions table:
Answers table
Is it possible to get something like this (by querying with typeor):
[{id: 1, question: "What is my name?", answers: [{ value: "Man" }, { value: "Boss"}] },
{id: 2, question: "Where I am?", answers: [*{ value: "Country", answers: [{ value: "Ro" }, { value: "En" }] }*, {value:"Iland"}]
I want to have the answers self-referenced relations as well.
I tried this:
this.questionsRepo.find({
relations: ['answers'],
});
But obviously I get only the question and answer array
Is it possible to do something like that with an orm like TypeOrm?
Thanks for reading this!
Upvotes: 0
Views: 590
Reputation: 47
I manage to fix it. If somebody have similar issues, here is the code:
this.questionsRepo
.createQueryBuilder('questions')
.leftJoinAndSelect(
'questions.answers',
'answer',
'answer.parentId is NULL',
)
.leftJoinAndSelect('answer.children', 'answer2')
.getMany();
Upvotes: 2