postgresql - 如何限制 typeorm 连接查询?
问题描述
我是 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();
解决方案
您可以将 .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();
推荐阅读
- angular - Ionic 4绑定输入数据与接口不起作用
- python - 从扫描的文档opencv python中提取内衬表
- css - 可变 CSS 网格布局
- c# - 从 DataGridView 创建一个新的文本文档
- elasticsearch - 在我的 Elastic Search upsert 脚本中,如何引用 upsert 内容?
- c - 使用 pthread_join 的 pthread 同步问题
- selenium - 如何在 TestNG.xml 中按顺序运行多个类?
- asp.net-core - 在 asp net core 中解析实例而不注入
- php - Laravel HasMany 关系未定义属性:Illuminate\Database\Eloquent\Relations\HasMany::$id
- visual-studio-2017 - 有没有办法在 Visual Studio 2017 中查看所有书签的列表?