diff --git a/src/server/api/routers/club.ts b/src/server/api/routers/club.ts index ce62811e..ee13dbdb 100644 --- a/src/server/api/routers/club.ts +++ b/src/server/api/routers/club.ts @@ -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({ @@ -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(['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 []; diff --git a/src/server/db/schema/club.ts b/src/server/db/schema/club.ts index a717e9ef..52f632c4 100644 --- a/src/server/db/schema/club.ts +++ b/src/server/db/schema/club.ts @@ -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'; @@ -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(); diff --git a/src/server/db/tagView.sql b/src/server/db/tagView.sql new file mode 100644 index 00000000..88f626ee --- /dev/null +++ b/src/server/db/tagView.sql @@ -0,0 +1,6 @@ +select distinct UNNEST(tags) as tags, +count(tags) +from +club +group by unnest(tags) +order by count desc