View as Json

MongoDB(Prisma) Provider

This provider sets up MongoDB connectivity with Prisma ORM for nextjs projects.

Official Docs


Installation Guide

Run the following command:

npx servercn-cli@latest add pr mongodb-prisma

Add the following scripts to your package.json:

package.json
{
  "scripts": {
    "db:generate": "prisma generate",
    "db:push": "prisma push",
    "db:studio": "prisma studio"
  },
}

Usage Example

import prisma from "@/lib/prisma";
src/app/api/route.ts
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
 
export const GET = async () => {
  const user = {
    email: "test@test.com",
    name: "Test User",
    password: "password123"
  };
 
  const createdUser = await prisma.user.create({
    data: user
  });
  console.log("Created user:", JSON.stringify(createdUser, null, 2));
 
  const allUsers = await prisma.user.findMany();
  console.log("All users:", JSON.stringify(allUsers, null, 2));
 
  const updatedUser = await prisma.user.update({
    where: {
      email: "test@test.com"
    },
    data: {
      name: "Updated Test User"
    }
  });
  console.log("Updated user:", JSON.stringify(updatedUser, null, 2));
 
  const deletedUser = await prisma.user.delete({
    where: {
      email: "test@test.com"
    }
  });
  console.log("Deleted user:", JSON.stringify(deletedUser, null, 2));
 
  return NextResponse.json({
    message: "Successfully executed",
    createdUser,
  });
};

File & Folder Structure

Loading files...

Installation

npx servercn-cli@latest add pr mongodb-prisma