I'm new to typeorm, maybe someone can resolve my problem. I'm using NestJS and TypeORM and have two tables (categories and talents). I wish to find a solution to limit typeorm join queries.
each category can have many talents in talent_worked and each talent can have many categories in working_categories.
i like to find all categories and there respected talent but i wish to get(limit) only five talents.
Talent:
@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[];
}
Category:
@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[];
}
here is my typeorm query so far:
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();