Filter Posts by categories?

I'm learning drizzle and I've come to a wall, how do i filter posts by category id bringing posts with all their respective categories? const postsByCategory = await db .select() .from(postsOnCategories) .leftJoin(posts, eq(postsOnCategories.postId, posts.id)) .leftJoin(categories, eq(postsOnCategories.categoryId, categories.id)) .where(eq(categories.id, 3)); This brings all posts that contain the referenced category but i want them with their category relations so i can show them in cards.
5 Replies
Angelelz
Angelelz12mo ago
Have you looked into the relational query builder?
Angelelz
Angelelz12mo ago
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Stingo
Stingo12mo ago
how about u do that server side not db side
Pepo
PepoOP12mo ago
The only workaround i found was selecting from postOncategories and getting the posts ids, then selecting posts using the array i wanted to do it in a single query
strangerSapien
strangerSapien12mo ago
const postsByCategory = await db.select().from(categories)
.leftJoin(postsOnCategories, eq(categories.id, postsOnCategories.categoryId))
.leftJoin(posts, eq(postsOnCategories.postId, posts.id))
.where(eq(categories.id, 3))
const postsByCategory = await db.select().from(categories)
.leftJoin(postsOnCategories, eq(categories.id, postsOnCategories.categoryId))
.leftJoin(posts, eq(postsOnCategories.postId, posts.id))
.where(eq(categories.id, 3))
this will correctly return even there are no posts
Want results from more Discord servers?
Add your server