系列: prisma
javascript
41 行
· 更新於 2026-02-03
seed.js
prisma/prisma/seed.js
/**
* Seed script: populate DB with Users, Posts, Comments.
* Idempotence note: This example clears tables each run—OK for teaching, not production.
*/
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function main() {
await prisma.comment.deleteMany();
await prisma.post.deleteMany();
await prisma.user.deleteMany();
const alice = await prisma.user.create({
data: {
email: 'alice@example.com',
name: 'Alice'
},
include: { posts: true }
});
const bob = await prisma.user.create({
data: { email: 'bob@example.com', name: 'Bob' }
});
const publishedPost = await prisma.post.findFirst({ where: { published: true } });
if (publishedPost) {
await prisma.comment.create({
data: {
content: 'Great write-up!',
authorId: bob.id,
postId: publishedPost.id
}
});
}
console.log('Seed complete:', { alice, bob, publishedPost });
}
main()
.catch((e) => { console.error(e); process.exit(1); })
.finally(async () => { await prisma.$disconnect(); });
相關文章
prisma
sql
更新於 2026-02-03
migration.sql
migration.sql — sql source code from the prisma learning materials (prisma/prisma/migrations/20250824120014_init/migration.sql).
閱讀文章 →
prisma
javascript
更新於 2026-02-03
index.js
index.js — javascript source code from the prisma learning materials (prisma/src/index.js).
閱讀文章 →
prisma
javascript
更新於 2026-02-03
errorHandler.js
errorHandler.js — javascript source code from the prisma learning materials (prisma/src/middleware/errorHandler.js).
閱讀文章 →
prisma
javascript
更新於 2026-02-03
prisma.js
prisma.js — javascript source code from the prisma learning materials (prisma/src/prisma.js).
閱讀文章 →
prisma
javascript
更新於 2026-02-03
comments.js
comments.js — javascript source code from the prisma learning materials (prisma/src/routes/comments.js).
閱讀文章 →
prisma
javascript
更新於 2026-02-03
posts.js
posts.js — javascript source code from the prisma learning materials (prisma/src/routes/posts.js).
閱讀文章 →