My database consists of the following entities:
@Entity
class User {
@ManyToMany(type => Group)
@JoinTable()
groups: Group[];
}
@Entity
class MediaObject {
@ManyToMany(type => Group)
@JoinTable()
groups: Group[];
}
@Entity
class Group {
// [...]
}
Now, I am looking to retrieve all MediaObjects that share at least one group with a specific User.
For instance, consider the following scenario:
User 1 MediaObject 1
-----------------------------
Group 1 |--- Group 2
Group 2 ---| Group 3
User 1 has at least one common group with MediaObject
How can I construct a SQL query to achieve this? I am utilizing Typeorm for my queries, so any SQL query or insight into how Typeorm performs table joins would be greatly appreciated.
LEFT JOIN "group" "groups" ON "groups"."id" = "media_groups"."groupId"