Module Update
This commit is contained in:
171
src/shared/db/relations.ts
Normal file
171
src/shared/db/relations.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import {
|
||||
users,
|
||||
accounts,
|
||||
sessions,
|
||||
roles,
|
||||
usersToRoles,
|
||||
questions,
|
||||
knowledgePoints,
|
||||
questionsToKnowledgePoints,
|
||||
textbooks,
|
||||
chapters,
|
||||
exams,
|
||||
examQuestions,
|
||||
examSubmissions,
|
||||
submissionAnswers
|
||||
} from "./schema";
|
||||
|
||||
// --- Users & Roles Relations ---
|
||||
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
accounts: many(accounts),
|
||||
sessions: many(sessions),
|
||||
usersToRoles: many(usersToRoles),
|
||||
createdExams: many(exams),
|
||||
submissions: many(examSubmissions),
|
||||
authoredQuestions: many(questions),
|
||||
}));
|
||||
|
||||
export const accountsRelations = relations(accounts, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [accounts.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const sessionsRelations = relations(sessions, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [sessions.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const rolesRelations = relations(roles, ({ many }) => ({
|
||||
usersToRoles: many(usersToRoles),
|
||||
}));
|
||||
|
||||
export const usersToRolesRelations = relations(usersToRoles, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [usersToRoles.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
role: one(roles, {
|
||||
fields: [usersToRoles.roleId],
|
||||
references: [roles.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
// --- Questions Relations (Self-Reference & KP) ---
|
||||
|
||||
export const questionsRelations = relations(questions, ({ one, many }) => ({
|
||||
author: one(users, {
|
||||
fields: [questions.authorId],
|
||||
references: [users.id],
|
||||
}),
|
||||
// Self-Reference: Parent Question
|
||||
parent: one(questions, {
|
||||
fields: [questions.parentId],
|
||||
references: [questions.id],
|
||||
relationName: "question_hierarchy",
|
||||
}),
|
||||
// Self-Reference: Child Questions
|
||||
children: many(questions, {
|
||||
relationName: "question_hierarchy",
|
||||
}),
|
||||
|
||||
// Many-to-Many with Knowledge Points
|
||||
questionsToKnowledgePoints: many(questionsToKnowledgePoints),
|
||||
|
||||
// Usage in Exams
|
||||
examQuestions: many(examQuestions),
|
||||
}));
|
||||
|
||||
export const knowledgePointsRelations = relations(knowledgePoints, ({ one, many }) => ({
|
||||
// Tree Structure
|
||||
parent: one(knowledgePoints, {
|
||||
fields: [knowledgePoints.parentId],
|
||||
references: [knowledgePoints.id],
|
||||
relationName: "kp_hierarchy",
|
||||
}),
|
||||
children: many(knowledgePoints, {
|
||||
relationName: "kp_hierarchy",
|
||||
}),
|
||||
|
||||
questionsToKnowledgePoints: many(questionsToKnowledgePoints),
|
||||
}));
|
||||
|
||||
export const questionsToKnowledgePointsRelations = relations(questionsToKnowledgePoints, ({ one }) => ({
|
||||
question: one(questions, {
|
||||
fields: [questionsToKnowledgePoints.questionId],
|
||||
references: [questions.id],
|
||||
}),
|
||||
knowledgePoint: one(knowledgePoints, {
|
||||
fields: [questionsToKnowledgePoints.knowledgePointId],
|
||||
references: [knowledgePoints.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
// --- Academic Relations ---
|
||||
|
||||
export const textbooksRelations = relations(textbooks, ({ many }) => ({
|
||||
chapters: many(chapters),
|
||||
}));
|
||||
|
||||
export const chaptersRelations = relations(chapters, ({ one, many }) => ({
|
||||
textbook: one(textbooks, {
|
||||
fields: [chapters.textbookId],
|
||||
references: [textbooks.id],
|
||||
}),
|
||||
parent: one(chapters, {
|
||||
fields: [chapters.parentId],
|
||||
references: [chapters.id],
|
||||
relationName: "chapter_hierarchy",
|
||||
}),
|
||||
children: many(chapters, {
|
||||
relationName: "chapter_hierarchy",
|
||||
}),
|
||||
}));
|
||||
|
||||
export const examsRelations = relations(exams, ({ one, many }) => ({
|
||||
creator: one(users, {
|
||||
fields: [exams.creatorId],
|
||||
references: [users.id],
|
||||
}),
|
||||
questions: many(examQuestions),
|
||||
submissions: many(examSubmissions),
|
||||
}));
|
||||
|
||||
export const examQuestionsRelations = relations(examQuestions, ({ one }) => ({
|
||||
exam: one(exams, {
|
||||
fields: [examQuestions.examId],
|
||||
references: [exams.id],
|
||||
}),
|
||||
question: one(questions, {
|
||||
fields: [examQuestions.questionId],
|
||||
references: [questions.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const examSubmissionsRelations = relations(examSubmissions, ({ one, many }) => ({
|
||||
exam: one(exams, {
|
||||
fields: [examSubmissions.examId],
|
||||
references: [exams.id],
|
||||
}),
|
||||
student: one(users, {
|
||||
fields: [examSubmissions.studentId],
|
||||
references: [users.id],
|
||||
}),
|
||||
answers: many(submissionAnswers),
|
||||
}));
|
||||
|
||||
export const submissionAnswersRelations = relations(submissionAnswers, ({ one }) => ({
|
||||
submission: one(examSubmissions, {
|
||||
fields: [submissionAnswers.submissionId],
|
||||
references: [examSubmissions.id],
|
||||
}),
|
||||
question: one(questions, {
|
||||
fields: [submissionAnswers.questionId],
|
||||
references: [questions.id],
|
||||
}),
|
||||
}));
|
||||
Reference in New Issue
Block a user