-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tag retrieval in ArticlesPage component to Drizzle (#835)
- Loading branch information
1 parent
d59aca0
commit 5bcb565
Showing
4 changed files
with
21 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import { createTRPCRouter, publicProcedure } from "../trpc"; | ||
import { GetTagsSchema } from "../../../schema/tag"; | ||
import { TRPCError } from "@trpc/server"; | ||
import { post_tag, tag } from "@/server/db/schema"; | ||
import { desc, eq, count } from "drizzle-orm"; | ||
|
||
export const tagRouter = createTRPCRouter({ | ||
get: publicProcedure.input(GetTagsSchema).query(async ({ ctx, input }) => { | ||
get: publicProcedure.query(async ({ ctx }) => { | ||
try { | ||
const count = await ctx.prisma.tag.count({}); | ||
const response = await ctx.prisma.tag.findMany({ | ||
orderBy: { | ||
PostTag: { | ||
_count: "desc", | ||
}, | ||
}, | ||
take: input.take, | ||
}); | ||
const data = await ctx.db | ||
.select({ | ||
title: tag.title, | ||
count: count(tag.title), | ||
}) | ||
.from(tag) | ||
.groupBy(tag.title) | ||
.leftJoin(post_tag, eq(post_tag.tagId, tag.id)) | ||
.limit(10) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JohnAllenTech
Contributor
|
||
.orderBy(desc(count(tag.title))); | ||
|
||
return { data: response, count }; | ||
return { data }; | ||
} catch (error) { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Maybe silly question but should the limit be last here? or will the query optimiser always do it after the orderBy?