Skip to content

Commit

Permalink
Refactor tag retrieval in ArticlesPage component to Drizzle (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiallJoeMaher authored Mar 14, 2024
1 parent d59aca0 commit 5bcb565
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
13 changes: 6 additions & 7 deletions app/(app)/articles/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ const ArticlesPage = () => {
},
);

const { status: tagsStatus, data: tagsData } = api.tag.get.useQuery({
take: 10,
});
const { status: tagsStatus, data: tagsData } = api.tag.get.useQuery();

const { ref, inView } = useInView();

Expand All @@ -59,6 +57,7 @@ const ArticlesPage = () => {
fetchNextPage();
}
}, [inView]);

return (
<>
<div className="mx-2">
Expand Down Expand Up @@ -187,14 +186,14 @@ const ArticlesPage = () => {
<div className="flex flex-wrap gap-2">
{tagsStatus === "loading" && <PopularTagsLoading />}
{tagsStatus === "success" &&
tagsData.data.map((tag) => (
tagsData.data.map(({ title }) => (
<Link
key={tag.id}
key={title}
// only reason this is toLowerCase is to make url look nicer. Not needed for functionality
href={`/articles?tag=${tag.title.toLowerCase()}`}
href={`/articles?tag=${title.toLowerCase()}`}
className="border border-neutral-300 bg-white px-6 py-2 text-neutral-900 dark:border-neutral-600 dark:bg-neutral-900 dark:text-neutral-50"
>
{getCamelCaseFromLower(tag.title)}
{getCamelCaseFromLower(title)}
</Link>
))}
</div>
Expand Down
5 changes: 0 additions & 5 deletions schema/tag.ts

This file was deleted.

26 changes: 14 additions & 12 deletions server/api/router/tag.ts
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.

Copy link
@JohnAllenTech

JohnAllenTech Mar 14, 2024

Contributor

Maybe silly question but should the limit be last here? or will the query optimiser always do it after the orderBy?

This comment has been minimized.

Copy link
@JohnAllenTech

JohnAllenTech Mar 14, 2024

Contributor

From testing locally it looks like it does but I only have a small sample size.

.orderBy(desc(count(tag.title)));

return { data: response, count };
return { data };
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
Expand Down
2 changes: 1 addition & 1 deletion server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const tag = pgTable(
{
createdAt: timestamp("createdAt").defaultNow().notNull(),
id: bigserial("id", { mode: "number" }).primaryKey(),
title: varchar("title", { length: 256 }),
title: varchar("title", { length: 256 }).notNull(),
},
(t) => {
return {};
Expand Down

0 comments on commit 5bcb565

Please sign in to comment.