Typing reusable functions for filtering rows

Hi all, we're using Kysely at our company and loving it! We recently encountered some issues when trying to type reusable functions. One example of our current attempt:
function applyFilters<T>({
query,
...args
}: {
query: SelectQueryBuilder<DB, "conversations", T>;
} & ConversationFilters): SelectQueryBuilder<DB, "conversations", T> {
if (args.businessId) {
query = query.where("businessId", "=", args.businessId);
}
...
return query;
}
function applyFilters<T>({
query,
...args
}: {
query: SelectQueryBuilder<DB, "conversations", T>;
} & ConversationFilters): SelectQueryBuilder<DB, "conversations", T> {
if (args.businessId) {
query = query.where("businessId", "=", args.businessId);
}
...
return query;
}
This works when our initial query is basic like this:
let query = getDb().selectFrom("conversations");
let query = getDb().selectFrom("conversations");
However, it stops working when we add some joins (which shouldn't break the original filtering function anyway):
let query = getDb()
.selectFrom("conversations")
.leftJoin(
"contactConversations",
"contactConversations.conversationId",
"conversations.id",
)
let query = getDb()
.selectFrom("conversations")
.leftJoin(
"contactConversations",
"contactConversations.conversationId",
"conversations.id",
)
If we update the SelectQueryBuilder middle type to be "conversations" | "contactConversations", it works with neither the simpler, nor the more "complex" query. I'd be thankful for any pointers and general tips on how to type reusable functions!
3 Replies
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
lukas.slezevicius
Right, thanks! But how do we type the function to accept SelectQueryBuilder in a flexible manner?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server