-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed.ts
179 lines (172 loc) · 4.66 KB
/
seed.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function runDBSeed () {
const user1Email = '[email protected]'
const user1 = await prisma.user.upsert({
where: { email: user1Email },
update: {},
create: {
name: 'Laura',
email: user1Email,
posts: {
create: [
{
title: 'GraphQL Academy #1 - Definition',
isPublished: true,
comments: {
create: [
{
author: { connect: { email: user1Email } },
text: 'Awesome post created by me!'
},
{
author: { connect: { email: user1Email } },
text: 'Well... it could be better'
}
]
}
}
]
}
}
})
console.log(`DB Seed 🌱 - User ${user1.email} created along with posts and comments`)
const user2Email = '[email protected]'
const user2 = await prisma.user.upsert({
where: { email: user2Email },
update: {},
create: {
name: 'Junior',
email: user2Email,
posts: {
create: [
{
title: 'GraphQL Academy #2 - Operations',
isPublished: false
},
{
title: 'GraphQL Academy #3 - Schema Design',
isPublished: false,
comments: {
create: [
{
author: { connect: { email: user2Email } },
text: 'Awesome post created by me!'
},
{
author: { connect: { email: user1.email } },
text: 'Well... mine is better than yours'
}
]
}
}
]
}
}
})
console.log(`DB Seed 🌱 - User ${user2.email} created with posts and comments`)
const user3Email = '[email protected]'
const user3 = await prisma.user.upsert({
where: { email: user3Email },
update: {},
create: {
name: 'Adriana',
email: user3Email,
posts: {
create: [
{
title: 'GraphQL Academy #4 - Directives',
isPublished: true
},
{
title: 'GraphQL Academy #5 - Servers',
isPublished: false,
comments: {
create: [
{
author: { connect: { email: user3Email } },
text: 'Awesome post created by me!'
},
{
author: { connect: { email: user2.email } },
text: 'Well... mine is better than yours'
}
]
}
},
{
title: 'GraphQL Academy #6 - Performance',
isPublished: true,
comments: {
create: [
{
author: { connect: { email: user1.email } },
text: 'Awesome post!'
},
{
author: { connect: { email: user1.email } },
text: 'Could you tell me more about edge cases?'
}
]
}
}
]
}
}
})
console.log(`DB Seed 🌱 - User ${user3.email} created with posts and comments`)
const user4Email = '[email protected]'
const user4 = await prisma.user.upsert({
where: { email: user4Email },
update: {},
create: {
name: 'Luis',
email: user4Email,
posts: {
create: [
{
title: 'GraphQL Academy #7 - N+1 issue',
isPublished: false,
comments: {
create: [
{
author: { connect: { email: user4Email } },
text: 'Awesome post created by me!'
},
{
author: { connect: { email: user3.email } },
text: 'Well... mine is better than yours'
}
]
}
}
]
}
}
})
console.log(`DB Seed 🌱 - User ${user4.email} created with posts and comments`)
const brandName = 'AMAZING BRAND!'
const brand = await prisma.brand.upsert({
where: { name: brandName },
update: {},
create: {
name: brandName,
posts: {
create: [{
title: 'Review of an amazing brand!',
author: { connect: { email: user4.email } }
}]
}
}
})
console.log(`DB Seed 🌱 - Brand ${brand.name} created with post`)
}
runDBSeed()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
console.log('DB Seed 🌱 - Finished!')
await prisma.$disconnect()
})