Reputation: 3904
How does one do the following query using typeorm please
SELECT *
FROM "UserStrength"
RIGHT JOIN "Strength" ON "Strength"."id" = "UserStrength"."strengthId"
Where the UserStrengthEntity is defined as:
@Entity("UserStrength")
export class UserStrengthEntity extends BaseEntity implements IUserStrength {
@Column({default: false}) isCompleted: boolean;
@ManyToOne(type => UserEntity, user => user.userStrengthArray, {
cascadeAll: true
})
@JoinColumn({name: "userId"})
user: UserEntity;
@OneToMany(type => UserStrengthItemEntity, userStrengthItem => userStrengthItem.userStrength, {
cascadeInsert: true,
cascadeUpdate: true
})
userStrengthItemArray?: UserStrengthItemEntity[];
@ManyToOne(type => StrengthEntity, strength => strength.userStrengthArray, {
cascadeAll: true
})
@JoinColumn({name: "strengthId"})
strength: StrengthEntity;
}
and the StrengthEntity is:
@Entity("Strength")
export class StrengthEntity extends BaseEntity implements IStrength {
@Column({length: 50}) name: string;
@Column({length: 100}) title: string;
@Column() strengthType: StrengthType;
@Column({length: 10, nullable: true}) titleColor: string;
@Column({nullable: true}) titleIcon: string;
@ManyToOne(type => ClientEntity, clientEntity => clientEntity.strengthArray, {
cascadeAll: true
})
@JoinColumn({name: "clientId"}) client: ClientEntity
@OneToMany(type => StrengthItemEntity, strengthItem => strengthItem.strength, {
cascadeInsert: true,
cascadeUpdate: true
})
strengthItemArray: StrengthItemEntity[];
@OneToMany(type => UserStrengthEntity, userStrength => userStrength.strength, {
cascadeInsert: true,
cascadeUpdate: true
})
userStrengthArray: UserStrengthEntity[];
}
I want to load a UserStrength which may or may not exist along with the Strength with a given strengthType (I would also need to RIGHT JOIN to the UserEntity to further filter by UserEntity.id)
Could anyone explain how this is accomplished with typeorm please?
Upvotes: 3
Views: 9290
Reputation: 176
Like others wrote there is no right join in TypeOrm, there are only inner (called join) and left join. But right join is nothing else then a left join done the other way. So instead of:
SELECT *
FROM "UserStrength"
RIGHT JOIN "Strength" ON "Strength"."id" = "UserStrength"."strengthId"
You coud use:
SELECT *
FROM Strength
LEFT JOIN UserStrength ON UserStrength.id = Strength.id
The end result will be the same, further more you can then join Users table on already joined UserStrength table. So there is no need for actual right join. Just starting point of query needs to be different. Hope this will be helpful for you.
Upvotes: 2
Reputation: 31
Currently There is No right Join in Typeorm.It's good to establish relation from inverse side and do Left Join
Upvotes: 3