How to use db.query.comments.findFirst with WHERE on comments.post.auhor.name?
Using the schema example from the doc: https://orm.drizzle.team/docs/rqb#select-filters
I'm trying to find the nicest way to write
db.query.comments.findFirst
for:
Find a comment by specific comment.text AND specific comment.post.author.name ?
Schema:
So the question is, is this query possible with drizzle and usedb.query.comments.findFirst
for:
Find a comment by specific comment.text AND specific comment.post.author.name ?
I'm aware comment's text and author (user) name are not unique like user id or comment Id, let's just ignore that for now.Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
8 Replies
I can think of something like this but got stuck from here
Querying by nested tables in the relational query builder isn't currently easy.
I see, thank you so much for help @Sillvva !!!
I think the first solution is nicer to write than the alternative solution. I think the alternative solution has an issue if it gets even more complicated.
Let's say there's
likes
table, one-to-many to comments
, with userId
and commentId
. Now if I want to pull likes
as well.
The first approach is pretty easy to modify.
Second approach is probably harder, you probably have to do join with likes table, and then reconstruct the object to make it return the object similarly to the first approach.
Hopefully there are more features coming for this in the future, again, thank you so much!RQB (relational query builder) is better for one-to-many and many-to-many for sure, because of the structure of the output type. For one-to-one and many-to-one, it's pretty much the same.
Yeah exactly, thank you so much for the help! I've heard there's new ideas for RQB going on for next major versions, excited to see what they come up with!
Ah found it
GitHub
Relational API v2 · drizzle-team drizzle-orm · Discussion #2316
We've launched Drizzle Relational Queries exactly a year ago and it's time to ship a fundamental upgrade. We've gathered a massive amount of valuable feedback from the community and goi...
Yep. I'm following it 🙂