Search code examples
next.jsserver-side-renderingprismasupabase

can not seed supabase using Prisma in nextjs project


I can not seed supabase database using Prisma from my project

I added seed script in package.json

"prisma": {
    "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},

In seeding file deleting previous records then create many entries.

await client.category.deleteMany();

const categories = await client.category.createMany({
    data: [
        {
            name: "swimming",
        },
    ],
});

but get error:

Error: Command was killed with SIGSEGV (Segmentation fault): ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts


Solution

  • supabase will give you connection string options, chose the URI and paste it to the env file that created by npx prisma init. After you successfully set schema, make sure this runs npx prisma db push without error.

    Then in pages/api/seed.ts api file, write this

    export default async function handler(
      req: NextApiRequest,
      res: NextApiResponse<Data>
    ) {
       await client.category.deleteMany();
    
       const categories = await client.category.createMany({
        data: [
            {
                name: "swimming",
            },
        ],
    });
    
    res.status(200).json({ message: "successfully seeded" });    
    }
    

    Then visit, http://localhost:3000/api/seed this api function will be executed