From 60467f1d7fdc63e8ec3b10ad0d183248cebac4ee Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 17 Mar 2024 15:38:03 +0000 Subject: feature(web): A better tags editor using react select with auto complete and auto create --- packages/trpc/routers/tags.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'packages/trpc') diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts index af11f34c..61b052d9 100644 --- a/packages/trpc/routers/tags.ts +++ b/packages/trpc/routers/tags.ts @@ -1,5 +1,5 @@ import { experimental_trpcMiddleware, TRPCError } from "@trpc/server"; -import { and, eq } from "drizzle-orm"; +import { and, count, eq } from "drizzle-orm"; import { z } from "zod"; import { bookmarks, bookmarkTags, tagsOnBookmarks } from "@hoarder/db/schema"; @@ -93,7 +93,32 @@ export const tagsAppRouter = router({ return { id: res[0].id, name: res[0].name, - bookmarks: res.flatMap((t) => t.bookmarkId ? [t.bookmarkId] : []), + bookmarks: res.flatMap((t) => (t.bookmarkId ? [t.bookmarkId] : [])), }; }), + list: authedProcedure + .output( + z.object({ + tags: z.array( + z.object({ + id: z.string(), + name: z.string(), + count: z.number(), + }), + ), + }), + ) + .query(async ({ ctx }) => { + const tags = await ctx.db + .select({ + id: tagsOnBookmarks.tagId, + name: bookmarkTags.name, + count: count(), + }) + .from(tagsOnBookmarks) + .where(eq(bookmarkTags.userId, ctx.user.id)) + .groupBy(tagsOnBookmarks.tagId) + .innerJoin(bookmarkTags, eq(bookmarkTags.id, tagsOnBookmarks.tagId)); + return { tags }; + }), }); -- cgit v1.2.3-70-g09d2