I'm working with typeORM with nestJS.
I have bi-directional many-to-many relationship between 2 tables: A and B (means a entity of A can be assign to many entities of B, and vice versa)
A.entity.ts:
import { Entity, Column, PrimaryGeneratedColumn, ManyToMany, JoinTable } from 'typeorm';
import { B } from './B.entity';
@Entity('A')
export class A {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(() => B,
(b)=>(b.AObjects))
BObjects: B[];
}
B.entity.ts:
import { Entity, Column, PrimaryGeneratedColumn, ManyToMany, JoinTable } from 'typeorm';
import { A} from './A.entity';
@Entity('B')
export class B{
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(() => A,
(a) => a.BObjects,
{eager:true})
@JoinTable({
name: 'AB',
inverseJoinColumn : { name: 'Aid', referencedColumnName: 'id'},
joinColumn: { name: 'Bid', referencedColumnName: 'id'},
})
AObjects: A[];
}
In the module service I want to implement a function, which receives a given id of B entity, and retreive all A's objects which refers to B's id
I want to write a typeORM query which implements the following sql query, for a given_B_id (which will be supplied as a parameter):
SELECT A.*
from A, AB
where AB.Bid = given_B_id and A.id = AB.Aid
Will appreciate your help