| 7 | |
| 8 | // A `main` function so that we can use async/await |
| 9 | async function main() { |
| 10 | const user1Email = `alice${Date.now()}@prisma.io` |
| 11 | const user2Email = `bob${Date.now()}@prisma.io` |
| 12 | |
| 13 | // Seed the database with users and posts |
| 14 | const user1 = await prisma.user.create({ |
| 15 | data: { |
| 16 | email: user1Email, |
| 17 | name: 'Alice', |
| 18 | posts: { |
| 19 | create: { |
| 20 | title: 'Join the Prisma community on Discord', |
| 21 | content: 'https://pris.ly/discord', |
| 22 | published: true, |
| 23 | }, |
| 24 | }, |
| 25 | }, |
| 26 | include: { |
| 27 | posts: true, |
| 28 | }, |
| 29 | }) |
| 30 | const user2 = await prisma.user.create({ |
| 31 | data: { |
| 32 | email: user2Email, |
| 33 | name: 'Bob', |
| 34 | posts: { |
| 35 | create: [ |
| 36 | { |
| 37 | title: 'Check out Prisma on YouTube', |
| 38 | content: 'https://pris.ly/youtube', |
| 39 | published: true, |
| 40 | }, |
| 41 | { |
| 42 | title: 'Follow Prisma on Twitter', |
| 43 | content: 'https://twitter.com/prisma/', |
| 44 | published: false, |
| 45 | }, |
| 46 | ], |
| 47 | }, |
| 48 | }, |
| 49 | include: { |
| 50 | posts: true, |
| 51 | }, |
| 52 | }) |
| 53 | console.log( |
| 54 | `Created users: ${user1.name} (${user1.posts.length} post) and ${user2.name} (${user2.posts.length} posts) `, |
| 55 | ) |
| 56 | |
| 57 | // Retrieve all published posts |
| 58 | const allPosts = await prisma.post.findMany({ |
| 59 | where: { published: true }, |
| 60 | }) |
| 61 | console.log(`Retrieved all published posts: ${JSON.stringify(allPosts)}`) |
| 62 | |
| 63 | // Create a new post (written by an already existing user with email alice@prisma.io) |
| 64 | const newPost = await prisma.post.create({ |
| 65 | data: { |
| 66 | title: 'Join the Prisma Discord community', |