Skip to content

Commit 1da14f0

Browse files
authored
Merge pull request #2553 from flacial/2296-seed-the-database-with-dummy-modules-and-exercises
feat: Seed the database with modules and exercises
2 parents 819db22 + 619bd98 commit 1da14f0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

__dummy__/lessonData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Lesson } from '../graphql'
1+
import { Lesson, Module } from '../graphql'
22

33
const moduleAuthor = {
44
id: 1,
@@ -12,7 +12,7 @@ const moduleAuthor = {
1212
discordAvatarUrl: ''
1313
}
1414

15-
const modules = [
15+
export const modules: Module[] = [
1616
{
1717
id: 1,
1818
name: 'module1',

prisma/seed.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Challenge, Lesson, User } from '@prisma/client'
22
import { Prisma, PrismaClient } from '@prisma/client'
33
import { SubmissionStatus } from '../graphql'
4+
import getExercisesData from '../__dummy__/getExercisesData'
5+
import { modules as modulesData } from '../__dummy__/lessonData'
46
import {
57
adminData,
68
leetData,
@@ -21,6 +23,9 @@ async function main() {
2123
await seedSubmissions(leet, admin, lessons, SubmissionStatus.Passed)
2224
await seedSubmissions(noob, null, lessons.slice(0, 1), SubmissionStatus.Open)
2325
await seedStars(leet, admin, lessons)
26+
await seedModules(admin)
27+
await seedExercises(admin)
28+
await seedExercisesWithoutExplanation(admin)
2429
}
2530

2631
async function seedLessons() {
@@ -31,6 +36,54 @@ async function seedLessons() {
3136
)
3237
}
3338

39+
async function seedModules(admin: User) {
40+
return prisma.$transaction(
41+
modulesData.map(data =>
42+
prisma.module.create({
43+
data: {
44+
content: data.content,
45+
// JS0
46+
lessonId: 1,
47+
authorId: admin.id,
48+
name: data.name,
49+
order: data.order
50+
}
51+
})
52+
)
53+
)
54+
}
55+
56+
async function seedExercises(admin: User) {
57+
return prisma.$transaction(
58+
getExercisesData.exercises.map(data =>
59+
prisma.exercise.create({
60+
data: {
61+
moduleId: modulesData[0].id,
62+
authorId: admin.id,
63+
description: data.description,
64+
answer: data.answer,
65+
explanation: data.answer
66+
}
67+
})
68+
)
69+
)
70+
}
71+
72+
async function seedExercisesWithoutExplanation(admin: User) {
73+
return prisma.$transaction(
74+
getExercisesData.exercises.map(data =>
75+
prisma.exercise.create({
76+
data: {
77+
moduleId: modulesData[1].id,
78+
authorId: admin.id,
79+
description: data.description,
80+
answer: data.answer
81+
}
82+
})
83+
)
84+
)
85+
}
86+
3487
async function seedUsers() {
3588
return prisma.$transaction([
3689
prisma.user.create({ data: adminData }),

0 commit comments

Comments
 (0)