Is there a way to write a query that orders and filters based on an array of possible options?

I'm trying to do some server-side filtering and sorting for a table, and I ended up writing this for the sorting:
const tickets = dbDrizzle.query.ticket.findMany({
orderBy(fields, operators) {
const firstSort = fields[sortBy.data[0]];
const firstDir = operators[sortDirection.data[0].toLowerCase() as 'desc' | 'asc'];
if (sortBy.data.length === 2) {
const secondSort = fields[sortBy.data[1]];
const secondDir = operators[sortDirection.data[1].toLowerCase() as 'desc' | 'asc'];
return and(firstDir(firstSort), secondDir(secondSort));
}

return firstDir(firstSort);
},
});
const tickets = dbDrizzle.query.ticket.findMany({
orderBy(fields, operators) {
const firstSort = fields[sortBy.data[0]];
const firstDir = operators[sortDirection.data[0].toLowerCase() as 'desc' | 'asc'];
if (sortBy.data.length === 2) {
const secondSort = fields[sortBy.data[1]];
const secondDir = operators[sortDirection.data[1].toLowerCase() as 'desc' | 'asc'];
return and(firstDir(firstSort), secondDir(secondSort));
}

return firstDir(firstSort);
},
});
I have typed the sortBy to have the column names for the table in it so when I'm selecting the sort fields it's a union of columns. But the orderBy function errors. Is there a way to do this both for orderBy and where?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server