-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
101 lines (94 loc) · 2.41 KB
/
script.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
import { PrismaClient, User } from '@prisma/client'
const prisma = new PrismaClient()
// A `main` function so that we can use async/await
async function main() {
// Seed the database with users and posts
const user1 = await prisma.user.create({
data: {
email: '[email protected]',
name: 'Alice',
posts: {
create: {
title: 'Watch the talks from Prisma Day 2019',
content: 'https://www.prisma.io/blog/z11sg6ipb3i1/',
published: true,
},
},
},
include: {
posts: true,
},
})
const user2 = await prisma.user.create({
data: {
email: '[email protected]',
name: 'Bob',
posts: {
create: [
{
title: 'Subscribe to GraphQL Weekly for community news',
content: 'https://graphqlweekly.com/',
published: true,
},
{
title: 'Follow Prisma on Twitter',
content: 'https://twitter.com/prisma/',
published: false,
},
],
},
},
include: {
posts: true,
},
})
console.log(
`Created users: ${user1.name} (${user1.posts.length} post) and ${user2.name} (${user2.posts.length} posts) `,
)
// Retrieve all published posts
const allPosts = await prisma.post.findMany({
where: { published: true },
})
console.log(`Retrieved all published posts: ${allPosts}`)
// Create a new post (written by an already existing user with email [email protected])
const newPost = await prisma.post.create({
data: {
title: 'Join the Prisma Slack community',
content: 'http://slack.prisma.io',
published: false,
author: {
connect: {
email: '[email protected]',
},
},
},
})
console.log(`Created a new post: ${newPost}`)
// Publish the new post
const updatedPost = await prisma.post.update({
where: {
id: newPost.id,
},
data: {
published: true,
},
})
console.log(`Published the newly created post: ${updatedPost}`)
// Retrieve all posts by user with email [email protected]
const postsByUser = await prisma.user
.findUnique({
where: {
email: '[email protected]',
},
})
.posts()
console.log(`Retrieved all posts from a specific user: ${postsByUser}`)
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})