Home > other >  Is it possible to get relations with take and skip
Is it possible to get relations with take and skip

Time:10-20

I have an entity named: UserEntity.And i have relations here.Following/Followers.Image: enter image description here

const user = await this.userRepository.findOne({
  where: {
    id: anotherUserId || userId,
    // following: { isDeleted: false }
  },
  select: {
    following: {
      id: true,
      firstName: true,
      lastName: true,
      about: true,
      organizationName: true,
      profileAvatar: true,
      isDeleted: true
    }
  },
  relations: {
    following: true,
  }
})

also i have query where i get relations.How can i take a certain amount?(f.e take 5 followers)Is it possible?Thank u

CodePudding user response:

You should use sub-query for this purpose.

const user = await this.userRepository
  .createQueryBuilder('user')
  .where({ id: anotherUserId || userId })
  .leftJoinAndSelect(
    qb => qb
      .select()
      .from(YourUserRelationTable, 'r')
      .where({ follower: userId })
      .orderBy({ 'r.createdAt': 'DESC' })
      .limit(5),
    'followerRelation',
    'followerRelation.id = user.id'
  )
  .getOne()
  • Related