How to do this query in one step instead of two?

Is there a better way to do this than having two different queries? // gets all posts for a specific user with only a users name passed in. export default async function getAllPosts( userName: string ): Promise<Post[]> { const userResult = await queryDB .select({ id: users.id }) .from(users) .where(eq(users.name, userName)) .limit(1); const results = await queryDB .select() .from(posts) .where(eq(posts.userId, userResult[0].id)) return results; }
1 Reply
Luxaritas
Luxaritas17mo ago
You need to do a join. It’ll look something like: select().from(posts).leftJoin(eq(users.id, posts.userId)).where(eq(users.name, userName)) This is the same semantics as SQL, so you may want to get familiar with some of the basics there, but the idea is that you “attach” (join) the relevant user row to the post row, so that you can then filter the row by the user property
Want results from more Discord servers?
Add your server