translaite/prisma/seed.ts

56 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-07 10:52:44 +02:00
import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
const prisma = new PrismaClient();
async function seed() {
2023-08-07 11:00:47 +02:00
const email = "nicola@nzambello.dev";
2023-08-07 10:52:44 +02:00
// cleanup the existing database
await prisma.user.delete({ where: { email } }).catch(() => {
// no worries if it doesn't exist yet
});
2023-08-07 11:00:47 +02:00
const hashedPassword = await bcrypt.hash("nzambello.dev", 10);
2023-08-07 10:52:44 +02:00
const user = await prisma.user.create({
data: {
email,
password: {
create: {
hash: hashedPassword,
},
},
},
});
2023-08-07 11:00:47 +02:00
await prisma.translation.create({
2023-08-07 10:52:44 +02:00
data: {
2023-08-07 11:00:47 +02:00
lang: "italian",
text: "Hello, world!",
result: "Ciao, mondo!",
2023-08-07 10:52:44 +02:00
userId: user.id,
},
});
2023-08-07 11:00:47 +02:00
await prisma.translation.create({
2023-08-07 10:52:44 +02:00
data: {
2023-08-07 11:00:47 +02:00
lang: "spanish",
text: "Hello, world!",
result: "¡Hola Mundo!",
2023-08-07 10:52:44 +02:00
userId: user.id,
},
});
console.log(`Database has been seeded. 🌱`);
}
seed()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});