-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathseed.js
99 lines (85 loc) · 1.59 KB
/
seed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcrypt'
const prisma = new PrismaClient()
async function seed() {
const cohort = await createCohort()
const student = await createUser(
'Testpassword1!',
cohort.id,
'Joe',
'Bloggs',
'Hello, world!',
'student1'
)
const teacher = await createUser(
'Testpassword1!',
null,
'Rick',
'Sanchez',
'Hello there!',
'teacher1',
'TEACHER'
)
await createPost(student.id, 'My first post!')
await createPost(teacher.id, 'Hello, students')
process.exit(0)
}
async function createPost(userId, content) {
const post = await prisma.post.create({
data: {
userId,
content
},
include: {
user: true
}
})
console.info('Post created', post)
return post
}
async function createCohort() {
const cohort = await prisma.cohort.create({
data: {}
})
console.info('Cohort created', cohort)
return cohort
}
async function createUser(
email,
password,
cohortId,
firstName,
lastName,
bio,
githubUrl,
role = 'STUDENT'
) {
const user = await prisma.user.create({
data: {
email,
password: await bcrypt.hash(password, 8),
role,
cohortId,
profile: {
create: {
firstName,
lastName,
bio,
githubUrl
}
}
},
include: {
profile: true
}
})
console.info(`${role} created`, user)
return user
}
seed().catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})