fix(data-access): 修复 tsc 错误 + 完成 adaptive-practice/elective 迁移至 cacheFn

This commit is contained in:
SpecialX
2026-07-05 22:24:56 +08:00
parent 27374d1b2c
commit 841b130f4c
8 changed files with 320 additions and 136 deletions

View File

@@ -1,6 +1,6 @@
import "server-only"
import { cache } from "react"
import { cacheFn } from "@/shared/lib/cache"
import { createId } from "@paralleldrive/cuid2"
import { and, count, desc, eq, inArray, sql } from "drizzle-orm"
@@ -69,8 +69,7 @@ const serializeRecord = (r: typeof gradeRecords.$inferSelect): GradeRecord => ({
updatedAt: r.updatedAt.toISOString(),
})
export const getGradeRecords = cache(
async (
export const getGradeRecordsRaw = async (
params: GradeQueryParams & {
scope: DataScope
currentUserId?: string
@@ -146,11 +145,22 @@ export const getGradeRecords = cache(
return { records, total }
}
)
export const getGradeRecordById = cache(async (id: string): Promise<GradeRecord | null> => {
export const getGradeRecords = cacheFn(getGradeRecordsRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getGradeRecords"],
})
export const getGradeRecordByIdRaw = async (id: string): Promise<GradeRecord | null> => {
const [row] = await db.select().from(gradeRecords).where(eq(gradeRecords.id, id)).limit(1)
return row ? serializeRecord(row) : null
}
export const getGradeRecordById = cacheFn(getGradeRecordByIdRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getGradeRecordById"],
})
export async function createGradeRecord(
@@ -289,8 +299,7 @@ export async function bulkDeleteGradeRecords(ids: string[]): Promise<number> {
return existingIds.length
}
export const getClassGradeStats = cache(
async (
export const getClassGradeStatsRaw = async (
classId: string,
subjectId?: string,
examId?: string,
@@ -317,7 +326,12 @@ export const getClassGradeStats = cache(
return computeGradeStats(rows)
}
)
export const getClassGradeStats = cacheFn(getClassGradeStatsRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getClassGradeStats"],
})
/**
* P2-4: 计算单个学生在班级中的排名。
@@ -373,8 +387,7 @@ export async function getStudentRankInClass(
return distinctHigherAvgs.size + 1
}
export const getStudentGradeSummary = cache(
async (
export const getStudentGradeSummaryRaw = async (
studentId: string,
scope?: DataScope
): Promise<StudentGradeSummary | null> => {
@@ -468,10 +481,14 @@ export const getStudentGradeSummary = cache(
rank,
}
}
)
export const getClassRanking = cache(
async (
export const getStudentGradeSummary = cacheFn(getStudentGradeSummaryRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getStudentGradeSummary"],
})
export const getClassRankingRaw = async (
classId: string,
subjectId?: string,
examId?: string,
@@ -529,10 +546,14 @@ export const getClassRanking = cache(
return ranked
}
)
export const getClassStudentsForEntry = cache(
async (
export const getClassRanking = cacheFn(getClassRankingRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getClassRanking"],
})
export const getClassStudentsForEntryRaw = async (
classId: string,
scope?: DataScope
): Promise<Array<{ id: string; name: string; email: string }>> => {
@@ -559,10 +580,14 @@ export const getClassStudentsForEntry = cache(
})
.sort((a, b) => a.name.localeCompare(b.name))
}
)
export const getClassGradeStatsWithMeta = cache(
async (
export const getClassStudentsForEntry = cacheFn(getClassStudentsForEntryRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getClassStudentsForEntry"],
})
export const getClassGradeStatsWithMetaRaw = async (
classId: string,
subjectId?: string,
examId?: string,
@@ -601,4 +626,9 @@ export const getClassGradeStatsWithMeta = cache(
studentCount: activeStudentIds.length,
}
}
)
export const getClassGradeStatsWithMeta = cacheFn(getClassGradeStatsWithMetaRaw, {
tags: ["grades"],
ttl: 300,
keyParts: ["grades", "getClassGradeStatsWithMeta"],
})