Seeding a Database with Prisma (TypeScript) - Robin Wieruch

Seeding a Database with Prisma (TypeScript) - Robin Wieruch

A short tutorial about seeding a database with Prisma in a TypeScript application. I decided to write this tutorial, because many of my other tutorials depend on a database with an initial seeding and I want to reference it.

We will assume the following model for our database in the prisma/schema.prisma * file:

We will create a new TypeScript file called seed.ts in the prisma folder. This file will contain the code to seed the database. We will use the Prisma Client to interact with the database:

Next we need to execute the seeding from the command line. Because we are dealing with a TypeScript file here, we need to add an extra dependency to our project:

Afterward, include it in your package.json :

Finally, you can run the seeding with the following command:

You should see the seeded data in your database:

That’s it! You have successfully seeded your database with Prisma in a TypeScript application.

Recommended articles