From baaa5bd80a76af7d8fd7f1adb6e9d4696bca62d0 Mon Sep 17 00:00:00 2001 From: Gabriel Bianchi Date: Sat, 11 May 2024 02:21:03 -0300 Subject: [PATCH] Avoid duplicated sql expressions (#327) * Avoid duplicated sql expressions * Fixerino capuccino --- src/app/_lib/queries.ts | 90 ++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 60 deletions(-) diff --git a/src/app/_lib/queries.ts b/src/app/_lib/queries.ts index 8fb459a1..19419c4e 100644 --- a/src/app/_lib/queries.ts +++ b/src/app/_lib/queries.ts @@ -4,7 +4,7 @@ import { unstable_noStore as noStore } from "next/cache" import { db } from "@/db" import { tasks, type Task } from "@/db/schema" import type { DrizzleWhere } from "@/types" -import { and, asc, count, desc, gte, lte, or } from "drizzle-orm" +import { and, asc, count, desc, gte, lte, or, type SQL } from "drizzle-orm" import { filterColumn } from "@/lib/filter-column" @@ -30,66 +30,36 @@ export async function getTasks(input: GetTasksSchema) { const fromDay = from ? new Date(from) : undefined const toDay = to ? new Date(to) : undefined + const expressions: (SQL | undefined)[] = [ + title + ? filterColumn({ + column: tasks.title, + value: title, + }) + : undefined, + // Filter tasks by status + !!status + ? filterColumn({ + column: tasks.status, + value: status, + isSelectable: true, + }) + : undefined, + // Filter tasks by priority + !!priority + ? filterColumn({ + column: tasks.priority, + value: priority, + isSelectable: true, + }) + : undefined, + // Filter by createdAt + fromDay && toDay + ? and(gte(tasks.createdAt, fromDay), lte(tasks.createdAt, toDay)) + : undefined, + ] const where: DrizzleWhere = - !operator || operator === "and" - ? and( - // Filter tasks by title - title - ? filterColumn({ - column: tasks.title, - value: title, - }) - : undefined, - // Filter tasks by status - !!status - ? filterColumn({ - column: tasks.status, - value: status, - isSelectable: true, - }) - : undefined, - // Filter tasks by priority - !!priority - ? filterColumn({ - column: tasks.priority, - value: priority, - isSelectable: true, - }) - : undefined, - // Filter by createdAt - fromDay && toDay - ? and(gte(tasks.createdAt, fromDay), lte(tasks.createdAt, toDay)) - : undefined - ) - : or( - // Filter tasks by title - title - ? filterColumn({ - column: tasks.title, - value: title, - }) - : undefined, - // Filter tasks by status - !!status - ? filterColumn({ - column: tasks.status, - value: status, - isSelectable: true, - }) - : undefined, - // Filter tasks by priority - !!priority - ? filterColumn({ - column: tasks.priority, - value: priority, - isSelectable: true, - }) - : undefined, - // Filter by createdAt - fromDay && toDay - ? and(gte(tasks.createdAt, fromDay), lte(tasks.createdAt, toDay)) - : undefined - ) + !operator || operator === "and" ? and(...expressions) : or(...expressions) // Transaction is used to ensure both queries are executed in a single transaction const { data, total } = await db.transaction(async (tx) => {