title: TypeORM: Select RANDOM() with a nested relation using QueryBuilder
body: <p>Lets say I have these entities:</p>
  <p>User</p>
  <pre><code>@Entity('user', { synchronize: true })
  export class UserEntity {
  
    @PrimaryGeneratedColumn('uuid')
    id: string;
  
    @Column()
    firstName: string;
  
    @Column()
    lastName: string;
  
    @OneToMany(type =&gt; PostEntity, post =&gt; post.user)
    posts: PostEntity[];
  
  }
  </code></pre>
  <p>Post</p>
  <pre><code>@Entity('post', { synchronize: true })
  export class PostEntity {
  
    @PrimaryGeneratedColumn('uuid')
    id: string;
  
    @ManyToOne(type =&gt; UserEntity , user =&gt; user.posts)
    @Index()
    user: UserEntity ;
  
    @Column({ nullable: true })
    text: string;
  }
  </code></pre>
  <p><strong>What I'm trying to get is one random post, together with it's related user, nested inside a user property on the object.</strong></p>
  <p>Using the actual TypeORM repository class I can do that easily:</p>
  <pre><code>  public async getOnePost(): Promise&lt;PostEntity&gt; {
      return await this.findOneOrFail({
        relations: ['user']
      });
  </code></pre>
  <p>This results in a PostEntity object with a user property which is in itself a well structured UserEntity  object.</p>
  <p>But for getting a <strong>random</strong> row from the DB, it seems I have to use TypeORM QueryBuilder. I managed something like this:</p>
  <pre><code>const array = await this.createQueryBuilder()
    .select('*')
    .from(PostEntity , 'post')
    .leftJoinAndSelect('post.user','user')
    .orderBy('RANDOM()')
    .limit(1)
    .execute();
  return array[0];
  </code></pre>
  <p>Playing around with the realtion/join functions and parameters I was only able to get:</p>
  <ol>
  <li>only 1 property &quot;userId&quot; added to PostEntity object</li>
  <li>&quot;userId&quot; property and all other user properties added &quot;flat&quot; to the PostEntity object</li>
  <li>&quot;userId&quot; property added, while all other user properties are added &quot;flat&quot; with an alias: user_id, user_firstName, etc.</li>
  </ol>
  <p>So, using QueryBuilder, how can I get a Post like this:</p>
  <pre><code>{
    id: 'e43c918c-55e1-4511-bce4-910fdd503548',
    text: 'this is some text',
    user: {
      id: '123456789',
      firstName: 'John',
      lastName: 'Doe',
    },
  }
  </code></pre>