Skip to content

Commit

Permalink
switch tag query to use new tag view
Browse files Browse the repository at this point in the history
  • Loading branch information
nl32 committed Sep 3, 2024
1 parent d71c990 commit 23801ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
14 changes: 5 additions & 9 deletions src/server/api/routers/club.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { z } from 'zod';
import { selectContact } from '@src/server/db/models';
import { clubEditRouter } from './clubEdit';
import { userMetadataToClubs } from '@src/server/db/schema/users';
import { club } from '@src/server/db/schema/club';
import { club, usedTags } from '@src/server/db/schema/club';
import { contacts } from '@src/server/db/schema/contacts';
import { carousel } from '@src/server/db/schema/admin';
const byNameSchema = z.object({
Expand Down Expand Up @@ -119,14 +119,10 @@ export const clubRouter = createTRPCRouter({
}),
distinctTags: publicProcedure.query(async ({ ctx }) => {
try {
const tags = await ctx.db.selectDistinct({ tags: club.tags }).from(club);
const tagSet = new Set<string>(['All']);
tags.forEach((club) => {
club.tags.forEach((tag) => {
tagSet.add(tag);
});
});
return Array.from(tagSet);
const tags = (await ctx.db.select().from(usedTags)).map(
(obj) => obj.tags,
);
return tags;
} catch (e) {
console.error(e);
return [];
Expand Down
14 changes: 13 additions & 1 deletion src/server/db/schema/club.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { relations, sql } from 'drizzle-orm';
import { boolean, pgEnum, pgTable, text } from 'drizzle-orm/pg-core';
import {
boolean,
integer,
pgEnum,
pgTable,
pgView,
text,
} from 'drizzle-orm/pg-core';
import { events } from './events';
import { userMetadataToClubs } from './users';
import { contacts } from './contacts';
Expand Down Expand Up @@ -35,3 +42,8 @@ export const clubRelations = relations(club, ({ many }) => ({
userMetadataToClubs: many(userMetadataToClubs),
carousel: many(carousel),
}));

export const usedTags = pgView('used_tags', {
tags: text('tags').notNull(),
count: integer('count').notNull(),
}).existing();
6 changes: 6 additions & 0 deletions src/server/db/tagView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select distinct UNNEST(tags) as tags,
count(tags)
from
club
group by unnest(tags)
order by count desc

0 comments on commit 23801ba

Please sign in to comment.