Seeding your database with Prisma in a NextJs application

In this article, I will show you how you can add data to your database (supabase in my/our case) with prisma in a NextJs application.

Previously, I wrote an article on how you can create a table for our Supabase database with Prisma in NextJs.

In this article, I will show how you can upload user data using the defined table.

Disclaimer: The process described on the official Prisma site is a bit different. You can follow those steps if you prefer them or find them easier. I find the following way to be the easiest.

Now to get started seeding, we need a "seed.ts" file to write our seeding script in. The location of the seed.ts will be in "pages-->api-->seed.ts"
NOTE: IT IS IMPORTANT TO NAME THE FILE "seed.ts" AND PLACE IT AT "pages-->api-->seed.ts" FOR IT TO WORK.

The seeding script that will go in seed.ts is mentioned below.

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
//This is the seed.ts file
import type { NextApiRequest, NextApiResponse } from "next";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

//The type declaration defines the shape of the response data that will be returned by this API route. In this case, the response will contain a name property of type string.
type Data = {
  name: string;
};

// The function handler will be executed when this API route is called.
export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
//The following line is optional. When "prisma.user.deleteMany()" function is called, it will delete all existing users from the database. It is really helpful if we have any properties with unique value. 

// await prisma.user.deleteMany();

//Now the "prisma.user.create()" function is called to create a new user in the database.
  const userTest = await prisma.user.create({
    data: {
      first_name: "test",
      last_name: "test",
      email: "test@hotmail.com",
      city: "manhattan",
      password: "123456789",
    },
  });
 const userTrace = await prisma.user.create({
    data: {
      first_name: "Trace",
      last_name: "Trace",
      email: "trace@test.com",
      city: "boulder",
      password: "123456789",
    },
  });

  res.status(200).json({ name: "From Seed.ts seeding was successful." });
}

Once you are ready with the script with your desired data. All we need to do is run
npm run dev on our terminal. Then go to "http://localhost:3000/api/seed" on that page we should see our desired message. In our case "From Seed.ts seeding was successful."

Voila, you are done. Now to verify you can either go to supabase or open another instance in your terminal and open up prisma studio with the following command
npx prisma studio

I like to go to supabase instead of prisma studio.

From the snapshot above, we can see that the data we had in our seeding script has been uploaded to our database.

I hope this article helped you.

Happy hacking!