Skip to content

Commit 6f2c850

Browse files
authored
Merge pull request #47 from ZeyadAbbas/db-update
Refactored DB Schema: Added Fundamental Columns to Section and File Tables
2 parents b12c640 + 1ec0a5c commit 6f2c850

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

src/server/db/schema/file.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { relations, sql } from 'drizzle-orm';
2-
import { pgTable, text } from 'drizzle-orm/pg-core';
2+
import {
3+
pgTable,
4+
text,
5+
} from 'drizzle-orm/pg-core';
36
import { user } from './user';
47
import { section } from './section';
58

@@ -15,6 +18,12 @@ export const file = pgTable('file', {
1518
});
1619

1720
export const fileRelations = relations(file, ({ one }) => ({
18-
author: one(user),
19-
section: one(section),
20-
}));
21+
author: one(user, {
22+
fields: [file.authorId],
23+
references: [user.id],
24+
}),
25+
section: one(section, {
26+
fields: [file.sectionId],
27+
references: [section.id],
28+
}),
29+
}));

src/server/db/schema/section.ts

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
import { relations, sql } from 'drizzle-orm';
2-
import { pgTable, text } from 'drizzle-orm/pg-core';
2+
import {
3+
pgTable,
4+
pgEnum,
5+
smallint,
6+
integer,
7+
varchar,
8+
text,
9+
timestamp,
10+
uniqueIndex,
11+
index,
12+
} from 'drizzle-orm/pg-core';
313
import { file } from './file';
414

5-
export const section = pgTable('section', {
6-
id: text('id')
7-
.default(sql`nanoid(20)`)
8-
.primaryKey(),
9-
});
15+
export const termEnum = pgEnum('term', ['Spring', 'Summer', 'Fall']);
16+
17+
export const section = pgTable(
18+
'section',
19+
{
20+
id: varchar('id', { length: 6 })
21+
.default(sql`nanoid(6)`)
22+
.primaryKey(),
23+
24+
// "CS" or "CE", short and indexable
25+
prefix: varchar('prefix', { length: 4 })
26+
.notNull(),
27+
28+
// Course number like 1200
29+
number: varchar('number', { length: 4 })
30+
.notNull(),
31+
32+
// Section code like "001"
33+
sectionCode: varchar('section_code', { length: 3 })
34+
.notNull(),
35+
36+
// Semester split into term + year for better filtering
37+
term: termEnum('term')
38+
.notNull(),
39+
year: smallint('year')
40+
.notNull(),
41+
42+
professor: text('professor'),
43+
numberOfNotes: integer('number_of_notes')
44+
.notNull()
45+
.default(0),
46+
47+
// Not required, but good practice usually
48+
createdAt: timestamp('created_at', { withTimezone: true })
49+
.notNull()
50+
.defaultNow(),
51+
updatedAt: timestamp('updated_at', { withTimezone: true })
52+
.notNull()
53+
.defaultNow(),
54+
},
55+
(t) => ([
56+
uniqueIndex('section_unique_idx').on(t.prefix, t.number, t.sectionCode, t.term, t.year),
57+
index('section_by_course_idx').on(t.prefix, t.number),
58+
index('section_by_professor_idx').on(t.professor),
59+
index('section_by_semester_idx').on(t.term, t.year),
60+
]),
61+
);
1062

1163
export const sectionRelations = relations(section, ({ many }) => ({
1264
files: many(file),
13-
}));
65+
}));

0 commit comments

Comments
 (0)