title: How to query a Many-to-Many relation with TypeORM
body: <p><code>Note</code> has a many-to-many relationship to <code>Subject</code></p>
  
  <p><strong>What is the best way to query it?</strong>  I would <em>like</em> to write the following to get all the subjects on a give note:</p>
  
  <pre><code>  const subjectRepo = connection.getRepository(Subject);
    const response = await subjectRepo.find({
      relations: ['notes'],
      where: { note }
    });
  </code></pre>
  
  <p>but that returns ALL of the subjects, not just the subjects on the note.</p>
  
  <p>Reln defined as:</p>
  
  <pre><code>  @ManyToMany(() =&gt; Subject, (subject: Subject) =&gt; subject.notes)
    subjects: Subject[];
  </code></pre>
  
  <p>-- and --</p>
  
  <pre><code>  @ManyToMany(() =&gt; Note, note =&gt; note.subjects)
    @JoinTable()
    notes: Note[];
  </code></pre>
  
  <p>The executed query is:</p>
  
  <pre><code>SELECT "Subject"."id" AS "Subject_id", "Subject"."name" AS "Subject_name", "Subject"."description" AS "Subject_description", "Subject"."createdDate" AS "Subject_createdDate", "Subject"."updatedDate" AS "Subject_updatedDate", "Subject"."notebookId" AS "Subject_notebookId", "Subject"."measurementsId" AS "Subject_measurementsId", "Subject_notes"."id" AS "Subject_notes_id", "Subject_notes"."content" AS "Subject_notes_content", "Subject_notes"."notedAt" AS "Subject_notes_notedAt", "Subject_notes"."createdDate" AS "Subject_notes_createdDate", "Subject_notes"."updatedDate" AS "Subject_notes_updatedDate", "Subject_notes"."notebookId" AS "Subject_notes_notebookId" FROM "subject" "Subject" LEFT JOIN "subject_notes_note" "Subject_Subject_notes" ON "Subject_Subject_notes"."subjectId"="Subject"."id" LEFT JOIN "note" "Subject_notes" ON "Subject_notes"."id"="Subject_Subject_notes"."noteId"
  </code></pre>
  
  <hr>
  
  <p>Note:  you can do this:</p>
  
  <pre><code>  return subjectRepo
      .createQueryBuilder('subject')
      .leftJoin('subject.notes', 'note')
      .where('note.id = :id', { id: note.id })
      .getMany();
  </code></pre>
  
  <p>But I am hoping for an approach with less strings, and explicit joining</p>