This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: ⚡️ optimize courses/instructors by ID (#117)
Co-authored-by: Aponia <[email protected]>
- Loading branch information
Showing
26 changed files
with
213 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lts/hydrogen | ||
lts/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Ambient declaration file for defining "virtual" modules/files. | ||
* The file contents are generated dynamically during build time by esbuild. | ||
* DO NOT add any imports/exports; that converts the file to a regular module | ||
* and removes the global declarations. | ||
*/ | ||
|
||
/** | ||
* Virtual module for caching course information during build time. | ||
*/ | ||
declare module "virtual:courses" { | ||
declare const courses: Record<string, import("@peterportal-api/types").Course>; | ||
} | ||
/** | ||
* Virtual module for caching instructor information during build time. | ||
*/ | ||
declare module "virtual:instructors" { | ||
declare const instructors: Record<string, import("@peterportal-api/types").Instructor>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ApiPropsOverride } from "@bronya.js/api-construct"; | ||
|
||
import { esbuildOptions, constructs } from "../../../../../../bronya.config"; | ||
|
||
export const overrides: ApiPropsOverride = { | ||
esbuild: esbuildOptions, | ||
constructs: { | ||
functionPlugin: constructs.functionPlugin, | ||
restApiProps: constructs.restApiProps, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,20 @@ | ||
import { PrismaClient } from "@libs/db"; | ||
import { createHandler } from "@libs/lambda"; | ||
|
||
import { normalizeCourse } from "../lib"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
async function onWarm() { | ||
await prisma.$connect(); | ||
} | ||
import { courses } from "virtual:courses"; | ||
|
||
export const GET = createHandler(async (event, context, res) => { | ||
const headers = event.headers; | ||
const requestId = context.awsRequestId; | ||
const params = event.pathParameters; | ||
|
||
if (params?.id == null) { | ||
return res.createErrorResult(400, "Course number not provided", requestId); | ||
} | ||
|
||
if (params?.id === "all") { | ||
const courses = await prisma.course.findMany(); | ||
return res.createOKResult(courses.map(normalizeCourse), headers, requestId); | ||
} | ||
const { id } = event.pathParameters ?? {}; | ||
|
||
try { | ||
return res.createOKResult( | ||
normalizeCourse( | ||
await prisma.course.findFirstOrThrow({ | ||
where: { id: decodeURIComponent(params.id) }, | ||
}), | ||
), | ||
headers, | ||
requestId, | ||
); | ||
} catch { | ||
return res.createErrorResult(404, `Course ${params.id} not found`, requestId); | ||
switch (id) { | ||
case null: | ||
case undefined: | ||
return res.createErrorResult(400, "Course number not provided", requestId); | ||
case "all": | ||
return res.createOKResult(Object.values(courses), headers, requestId); | ||
default: | ||
return courses[decodeURIComponent(id)] | ||
? res.createOKResult(courses[decodeURIComponent(id)], headers, requestId) | ||
: res.createErrorResult(404, `Course ${id} not found`, requestId); | ||
} | ||
}, onWarm); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ApiPropsOverride } from "@bronya.js/api-construct"; | ||
|
||
import { esbuildOptions, constructs } from "../../../../../../bronya.config"; | ||
|
||
export const overrides: ApiPropsOverride = { | ||
esbuild: esbuildOptions, | ||
constructs: { | ||
functionPlugin: constructs.functionPlugin, | ||
restApiProps: constructs.restApiProps, | ||
}, | ||
}; |
41 changes: 13 additions & 28 deletions
41
apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,20 @@ | ||
import { PrismaClient } from "@libs/db"; | ||
import { createHandler } from "@libs/lambda"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
async function onWarm() { | ||
await prisma.$connect(); | ||
} | ||
import { instructors } from "virtual:instructors"; | ||
|
||
export const GET = createHandler(async (event, context, res) => { | ||
const headers = event.headers; | ||
const requestId = context.awsRequestId; | ||
const params = event.pathParameters; | ||
|
||
if (params?.id == null) { | ||
return res.createErrorResult(400, "Instructor UCInetID not provided", requestId); | ||
} | ||
|
||
try { | ||
if (params.id === "all") { | ||
const instructors = await prisma.instructor.findMany(); | ||
return res.createOKResult(instructors, headers, requestId); | ||
} | ||
const { id } = event.pathParameters ?? {}; | ||
|
||
return res.createOKResult( | ||
await prisma.instructor.findFirstOrThrow({ | ||
where: { ucinetid: decodeURIComponent(params.id) }, | ||
}), | ||
headers, | ||
requestId, | ||
); | ||
} catch { | ||
return res.createErrorResult(404, `Instructor ${params.id} not found`, requestId); | ||
switch (id) { | ||
case null: | ||
case undefined: | ||
return res.createErrorResult(400, "Instructor UCInetID not provided", requestId); | ||
case "all": | ||
return res.createOKResult(Object.values(instructors), headers, requestId); | ||
default: | ||
return instructors[decodeURIComponent(id)] | ||
? res.createOKResult(instructors[decodeURIComponent(id)], headers, requestId) | ||
: res.createErrorResult(404, `Instructor ${id} not found`, requestId); | ||
} | ||
}, onWarm); | ||
}); |
Oops, something went wrong.