Help with Join query
Hi all, I have this three tables:
users (id, name, email)
account (id, username, public)
userAccounts (user_id, account_id, role)
I would like to get a query that for a given account it gives me the account data and all the managers that this account have. I have archived this via two queries, but I would like to do it in just one DB call.
The expected result:
[
{
username
public
managers: {
name,
email
role,
}
}
]
2 Replies
You can use db.query.table and map the result and put the names you like for c/key. I assume you already have the tables related in your schema https://orm.drizzle.team/docs/rqb#select-filters with npx drizzle-kit introspect you can generate all relations auto
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Makes sense, so I'm gonna make a query with two inner joins, so I have the tree tables information in the result. This results in something like:
Then what I understood is that i can use the map method to convert this array into an object with the common values and a new object "managers" that contains an array of the variable data. What I have done is the following:
And this results in:
That's the data that I need. Do you think this is the best approach or you think there is an optimal way doing this?
Thanks a lot for your time.