translaite/prisma/seed.ts

56 lines
1.1 KiB
TypeScript

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