I have two entities: Something and Other (prototypes are below).
How can I use In() method to find all Somethings that have related Others by array of Others ids?
I need something like this:
const smths: Array<Something> = await connection.getRepository(Something).find({
others: In(arrayWithOthersIds),
});
Entities:
@Entity()
class Something {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany(() => Other, (other) => other.smths, { cascade: true })
others: Other[];
}
@Entity()
class Other {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany(() => Something, (smth) => smth.others, { cascade: true })
smths: Something[];
}
You must use @JoinTable() from 1 side of your many-to-many relationship. Also as I know you can not use { cascade: true } on both sides, so remove one. And use this query:
const smths: Something[] = await connection
.getRepository(Something)
.createQueryBuilder('smth')
.innerJoin("smth.others", "others")
.where('others.id IN (:...ids)', { ids } ) // ids = [1, 2..]
.getMany();