Unwrap the return type of a Subquery, native jsonAgg support or RQ Join Names?
Hi There,
I am currently investigating Drizzle ORM and seeing if I can bend it to do everything I need 😅
I quickly got into making queries that both return nested JSON properties (User Including Posts Including Comments etc) but also wanting to use the power of SQL to do some summations / transform values etc.
Context:
- My Play Around Repo: https://github.com/JonParton/drizzle-local-playground
- The Schema: https://github.com/JonParton/drizzle-local-playground/blob/master/src/schema.ts
I started with the "new" Drizzle Relational querie's api(https://orm.drizzle.team/docs/rqb) which is awesome for the nested JSON returns, but I was struggeling with how to then dig in deeper if you wanted to say do a summation of values across one of the nested values... I saw the
extras
property was for this but I struggled as the aliases for the joined tables get made automatically and I coudln't find a property to access their names so you end up having to hardcode them:
So after that I started seeing if I could take more control and use the brialliant jsonAgg helpers that @Raphaël Moreau shared in his gist https://gist.github.com/rphlmr/0d1722a794ed5a16da0fdf6652902b1512 Replies
However, I quickly found I needed to start using sub queries to allow easy referencing of values at the top level and allow json nesting within nesting. However, this lead me to then need to use jsonAgg on a sub query and I got stuffed by types:
So I started trying to create a jsonAgg function that could handle a subquery but the figuring out the rigth types to return stumped me and I couldn't find API's to help.
Is there an api or a method someone has created to handle sub queries like this ... basically looking for a
InferModel<SubQuery>
api!
Or if someone has an idea of how I should better go about this please fire away!
For reference I did bomb this old thread (https://discord.com/channels/1043890932593987624/1093581666666156043/1135127584477286530) with my queries too ... Moved here to try and be well behaved!Going to have to forgive me on this. I normally don't store JSON docs in SQL
But I think what you're looking for is JSONb format
I remember you were able to play with that type of stuff in postgres
And JSON has a hard time with searching inside the nested JSON but a format called JSONB could be parsed and indexed
Or . ....I could be making this up I try so hard to stay away from nested JSON
So JSON vs jsonb
JSON = everything from the db view in postgresSQL (I think mySQL also) is viewed as one big string
JSONB is viewed as one big array in a cell that can be indexed for.
Again
So just to be clear, all my Schema is flat normal database tables with relations and no JSON fields - I am just using either relational queries or jsonAgg to make the return from the database include nested elements.
So in terms of typescript I just want to be able to access them via indexes and keys... No need for specific jsonB queries etc.
Ie should be able to do
usersReturn[0].posts[0].comments[0].author_id
for example and have typescript be happy!I appreciate trying to understand your problem more. Because I know I might face it in the future
Bump!...
Hi. Honestly I am at least satisfied, that someone faces the same problem as me. Drizzle seems great for inference from simple queries, but the types of subqueries are a mess and I can't find a solution for that.
@Andrew Sherman - so sorry for the tag.... I know a while back you mentioned to tag you in any threads that got missed when you were snowed under... This is one of those threads!
I guess let's bring @Dan Kochetov here, we can help with types
This type of complex queries are not available yet in the RQB
I'll work on json helpers when I finish mssql implementation
Yep, I think it has to be done with the CRUD API
A little bit too magic for me haha but at some point we just need it works