Skip to content

Commit

Permalink
feat(backend): db drizzle schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0nia authored and MinhxNguyen7 committed Oct 24, 2024
1 parent 11bc822 commit 54a0644
Show file tree
Hide file tree
Showing 11 changed files with 805 additions and 117 deletions.
4 changes: 4 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
},
"dependencies": {
"@packages/antalmanac-types": "*",
"@paralleldrive/cuid2": "^2.2.2",
"@trpc/server": "^10.30.0",
"@vendia/serverless-express": "^4.10.1",
"arktype": "1.0.14-alpha",
"aws-lambda": "^1.0.7",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"drizzle-orm": "^0.30.10",
"envalid": "^7.3.1",
"express": "^4.18.2",
"mongodb": "^5.0.1",
"mongoose": "^7.1.0j",
"postgres": "^3.4.4",
"superjson": "^1.12.3",
"websoc-api": "^3.0.0"
},
Expand All @@ -33,6 +36,7 @@
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"concurrently": "^8.0.1",
"drizzle-kit": "^0.21.2",
"esbuild": "^0.17.19",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
Expand Down
6 changes: 6 additions & 0 deletions apps/backend/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

export const client = postgres('postgres://postgres:postgres@localhost:5432/antalmanac');

export const db = drizzle(client);
29 changes: 29 additions & 0 deletions apps/backend/src/db/schema/auth/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { primaryKey, pgTable, text } from 'drizzle-orm/pg-core';

import { user } from './user';

export const account = pgTable(
'account',
{
userId: text('user_id')
.references(() => user.id, { onDelete: 'cascade' })
.notNull(),

providerId: text('provider').notNull(),

providerAccountId: text('provider_account_id').notNull(),

providerAccountCredential: text('provider_account_credential'),

providerType: text('provider_type'),
},
(table) => {
return {
primaryKey: primaryKey({
columns: [table.userId, table.providerId, table.providerAccountId],
}),
};
}
);

export type Account = typeof account.$inferSelect;
3 changes: 3 additions & 0 deletions apps/backend/src/db/schema/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './account'
export * from './session'
export * from './user'
20 changes: 20 additions & 0 deletions apps/backend/src/db/schema/auth/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createId } from '@paralleldrive/cuid2';
import { integer, pgTable, text } from 'drizzle-orm/pg-core';

import { user } from './user';

export const session = pgTable('session', {
id: text('id').primaryKey().$defaultFn(createId),

userId: text('user_id')
.references(() => user.id, { onDelete: 'cascade' })
.notNull(),

expires: integer('expires').notNull(),

status: text('status').default('ACTIVE'),

refreshToken: text('refresh_token').$defaultFn(createId),
});

export type Session = typeof session.$inferSelect;
34 changes: 34 additions & 0 deletions apps/backend/src/db/schema/auth/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createId } from '@paralleldrive/cuid2';
import { integer, pgTable, text } from 'drizzle-orm/pg-core';

/**
* User entity is analogous to a person.
*/
export const user = pgTable('user', {
/**
* Unique ID (CUID) to represent the entity.
*/
id: text('id').primaryKey().$defaultFn(createId),

/**
* Phone number for subscribing to notifications.
*/
phone: text('phone'),

/**
* Display name.
*/
name: text('name'),

/**
* Profile picture..
*/
avatar: text('avatar'),

/**
* Whether the email has been verified.
*/
verified: integer('verified'),
});

export type User = typeof user.$inferSelect;
39 changes: 39 additions & 0 deletions apps/backend/src/db/schema/course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { integer, pgTable, primaryKey, text } from 'drizzle-orm/pg-core';
import { schedule } from './schedule';

/**
* Courses have a N:1 relation with schedules.
*
* Schedules can't have duplicate courses. i.e. courses with the same section code and term.
*
* Once a schedule and its courses have been loaded, additional context can be retrieved
* for the courses by querying PPA with the section code and term.
*/
export const course = pgTable(
'course',
{
scheduleId: text('scheduleId').references(() => schedule.id, { onDelete: 'cascade' }),

/**
* The course's section code.
*/
sectionCode: integer('sectionCode'),

/**
* @example Winter 2024.
*/
term: text('term'),

/**
* Color that the course has when displayed on calendar.
*/
color: text('color'),
},
(table) => {
return {
primaryKey: primaryKey({
columns: [table.scheduleId, table.sectionCode, table.term],
}),
};
}
);
16 changes: 16 additions & 0 deletions apps/backend/src/db/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { integer, pgEnum, pgTable, serial, uniqueIndex, varchar } from 'drizzle-orm/pg-core';

export const user = pgTable(
'user',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
},
);

export const cities = pgTable('cities', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
countryId: integer('country_id').references(() => countries.id),
popularity: popularityEnum('popularity'),
});
29 changes: 29 additions & 0 deletions apps/backend/src/db/schema/schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createId } from '@paralleldrive/cuid2';
import { boolean, pgTable, text } from 'drizzle-orm/pg-core';
import { user } from './auth/user';

export const schedule = pgTable('schedule', {
id: text('id').primaryKey().$defaultFn(createId),

/**
* A schedule is owned by a user.
*/
userId: text('user_id')
.references(() => user.id, { onDelete: 'cascade' })
.notNull(),

/**
* Whether this schedule was the most recently focused.
*/
active: boolean('active'),

/**
* Name of the schedule.
*/
name: text('name'),

/**
* Any custom notes.
*/
notes: text('notes'),
});
30 changes: 30 additions & 0 deletions apps/backend/src/db/schema/subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { integer, pgTable, primaryKey, text } from 'drizzle-orm/pg-core';
import { user } from './auth/user';

export const subscription = pgTable(
'subscription',
{
/**
* The user that is subscribing to course updates for the specified section.
*/
userId: text('userId').references(() => user.id, { onDelete: 'cascade' }),

/**
* Section code.
*/
sectionCode: integer('sectionCode'),

/**
* @example "OPEN" could indicate that the user wants to be notified when this
* section changes from "WAITLISTED" to "OPEN".
*/
status: text('status'),
},
(table) => {
return {
primaryKey: primaryKey({
columns: [table.userId, table.sectionCode],
}),
};
}
);
Loading

0 comments on commit 54a0644

Please sign in to comment.