Search code examples
typeorm

TypeORM - Search and populate Many-to-Many relation


I have two entities Tag and Photo:

// tag.entity.ts

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Tag {
  @PrimaryGeneratedColumn()
  id!: number;

  // Other columns
}
// photo.entity.ts

import { Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn, } from 'typeorm';
import { Tag } from './tag.entity';

@Entity()
export class Photo {
  @PrimaryGeneratedColumn()
  id!: number;

  @ManyToMany(type => Tag, { eager: true })
  @JoinTable()
  tags!: Tag[];

  // Other columns
}

I need to filter results from photos table using results from tags table.

const photos = await photosRepository
  .createQueryBuilder('photo')
  .leftJoinAndSelect('photo.tags', 'tag')
  .where('tag.name LIKE :searchQuery', { searchQuery: 'nature%' })
  .skip(0)
  .take(10)
  .getMany();

The query above works fine: photos table records are filtered using tag.name column from tags table.

The issue is that an each photo entity in returned photos array contains only filtered (tag.name LIKE :searchQuery) tags relation entities. What I need is to eager load all tags relation entities for each photo. Is it possible somehow?

For example, with Laravel's Eloquent it's possible achive what I need with:

$photos = Photo::query()
  ->with('tags')
  ->whereHas('tags', function (Builder $query) {
    $query->where('tags.name', 'like', 'nature%');
  })
  ->skip(0)
  ->take(10)
  ->get();

Solution

  • maybe i found the answer

    const photos = await photosRepository
      .createQueryBuilder('photo')
      .leftJoin('photo.tags', 'tag')
      .leftJoinAndSelect('photo.tags', 'tagSelect')
      .where('tag.name LIKE :searchQuery', { searchQuery: 'nature%' })
      .skip(0)
      .take(10)
      .getMany();
    

    the relative reference: https://github.com/typeorm/typeorm/issues/3731