title: TypeORM make COUNT query on table which maps two tables together
body: <p>I have two entities <code>Model</code> and <code>Video</code> where <code>Video</code> basically store information about videos and <code>Model</code> store information about model. Because each video can have multiple models and each model can have multiple video entities looks like this:</p>
  <h3>How my entites and tables looks like:</h3>
  <pre><code>// Video Entity
  @Entity()
  export class Video {
    @PrimaryGeneratedColumn()
    id?: number; 
  
    @Column({ charset: 'utf8mb4', collation: 'utf8mb4_unicode_ci' })
    name: string;
  
    @Column({ type: 'text', charset: 'utf8mb4', collation: 'utf8mb4_unicode_ci' })
    description: string;
  
    @ManyToMany((type) =&gt; Model)
    @JoinTable()
    models: Model[];
  }
  
  
  // Model Entity
  @Entity()
  export class Model {
  
    @PrimaryGeneratedColumn()
    id?: number; 
  
    @Column()
    name: string;
  
    @Column()
    code: string;
  }
  </code></pre>
  <p>Because there is relation <code>@ManyToMany</code> between model and video TypeORM also created one extra table for connecting this two. Table name is <code>video_models_model</code> and it looks like this:</p>
  <pre><code>+-----------+---------+
  | videoId   | modelId |
  +===========+=========+
  | 1         | 107     |
  +-----------+---------+
  | 2         | 142     |
  +-----------+---------+
  | 3         | 91      |
  +-----------+---------+
  </code></pre>
  <h3>What I need:</h3>
  <p>Based on modelId I need to find out <code>COUNT()</code> of videos.
  In regular query language it  would be something like:</p>
  <pre><code>SELECT model.*, COUNT(model_videos.videoId) as totalVideos FROM model as model
  LEFT JOIN `video_models_model` `model_videos` ON `model_videos`.`modelId`=`model`.`id` 
  WHERE model.id = 1;
  </code></pre>
  <h3>What I tried:</h3>
  This is how regular query would looks like:
  <pre><code>this.modelRepository
    .createQueryBuilder('model')  
    .where('model.id = :id', { id: id })        
    .getOne();
  </code></pre>
  <p>so what I did was added to <code>Model</code> entity</p>
  <pre><code>  @ManyToMany(type =&gt; Video)
    @JoinTable()
    videos: Video[];
  </code></pre>
  <p>and after that I tried</p>
  <pre><code>this.modelRepository
    .createQueryBuilder('model')
    .leftJoin('model.videos', 'videos')
    .select('COUNT(videos)', 'totalVideos')  
    .where('model.id = :id', { id: id })        
    .getOne();
  </code></pre>
  <p>But it didn't work for me at all pluis it created one additional table named <code>model_videos_video</code> with <code>modelId</code> and <code>videoId</code> columns. So basically duplicate <code>video_models_model</code> table.</p>
  <p>Is there any way how to make that easy query with TypeORM?</p>