For example, I have three tables as follows:
User:
- id
- name
Organisation:
- id
- name
OrganisationToUser:
- userId
- organisationIsd
One user has many organisations and one organisation have many users. This is a Planetscale database so no foreign keys. Only virtual relationships. In this case, how can I filter users with their email and organisationId? Here's something I tried:
const userData = await db.query.user.findFirst({
with: {
organisationToUsers: {
with: {
organisation: true,
},
where: eq(organisationToUser.a, 'wrongOrganisationId'),
},
},
where: eq(user.email, ctx.session?.user.email || ''),
});
Unfortunately, this does not work. It only filters the user's existing organisations but still return the user. Any idea how can I get this done?
I spent years with Prisma and forgot to check on this. For anyone who is coming from an ORM like Prisma, Drizzle resembles SQL closely. So to do the above function, the SQL will be:
In drizzle, this will look like: