Skip to content

Commit

Permalink
Merge pull request #237 from UTDNebula/JUP-84-tag-improvements
Browse files Browse the repository at this point in the history
JUP-84 tag improvements
  • Loading branch information
nl32 authored Sep 10, 2024
2 parents d71c990 + 7fd2d73 commit 570b355
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ $$;

Currently, drizzle does not automatically create enums for you. You will have to create them manually. This [link](https://orm.drizzle.team/docs/column-types/pg#enum) should give you a good idea of how to create enums in postgres.

</details> </br>
</details>
<details>
<summary>Tag View</summary>
there is a sql query in `src/server/db/tagView.sql` that you need to run in order for tags to work properly.
This query sets up a view that queries the different tags from clubs and orders them by how many clubs have those tags in descending order.
</details></br>

Once you have added the `DATABASE_URL` to your `.env` file, and have all the necessary extensions as well as enums, you will need to run the following commands to create the tables in your database.

Expand Down
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 570b355

Please sign in to comment.