#How to Join Table without entity on using QueryBuilder.

1 messages · Page 1 of 1 (latest)

zealous roost
#

This is what i came up with

const result = await this.examTemplateRepo
    .createQueryBuilder("examTemplate")
    .leftJoinAndSelect("examTemplate.Questions", "questions")
    .innerJoin("CurriculumModels", "curriculum", "curriculum.Id = examTemplate.CurriculumId")
    .select([
        "examTemplate.Id",
        "examTemplate.CurriculumId",
        "curriculum.Id",
        "curriculum.Name",
        "questions.Id",
        "questions.QuestionTypeId",
        "questions.NoOfQuestions",
    ])
    .getMany();

but the result is

[
    {
    "Id": 1,
    "CurriculumId": 1,
    "Questions": [
      {
        "Id": 1,
        "QuestionTypeId": 1,
        "NoOfQuestions": 10
      },
      {
        "Id": 2,
        "QuestionTypeId": 4,
        "NoOfQuestions": 10
      }
    ]
  }
]

"curriculum.Id", "curriculum.Name", are being hydrated by TypeORM how can i handle this?
result is type of ExamTemplate so i cant exactly loop through the result and re-hydrate the data.
Any advice or suggestions would be greatly appreciated!

#

I could use RawQuery, but then i would need to loop through data manually, which is what i'm trying to avoid, if possible, RawQuery Would be like

SELECT ETM.Id, ETM.CurriculumId, ETQM.NoOfQuestions, ETQM.QuestionTypeId
FROM ExamTemplateModel ETM
         INNER JOIN ExamTemplateQuestionModel ETQM ON ETQM.ExamTemplateId = ETM.Id
| ID | CurriculumId | NoOfQuestions | QuestionTypeId |
|----|--------------|---------------|-----------------|
| 1  | 1            | 10            | 1               |
| 1  | 1            | 10            | 4               |
| 2  | 1            | 10            | 1               |
dire igloo
#

this innerJoin is weird, shoudl look like leftJoin with syntax

zealous roost
#

Cause i do not have entities, so i went with Raw Query.