Seeding Data

Seeding data means to create data in your database that you can use to test your application. It's also useful for initializing your database with data that you know you'll need.

Seeding SQLite Data in Development

During development, it's much more useful to have data to work with. You can definitely use your own UI (or Prisma Studio) to add data to the database manually, but this may make you cry when you accidentally reset your database ๐Ÿ˜ญ.
Instead, you'll want to have a script that can reset your database. Typically it's best for this script to be idempotent. Meaning you can run it as many times as you like and you get the same desired result. So, typically you want this script to delete all the data in your database and then re-create it from scratch. If you make your seed script rely on existing data in the database you'll start looking for a way to seed your seed script! What a mess!
Another benefit of writing a seed script is you can set the IDs of your records so every time you run the seed script you can have some predictability in the IDs of your records which makes it easier to navigate around the app consistently.

Seeding SQLite Data in Production

For example, if you're building an application that allows users to lookup the nearest city with a certain population threshold from their geographic location, you'll need to have a database of cities with their population and geographic coordinates. You could manually enter this data into your database, but that would be tedious and error prone. Instead, you can write a script that will populate your database with the data you need.
The easiest way to seed production data is to include it in your migration script. You literally edit the migration.sql file that prisma generates for you to have it create the data you need.
We do this when we add permissions to the database in the web authentication workshop: Roles Seed.

Prisma Seed

Prisma's CLI has built-in support for a seed script. It will run your seed script after running dev migrate subcommand npx prisma migrate reset. You can also run it manually with npx prisma db seed.
But first it must be configured in the package.json:
"prisma": {
  "seed": "npx tsx prisma/seed.ts"
Whatever the seed property is set to will be what is run.