Simple but tricky

Hi all, I'm struggling with this simple use case: contacts have contact_email_addresses (joined on contact.id = contact_email_address.contact_id) when running: const rows = await db.query.contact.findMany({ with: { emailAddresses: true }, }); I get a nicely formed array of contact objects that each also contain an array of contact_email_addresses. Nice! (I can see behind the scenes drizzle is using json_arrayagg and json_array) I now need to get the same result structure but only include contacts where contact_email_address.email like '%something%' Any help much appreciated
4 Replies
Angelelz
Angelelz11mo ago
Drizzle doesn't currently have support for filters on nested relations But you can workaroundit by using a subquery Here is an example on how you can do it: https://discord.com/channels/1043890932593987624/1190608171027398738/1190661834726977686
mattposgate
mattposgateOP10mo ago
aha thanks
Ariel
Ariel10mo ago
@Angelelz piggy-backing off of this, but is there a way in which you can also use a subquery to include aggregate functions in the extras?
Angelelz
Angelelz10mo ago
Sure, you can use a subquery in the extras
Want results from more Discord servers?
Add your server