Prisma Next is in early access.Read the docs
Add to Existing Project

MongoDB

Add Prisma Next to an existing MongoDB project.

This guide shows how to add Prisma Next to a project that already uses MongoDB. You will run prisma-next init, describe the collections you want to work with, emit the generated artifacts, and run a couple of queries.

Use this path when you already have an application and database. Make sure the app can already reach its MongoDB deployment and runs on Node.js 24 or newer. If you want Prisma Next to create a new app for you, use the MongoDB quickstart.

For local development, use a replica set. MongoDB Atlas already gives you that.

1. Make sure you can run the example script

If your project already runs TypeScript scripts, you can skip this step.

Otherwise, install the script tooling:

bun add --dev tsx typescript

Later, prisma-next init will also add the Node.js types it needs and make sure the generated Prisma Next files can run as ES modules. If your project already declares "type": "commonjs", Prisma Next leaves that choice alone and prints a warning so you can decide how to wire the generated helper into your app.

2. Initialize Prisma Next

From the root of your existing project, run:

bunx prisma-next init --target mongodb

This is the existing-project path. It preselects MongoDB, adds Prisma Next files and package scripts to the app you already have, and does not scaffold a new framework project.

It also adds prisma-next.md and project-level Prisma Next skills for Cursor, Claude Code, Codex, and Windsurf so your agent can read the Prisma Next usage, upgrade, and extension-author guidance from the project.

When Prisma Next asks the remaining setup questions:

  • choose PSL
  • keep the default schema path, prisma/contract.prisma

3. Set your database connection string

Update .env with the connection string for the MongoDB deployment your app already uses:

.env
DATABASE_URL="mongodb://127.0.0.1:27017/app?replicaSet=rs0"

4. Describe the collections you want Prisma Next to know about

This is the key adoption step for MongoDB, because you decide which part of the existing database Prisma Next should model first.

PostgreSQL has contract infer. MongoDB does not, so this step is manual.

Open prisma/contract.prisma and make it match the collections you want Prisma Next to query first. If your existing database already has users and posts collections with email, name, title, and authorId, the starter contract is already a useful first draft:

prisma/contract.prisma
// use prisma-next

model User {
  id    ObjectId @id @map("_id")
  email String   @unique
  name  String?
  posts Post[]
  @@map("users")
}

model Post {
  id       ObjectId @id @map("_id")
  title    String
  content  String?
  author   User     @relation(fields: [authorId], references: [id])
  authorId ObjectId
  @@map("posts")
}

You do not need to model every collection on day one. Start with the part of the database you want to read and write first.

5. Emit the generated artifacts

Once the contract looks right, this step turns it into the generated files the runtime and query APIs use.

Run:

bunx prisma-next contract emit

This refreshes prisma/contract.json and prisma/contract.d.ts so the runtime and query APIs are aligned with the contract you just reviewed.

6. Run a simple high-level query

With the emitted artifacts in place, you can test the higher-level API first and confirm Prisma Next can read the existing collections.

Create a script.ts file:

script.ts
import "dotenv/config";
import { db } from "./prisma/db";

async function main() {
  const user = await db.orm.users.where({ email: "existing@example.com" }).first();
  console.log(user);

  await db.close();
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Run it:

bunx tsx script.ts

7. Run a simple low-level query

After the ORM example, this step shows the lower-level MongoDB pipeline builder against the same existing collections.

Replace script.ts with this version:

script.ts
import "dotenv/config";
import { db } from "./prisma/db";

async function main() {
  const runtime = await db.runtime();
  const plan = db.query
    .from("users")
    .match((fields) => fields.email.eq("existing@example.com"))
    .project("email", "name")
    .build();

  const rows = await runtime.execute(plan);
  console.log(rows);

  await db.close();
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Run it again:

bunx tsx script.ts

8. Next steps

When you change prisma/contract.prisma, emit the contract again:

bunx prisma-next contract emit

You do not need a migration just to read collections that already exist. Use migration plan when you want Prisma Next to own a schema change.

On this page