From a1fbea3e45612f3cc639d1280c6009f1df38b3bc Mon Sep 17 00:00:00 2001 From: static Date: Wed, 22 Jan 2025 23:14:32 +0900 Subject: [PATCH] =?UTF-8?q?=ED=98=84=EC=9E=AC=20=EC=B9=B4=ED=85=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=EC=97=90=20=ED=8C=8C=EC=9D=BC=EC=9D=B4=20?= =?UTF-8?q?=EC=A1=B4=EC=9E=AC=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9C=BC?= =?UTF-8?q?=EB=A9=B4=20=ED=95=98=EC=9C=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EC=97=90=20=EC=9E=88=EB=8A=94=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=EC=9D=B4=20=EA=B0=80=EC=A0=B8=EC=99=80?= =?UTF-8?q?=EC=A7=80=EC=A7=80=20=EC=95=8A=EB=8D=98=20=EB=B2=84=EA=B7=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/server/db/file.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/lib/server/db/file.ts b/src/lib/server/db/file.ts index e108e29..6a53c5b 100644 --- a/src/lib/server/db/file.ts +++ b/src/lib/server/db/file.ts @@ -1,4 +1,4 @@ -import { sql } from "kysely"; +import { sql, type NotNull } from "kysely"; import pg from "pg"; import { IntegrityError } from "./error"; import db from "./kysely"; @@ -299,27 +299,34 @@ export const getAllFilesByCategory = async ( const files = await db .withRecursive("cte", (db) => db - .selectFrom("file") - .innerJoin("file_category", "file.id", "file_category.file_id") - .selectAll("file_category") + .selectFrom("category") + .leftJoin("file_category", "category.id", "file_category.category_id") + .select(["id", "parent_id", "user_id", "file_category.file_id"]) .select(sql`0`.as("depth")) - .where("user_id", "=", userId) - .where("category_id", "=", categoryId) + .where("id", "=", categoryId) .$if(recursive, (qb) => qb.unionAll((db) => db - .selectFrom("file") - .innerJoin("file_category", "file.id", "file_category.file_id") - .innerJoin("category", "file_category.category_id", "category.id") - .innerJoin("cte", "category.parent_id", "cte.category_id") - .selectAll("file_category") - .select(sql`cte.depth + 1`.as("depth")) - .where("file.user_id", "=", userId), + .selectFrom("category") + .leftJoin("file_category", "category.id", "file_category.category_id") + .innerJoin("cte", "category.parent_id", "cte.id") + .select([ + "category.id", + "category.parent_id", + "category.user_id", + "file_category.file_id", + ]) + .select(sql`cte.depth + 1`.as("depth")), ), ), ) .selectFrom("cte") .select(["file_id", "depth"]) + .distinctOn("file_id") + .where("user_id", "=", userId) + .where("file_id", "is not", null) + .$narrowType<{ file_id: NotNull }>() + .orderBy(["file_id", "depth"]) .execute(); return files.map(({ file_id, depth }) => ({ id: file_id, isRecursive: depth > 0 })); };