|
| 1 | +import { prisma } from "../database/client"; |
| 2 | +import { |
| 3 | + courses, |
| 4 | + enrollments, |
| 5 | + interest, |
| 6 | + slots, |
| 7 | + subjects, |
| 8 | + users, |
| 9 | +} from "./test-data/data"; |
| 10 | + |
| 11 | +async function main() { |
| 12 | + await Promise.all( |
| 13 | + subjects.map(async ({ group, subjects }) => { |
| 14 | + for (const [id, name] of subjects) { |
| 15 | + await prisma.interestSubject.upsert({ |
| 16 | + where: { id }, |
| 17 | + update: { name, group }, |
| 18 | + create: { id, name, group }, |
| 19 | + }); |
| 20 | + } |
| 21 | + }), |
| 22 | + ); |
| 23 | + |
| 24 | + await Promise.all( |
| 25 | + users.map(async (user) => { |
| 26 | + await prisma.user.upsert({ |
| 27 | + where: { id: user.id }, |
| 28 | + update: {}, |
| 29 | + create: user, |
| 30 | + }); |
| 31 | + }), |
| 32 | + ); |
| 33 | + |
| 34 | + await Promise.all( |
| 35 | + interest.map(async (interest) => { |
| 36 | + await prisma.interest.upsert({ |
| 37 | + where: { |
| 38 | + userId_subjectId: interest, |
| 39 | + }, |
| 40 | + update: interest, |
| 41 | + create: interest, |
| 42 | + }); |
| 43 | + }), |
| 44 | + ); |
| 45 | + |
| 46 | + await Promise.all( |
| 47 | + courses.map(async (course) => { |
| 48 | + await prisma.course.upsert({ |
| 49 | + where: { id: course.id }, |
| 50 | + update: course, |
| 51 | + create: course, |
| 52 | + }); |
| 53 | + }), |
| 54 | + ); |
| 55 | + |
| 56 | + await Promise.all( |
| 57 | + slots.map(async (slot) => { |
| 58 | + await prisma.slot.upsert({ |
| 59 | + where: { |
| 60 | + courseId_period_day: { |
| 61 | + courseId: slot.courseId, |
| 62 | + period: slot.period, |
| 63 | + day: slot.day, |
| 64 | + }, |
| 65 | + }, |
| 66 | + update: slot, |
| 67 | + create: slot, |
| 68 | + }); |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + const promises = enrollments.map(async ([user, course]) => { |
| 73 | + await prisma.enrollment.upsert({ |
| 74 | + where: { |
| 75 | + userId_courseId: { userId: user, courseId: course }, |
| 76 | + }, |
| 77 | + update: {}, |
| 78 | + create: { |
| 79 | + userId: user, |
| 80 | + courseId: course, |
| 81 | + }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + await Promise.all(promises); |
| 85 | +} |
| 86 | + |
| 87 | +await main() |
| 88 | + .then(async () => { |
| 89 | + await prisma.$disconnect(); |
| 90 | + }) |
| 91 | + .catch(async (e) => { |
| 92 | + console.error(e); |
| 93 | + await prisma.$disconnect(); |
| 94 | + process.exit(1); |
| 95 | + }); |
0 commit comments