TypeORM join custom table result

Viewed 182

I've been studying typeORM and using this tool in some projects. I have a question about joins and tables.

Is it possible to return getMany() instead of getRawMany() from a query that left joins the result of a nested Query? This would allow the result to match the Typescript entity class.

To give some insight, at the moment I have something like this in the function with the queries:

const subquery2 = getConnection()
      .createQueryBuilder()
      .select('c.idProcess', 'idProcess')
      .addSelect('MIN(cd.idAppointment)', 'idAppointment')
      .from('Appointment', 'c')
      .leftJoin('AppointmentDiagnosis', 'cd', 'cd.idAppointment = c.idAppointment')
      .where('cd.code IS NOT NULL')
      .groupBy('c.idProcesso')
      .getQuery();

const subquery1 = getConnection()
      .createQueryBuilder()
      .select('t2.idProcess', 'idProcess')
      .addSelect('MIN(cd.idDiagnosis)', 'appointmentDiagnosticId')
      .from('(' + subquery2 + ')', 't2')
      .leftJoin('appointmentDiagnosis', 'cd', 'cd.idAppointment = t2.idAppointment')
      .groupBy('t2.idProcess')
      .getQuery();

Later on, the subquery1 is used in a left join and the query returns raw response:

 ...
    .leftJoinAndSelect('(' + subquery1 + ')', 't1', 't1.idProcess = pc.idProcess')
 ...
    .getRawMany()

At the moment, I’m getting raw responses, a single JSON object with key: value properties, but what I want is a JSON corresponding to the TypeScript entity, like:

entity: {
  key: value,
  object: {
    key: value
  }
}

And at the moment, the entity only has properties matching SQL columns or direct relations, not relations with "custom joins" like this one.

If something like this is possible to achieve, how can I configure the entity to map these kind of joins?

Note: I’m using NestJS, TypeScript, typeORM and Microsoft SQL.

0 Answers
Related