11import { 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' ;
313import { 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
1163export const sectionRelations = relations ( section , ( { many } ) => ( {
1264 files : many ( file ) ,
13- } ) ) ;
65+ } ) ) ;
0 commit comments