What is seeding?

Seeding allows me to add some dummy-data into my database. With this nonsense data, I can verify whether my application works the way its intended to or not. In case of a data lost, I don’t need to worry, since the data was not important and I can seed the database any time again.


How do i seed?

Seeding is simple and straight forward, Prisma themself documented this process pretty well, so I just can follow their guide:

Seeding Customization

Robin of the online road to next, recommended to update the default seeding script. Therefore I will add the following code at the end of the main function within my prisma/seed.ts file.

export async function main() {
  // Start timer
  const t0 = performance.now();
  console.log("DB Seed: Started ...");
 
  // Wipe existing data
  await prisma.ticket.deleteMany();
 
  // ! This code already exist in the main function !
  // for (const t of ticketData) {
  //  await prisma.ticket.create({ data: t });
  // }
 
  // Stop timer
  const t1 = performance.now();
  console.log(`DB Seed: Finished (${t1 - t0}ms)`);
}

This customization is doing the following:

  • Starting a timer when the seeding starts
  • Wipes the existing data
  • Stops the timer and prints the total time needed for seeding

Seed command

With this command I can seed the database, every time I do so, the main function of the seed.ts file gets executed. Re-seed the database

npx prisma db seed

Conclusion

Seeding is a great a simple way to work with my database, figure out if everything works and not worrying about losing any valuable data.