【问题标题】:how to limit typeorm join queries?如何限制 typeorm 连接查询?
【发布时间】:2020-12-26 12:43:19
【问题描述】:

我是 typeorm 的新手,也许有人可以解决我的问题。 我正在使用 NestJS 和 TypeORM 并有两个表(类别和人才)。我希望找到一种解决方案来限制 typeorm 连接查询。

talent_worked每个类别可以有多个人才,working_categories每个人才可以有多个类别。 我喜欢找到所有类别并且有受人尊敬的人才,但我希望只获得(限制)五个人才。

人才

@Entity('talents')
@Unique(['mobile'])
export class TalentsEntity extends BaseEntity {

    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({ nullable: true })
    name: string;
    
    @Column({ unique: true })
    mobile: string;
    
    @Column({ nullable: true })
    email: string;
    
    @Column({ select: false })
    password: string;
    
    @Column({ select: false })
    salt: string;
    
    @Column({ default: false })
    isBlocked: boolean;

    @Column({ default: true })    
    isActive: boolean;

    // relation to categories model
    @ManyToMany(
        type => CategoriesEntity,
        categoriesEntity => categoriesEntity.talent_worked,
        { eager: true },
    )
    @JoinTable({ name: 'talents_working_categories' })
    working_categories: CategoriesEntity[];
}

类别

@Entity('categories')
@Unique(['title'])
export class CategoriesEntity extends BaseEntity {

    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({ nullable: true })
    title: string;

    // relation to talents
    @ManyToMany(
        type => TalentsEntity,
        talentsEntity => talentsEntity.working_categories,
        { eager: false },
    )
    talent_worked: TalentsEntity[];

}

这是我目前的 typeorm 查询

const query = await this.createQueryBuilder('category');
query.leftJoinAndSelect('category.talent_worked', 'talent');
query.leftJoinAndSelect('talent.working_categories', 'talentCategories');
query.where('talent.isActive = :isActive AND talent.isBlocked = :isBlocked', { isActive: true, isBlocked: false});
if (categoryId) query.andWhere('category.id = :categoryId', { categoryId });
query.select([
    'category.id',
    'category.title',
    'talent.id',
    'talent.name',
    'talentCategories.id',
    'talentCategories.title',
]);
query.orderBy('category.created_at', 'ASC');
query.addOrderBy('talent.created_at', 'ASC');
return await query.getMany();

【问题讨论】:

标签: postgresql nestjs typeorm


【解决方案1】:

您可以将 .limit(count) 添加到查询中。 您的 typeorm 代码将是

  const query = await this.createQueryBuilder('category');
    query.leftJoinAndSelect('category.talent_worked', 'talent');
    query.leftJoinAndSelect('talent.working_categories', 'talentCategories');
    query.where('talent.isActive = :isActive AND talent.isBlocked = :isBlocked', { isActive: true, isBlocked: false});
    if (categoryId) query.andWhere('category.id = :categoryId', { categoryId });
    query.select([
        'category.id',
        'category.title',
        'talent.id',
        'talent.name',
        'talentCategories.id',
        'talentCategories.title',
    ]);
    query.orderBy('category.created_at', 'ASC');
    query.addOrderBy('talent.created_at', 'ASC');
    query.limit(5);
    return await query.getMany();

【讨论】:

  • 为您的回答提供帮助,但它不起作用。有了这个限制,我们只是限制了类别的数量,但我希望获得所有类别并限制其中的人才。
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 2013-11-02
  • 1970-01-01
  • 2015-03-18
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
相关资源
最近更新 更多