different `where` but same return type for relational queries?

what's the best way to approach it when i have a fair few relational queries that have different where: clauses, but are otherwise the same (or extremely similar)?
public static async queryPostsByWantsItemId(itemId: number) {
return await db.query.posts.findMany({
/* different v */
where: (post, { eq, and }) => exists(
db.select({}).from(postItemsWants).limit(1).where(
and(
eq(postItemsWants.postId, post.id),
eq(postItemsWants.itemId, itemId)
)
)
),
/* different ^ */
/* same v */
limit: 50,
orderBy: (post, { desc }) => [desc(post.createdAt)],
columns: {
id: true,
description: true,
createdAt: true,
expiresAt: true,
},
with: {
// ...
}
/* same ^ */
});
}
public static async queryPostsByWantsItemId(itemId: number) {
return await db.query.posts.findMany({
/* different v */
where: (post, { eq, and }) => exists(
db.select({}).from(postItemsWants).limit(1).where(
and(
eq(postItemsWants.postId, post.id),
eq(postItemsWants.itemId, itemId)
)
)
),
/* different ^ */
/* same v */
limit: 50,
orderBy: (post, { desc }) => [desc(post.createdAt)],
columns: {
id: true,
description: true,
createdAt: true,
expiresAt: true,
},
with: {
// ...
}
/* same ^ */
});
}
3 Replies
finn
finnOP2y ago
any ideas? if there’s no good way to do this would you mind letting me know either way?
Andrii Sherman
what do you want to approach exactly?
finn
finnOP2y ago
is there a part of my post that didn't make sense? basically i have quite a few queries that return posts but posts are some abstract object that are made up of other columns and relations i.e., the with section so the only difference between all these queries is what's inside the where so essentially i'm wondering how i can avoid repeating exactly the same with like 10 times especially since it's rather long
Want results from more Discord servers?
Add your server