-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(posting): serve postings from cache (#124)
- Loading branch information
1 parent
2bc02c0
commit e7d9924
Showing
9 changed files
with
121 additions
and
105 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,20 +1,26 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { jobPostingsTable } from '../../db/schema'; | ||
import { type JobPosting } from '../../db/schema'; | ||
import authenticateAdminRequest from '../../utils/admin'; | ||
import { fetchJobPostingFilterSchema } from '~~/shared/schemas/posting'; | ||
|
||
/** | ||
* In future, if totalApplicants needed on posting page from admin side, | ||
* either revamp caching or use database straight away for applicants. | ||
*/ | ||
export default defineEventHandler(async (event) => { | ||
await authenticateAdminRequest(event); | ||
const q = await getValidatedQuery(event, fetchJobPostingFilterSchema.parse); | ||
|
||
const database = await useDatabase(); | ||
const postings = | ||
(await general_memoryStorage.getItem<JobPosting[]>('postings')) || []; | ||
|
||
return ( | ||
await database | ||
.select() | ||
.from(jobPostingsTable) | ||
.where(eq(jobPostingsTable.id, q.id)) | ||
.orderBy(jobPostingsTable.createdAt) | ||
.limit(1) | ||
)[0]; | ||
const posting = postings.find((p) => p.id === q.id); | ||
|
||
if (!posting) { | ||
throw createError({ | ||
statusCode: 404, | ||
statusMessage: 'Posting not found', | ||
}); | ||
} | ||
|
||
return posting; | ||
}); |
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 |
---|---|---|
@@ -1,41 +1,33 @@ | ||
import { jobPostingsTable } from '../../db/schema'; | ||
import { type JobPosting, jobPostingsTable } from '../../db/schema'; | ||
import authenticateAdminRequest from '../../utils/admin'; | ||
import { and, eq, getTableColumns, or } from 'drizzle-orm'; | ||
import { listJobPostingsFilterSchema } from '~~/shared/schemas/posting'; | ||
import { desc } from 'drizzle-orm'; | ||
|
||
/** | ||
* DB lookup for totalApplicants and cache lookup for everything else. | ||
*/ | ||
export default defineEventHandler(async (event) => { | ||
const database = await useDatabase(); | ||
|
||
const session = await authenticateAdminRequest(event); | ||
const q = await getValidatedQuery(event, listJobPostingsFilterSchema.parse); | ||
await authenticateAdminRequest(event); | ||
|
||
const { contents, ...columns } = getTableColumns(jobPostingsTable); | ||
const database = await useDatabase(); | ||
|
||
const conditions = []; | ||
const totalApplicantsRecord = await database | ||
.select({ | ||
id: jobPostingsTable.id, | ||
totalApplicants: jobPostingsTable.totalApplicants, | ||
}) | ||
.from(jobPostingsTable) | ||
.orderBy(desc(jobPostingsTable.createdAt)); | ||
|
||
if (q && q.id) { | ||
conditions.push(eq(jobPostingsTable.id, q.id)); | ||
} else if (q && q.ownerId) { | ||
// If owner specified, just return published postings. | ||
conditions.push( | ||
and( | ||
eq(jobPostingsTable.owner, q.ownerId), | ||
eq(jobPostingsTable.isPublished, true) | ||
) | ||
); | ||
} else { | ||
// If owner not specified, just return published postings + postings by admin himself | ||
conditions.push( | ||
or( | ||
eq(jobPostingsTable.isPublished, true), | ||
eq(jobPostingsTable.owner, session.user.id) | ||
) | ||
); | ||
} | ||
const totalApplicantsById: Record<string, number> = {}; | ||
totalApplicantsRecord.forEach( | ||
(tar) => (totalApplicantsById[tar.id] = tar.totalApplicants) | ||
); | ||
|
||
return database | ||
.select(columns) | ||
.from(jobPostingsTable) | ||
.where(conditions.length ? and(...conditions) : undefined) | ||
.orderBy(jobPostingsTable.createdAt); | ||
const postings = | ||
(await general_memoryStorage.getItem<JobPosting[]>('postings')) || []; | ||
return postings.map((p) => ({ | ||
...p, | ||
contents: null, | ||
totalApplicants: totalApplicantsById[p.id] || 0, | ||
})); | ||
}); |
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,19 +1,15 @@ | ||
import { desc } from 'drizzle-orm'; | ||
import { jobPostingsTable } from '~~/server/db/schema'; | ||
import { type JobPosting } from '~~/server/db/schema'; | ||
import authenticateAdminRequest from '~~/server/utils/admin'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
await authenticateAdminRequest(event); | ||
|
||
const db = await useDatabase(); | ||
const postings = | ||
(await general_memoryStorage.getItem<JobPosting[]>('postings')) || []; | ||
|
||
const postings = await db | ||
.select({ id: jobPostingsTable.id, title: jobPostingsTable.title }) | ||
.from(jobPostingsTable) | ||
.orderBy(desc(jobPostingsTable.createdAt)); | ||
if (IS_DEV) { | ||
console.log('[/api/postings/lite] found', postings.length, 'postings'); | ||
} | ||
|
||
return postings; | ||
return postings.map((p) => ({ id: p.id, title: p.title })); | ||
}); |
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,22 +1,19 @@ | ||
import { eq, getTableColumns } from 'drizzle-orm'; | ||
import { type JobPosting, jobPostingsTable } from '~~/server/db/schema'; | ||
import { type JobPosting } from '~~/server/db/schema'; | ||
|
||
export default defineEventHandler(async (_) => { | ||
const database = await useDatabase(); | ||
|
||
const { contents, owner, isPublished, totalApplicants, ...requiredColumns } = | ||
getTableColumns(jobPostingsTable); | ||
|
||
const postings = await database | ||
.select({ | ||
...requiredColumns, | ||
}) | ||
.from(jobPostingsTable) | ||
.where(eq(jobPostingsTable.isPublished, true)); | ||
const postings = ( | ||
(await general_memoryStorage.getItem<JobPosting[]>('postings')) || [] | ||
) | ||
.filter((p) => p.isPublished) | ||
.map((p) => ({ | ||
...p, | ||
contents: null, | ||
owner: null, | ||
})); | ||
|
||
if (IS_DEV) { | ||
console.log('[PUBLIC] postings page found', postings.length, 'postings.'); | ||
} | ||
|
||
return postings as JobPosting[]; | ||
return postings; | ||
}); |
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