1.7 KiB
1.7 KiB
up:: Boilerplate Code X:: JavaScript tags:: #boilerplate
Updating schema file
after the file has been updated with new schema details, a new migration needs to be run
npx prisma migrate --name init
then we can run the command below to reset and set new seed
How to clear the Database and rerun seeds
npx prisma migrate reset
how to run seed command
before being able to run seed command, you need to enable seeding in the packag.json file add this key/val pair to package json root
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
the --compiler-options {\"module\":\"CommonJS\"}
part is for next.js
now we can run seed command
npx prisma db seed
should work if seed file created already
Boiler-plate seed file
import { PrismaClient, Prisma } from "@prisma/client";
const prisma = new PrismaClient();
const userData: Prisma.UserCreateInput[] = [
{
email: "email@gmail.com",
password: "$2b$04$C/l6kQt/LCNxp4HTR.fZde2mN68ppAyhx7rf1nFAWOza.YqzGNylK",
},
];
async function main() {
console.log(`Start seeding ...`);
for (const u of userData) {
const user = await prisma.user.create({
data: u,
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`);
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});