forked from boolean-uk/team-dev-server-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
159 lines (134 loc) · 2.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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'
)
const post = await createPost(student.id, 'My first post!')
await createPost(teacher.id, 'Hello, students')
await createComment(post.id, student.id, 'Great post!')
await addUsersToCohort(cohort.id, student.id)
process.exit(0)
}
async function createComment(postId, userId, content) {
const comment = await prisma.comment.create({
data: {
postId,
userId,
content
},
include: {
user: true
}
})
console.info('Comment created', comment)
return comment
}
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: {
name: 'Web Development',
startDate: new Date('2014-06-01'),
endDate: new Date('2014-12-01'),
createdAt: new Date(),
updatedAt: new Date()
}
})
console.info('Cohort created', cohort)
return cohort
}
async function addUsersToCohort(cohortId, userId) {
const cohort = await prisma.cohort.update({
where: { id: cohortId },
data: {
users: {
connect: { id: userId }
}
}
})
// Adding cohort to user
const user = await prisma.user.update({
where: { id: userId },
data: {
cohorts: {
connect: { id: cohortId }
}
}
})
console.log('-----')
console.log('Added user: ', user)
console.log('To cohort: ', cohort)
console.log('-----')
return { cohort, user }
}
async function createUser(
email,
password,
cohortId,
firstName,
lastName,
bio,
username,
role = 'STUDENT'
) {
const user = await prisma.user.create({
data: {
email,
password: await bcrypt.hash(password, 8),
role,
cohortId,
profile: {
create: {
firstName,
lastName,
bio,
username,
githubUsername: '',
profilePicture: '',
mobile: ''
}
}
},
include: {
profile: true
}
})
console.info(`${role} created`, user)
return user
}
seed().catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})