-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathscript.ts
71 lines (62 loc) · 1.73 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
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient();
async function main() {
try {
await prisma.user.findFirstOrThrow({ where: { email: 'alice@prisma.io' } })
} catch {
const consoleMessage = `
User alice@prisma.io not found. Please run the seed script before running this script.
You can run the seed script via the following command:
npx prisma db seed
`;
console.error(consoleMessage);
return
}
// 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 alice@prisma.io)
const newPost = await prisma.post.create({
data: {
title: "Join us for another episode of What's new in Prisma",
content: 'https://youtube.com/playlist?list=PLn2e1F9Rfr6l1B9RP0A9NdX7i7QIWfBa7',
published: false,
author: {
connect: {
email: 'alice@prisma.io',
},
},
},
})
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 alice@prisma.io
const postsByUser = await prisma.user
.findUnique({
where: {
email: 'alice@prisma.io',
},
})
.posts()
console.log(`Retrieved all posts from a specific user: `, postsByUser)
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})