title: TypeORM: Insert an entity which has many to many relation with another entity (based on existing records)
body: <p>I have 2 following entities with Many To Many relationship</p>
  <ul>
  <li>User entity (relationship owned by the User entity)</li>
  </ul>
  <pre class="lang-js prettyprint-override"><code>import { Entity, Column, PrimaryGeneratedColumn, UpdateDateColumn, ManyToMany, JoinTable, CreateDateColumn } from 'typeorm';
  import { Role } from './role.schema';
  
  @Entity('Users')
  export class User {
  
    @PrimaryGeneratedColumn({ name: 'Id' })
    id: number;
  
    @Column({
      name: 'Email',
      length: 100,
      unique: true
    })
    email: string;
  
    @Column({
      name: 'FirstName',
      length: 30
    })
    firstName: string;
  
    @Column({
      name: 'LastName',
      length: 30
    })
    lastName: string;
  
    @ManyToMany(type =&gt; Role, { eager: true })
    @JoinTable({
      name: 'UserRoles',
      joinColumns: [
        { name: 'UserId' }
      ],
      inverseJoinColumns: [
        { name: 'RoleId' }
      ]
    })
    roles: Role[];
  }
  </code></pre>
  <ul>
  <li>Role entity (with two existing roles: Admin and User)</li>
  </ul>
  <pre class="lang-js prettyprint-override"><code>import { Entity, Column, PrimaryGeneratedColumn, ManyToMany } from 'typeorm';
  import { User } from './user.schema';
  
  @Entity(&quot;Roles&quot;)
  export class Role {
    @PrimaryGeneratedColumn({ name: 'Id' })
    id: number;
  
    @Column({
      name: 'Name',
      length: 50,
      unique: true
    })
    name: string;
  
    @ManyToMany(type =&gt; User, user =&gt; user.roles)
    users: User[];
  }
  </code></pre>
  <p>the design of application is such that Role name is sent in request in the form of an array e.g. ['Admin', 'User']</p>
  <p>Now while inserting a User,<br/>
  currently I first retrieve Role object from database based on the role name array received in request (to assign a desired role),<br/>
  then assign it to the User object (roles property) and<br/>
  then finally call save method on User object to insert the record in User table.</p>
  <p><strong>Snippet:</strong></p>
  <pre class="lang-js prettyprint-override"><code>createConnection(connectionConfig as ConnectionOptions).then(async connect =&gt; {
  
    let userRepo = connect.getRepository(User);
    let roleRepo = connect.getRepository(Role);
    let roles = ['Admin', 'User'];
    let user = userRepo.create();
  
    return roleRepo.createQueryBuilder('role').where('role.name IN (:roleNames)', { roleNames: roles }).getMany().then((roles) =&gt; {
      user.email = 'test1@test.test';
      user.firstName = 'TestFName';
      user.lastName = 'TestLName';
      user.roles = roles;
      return userRepo.save(user)
    })
  
  }).catch(error =&gt; {
    console.log(error);
  });
  </code></pre>
  <p>This results in many database calls. It would be great if some one can enlighten me with smarter and more elegant way ( using fluent QueryBuilder to achieve above result)</p>