Skip to main content

Create inner join in TypeORM without explicit relations in Enitity

· One min read
Hreniuc Cristian-Alexandru

When you have two tables and you want to create an inner join without having a relation in the entities, you can use the innerJoin method from the createQueryBuilder method like this:

.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)

This works even if you don't have a relation between the two entities.

const notifForCompanyManagers = await dbConnection.manager
.getRepository(NotificationSubscription)
.createQueryBuilder('notification_subscription')
.select('notification_subscription.token')
.addSelect('notification_subscription.userId')
.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)
.where(
'(user_company.manager = true or user_company.owner = true) and user_company.companyId = :companyId',
{ companyId: companyId },
)
.getMany();