Skip to content

Commit

Permalink
Seeding some sample quizes
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanguns committed Apr 27, 2021
1 parent c824c06 commit 332fa33
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/common/helpers/shuffle-array.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const shuffleArray = (array: string[] = []): string[] => {
return array
.map((a) => ({sort: Math.random(), value: a}))
.sort((a, b) => a.sort - b.sort)
.map((a) => a.value)
}

export default shuffleArray
4 changes: 2 additions & 2 deletions src/core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ model Question {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
question String
content Answer[]
content String
answers Answer[]
category Category @relation(fields: [categoryId], references: [id])
categoryId String
}
Expand Down
105 changes: 105 additions & 0 deletions src/core/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { PrismaClient, Prisma } from '.prisma/client'
import shuffleArray from '../../common/helpers/shuffle-array.helper'

const jsonData = [
{
"default_size": "https://flagcdn.com/256x192/dz.png",
"mini_size": "https://flagcdn.com/128x96/dz.png",
"correct": "Algeria",
"incorrects": ["Ethiopia", "Afghanistan", "Tanzania"],
"url": "/algeria"
},
{
"default_size": "https://flagcdn.com/256x192/ao.png",
"mini_size": "https://flagcdn.com/128x96/ao.png",
"correct": "Angola",
"incorrects": ["San Marino", "Gambia", "Lithuania"],
"url": "/angola"
},
{
"default_size": "https://flagcdn.com/256x192/bj.png",
"mini_size": "https://flagcdn.com/128x96/bj.png",
"correct": "Benin",
"incorrects": ["Belize", "Slovakia", "DR Congo"],
"url": "/benin"
}
]


const prisma = new PrismaClient()

async function main() {
// await prisma.category.create({
// data: {
// name: 'questions_all_2',
// question: {
// create: [
// {
// content: 'Pregunta 1',
// answers: {
// create: [
// {
// content: 'Respuesta 1',
// isCorrect: false
// },
// {
// content: 'Respuesta 2',
// isCorrect: false
// },
// {
// content: 'Respuesta 3',
// isCorrect: false
// },
// ]
// }
// },
// {
// content: 'Pregunta 2',
// answers: {
// create: [
// {
// content: 'Respuesta 4',
// isCorrect: false
// },
// {
// content: 'Respuesta 5',
// isCorrect: true
// },
// {
// content: 'Respuesta 6',
// isCorrect: false
// },
// ]
// }
// }
// ]
// }
// }
// })
await prisma.category.create({
data: {
name: 'questions_all_2',
question: {
create: jsonData.map(question => ({
content: question.default_size,
answers: {
create: shuffleArray([...question.incorrects, question.correct]).map(answer => ({
content: answer,
isCorrect: answer === question.correct
}))
}
}))
}
}
})
}

main()
.catch(e => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})

0 comments on commit 332fa33

Please sign in to comment.