Fetching many to many along with some aggregation using select
I am trying to fetch many to many relation with drizzle select(). I need to use it because I also want to aggregate a value in nested relation ship Here is the schema
Tried several things but could not wrap my head around how to return something like below
11 Replies
Have a look into this https://orm.drizzle.team/docs/rqb
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
If you otherwise want to just use the select functions, you will have to manually group things I believe
@Torbaz when using relational queries can we filter the top level table with fields from child table
Firstly, it would be easier to create your own posts rather than hijacking someone else's with an unrelated question.
To answer your question, though. Not directly. Use the
exists
filter
https://orm.drizzle.team/docs/operators#exists
Example:
Drizzle ORM - Filters
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
As Torbaz pointed out, you can use the relational query builder, but you can't just cut out the intermediate table (BatchToCustomer) from the nested return object that way. To do that, would require constructing your own queries with the SQLite JSON aggregation functions.
https://www.sqlite.org/json1.html
you can use it like:
lmk if that works for you!
im doing some improvements on this function already, might release that as a little drizzle toolkit library later
ok so I made a more reliable verson of that function but it uses lodash’s
get
well discord wont let me paste the code here, so here it goes: https://tsplay.dev/wE2LgN
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
As alternative to lodash.get => https://youmightnotneed.com/lodash#get
You Might Not Need Lodash
A collection of "You might not need ${something}" resources
or something else - found this thread while testing many 2 many relations and currently testing your provided solution.
Question: Whats the reason for the
fullJoin
instead of leftJoin
?
Checked the snippet from freddie after writing the snippet and it seems that his solution is more generic and reusable.Thanks Freddie for adding it to your repo ❤️
https://github.com/fredericoo/drizzle-toolbelt
GitHub
GitHub - fredericoo/drizzle-toolbelt
Contribute to fredericoo/drizzle-toolbelt development by creating an account on GitHub.
btw lodash’s get is well tested and gets bundled with tree shaking so i chose over rolling my own recursive