Getting Started with Prisma ORM and MongoDB using TypeScript

Max Headway
3 min readJan 7, 2024

--

Prisma ORM, a powerful database toolkit, offers seamless integration with MongoDB when paired with TypeScript. Leveraging Prisma with MongoDB unlocks a world of possibilities for building robust applications. In this guide, we’ll explore the steps to integrate Prisma ORM with MongoDB using TypeScript.

Preparing the Environment

Before diving into the integration process, ensure you have Node.js installed along with npm or yarn for package management. Begin by initializing a new TypeScript project:

mkdir prisma-mongodb-example
cd prisma-mongodb-example
npm init -y
npm install typescript prisma @prisma/client

Setting up Prisma with MongoDB

  1. Define Prisma Schema:
    Create a schema.prisma file to define your data model. For MongoDB, specify the provider as mongodb.
datasource db {
provider = "mongodb"
url = env("MONGODB_URL")
}

generator client {
provider = "prisma-client-js"
}

2. Generate Prisma Client:
Execute the following command to generate the Prisma Client.

npx prisma generate

3. Create Prisma Models:
Define your MongoDB models in the Prisma schema, for instance:

model User {
id String @id @default(cuid())
email String @unique
name String?
}

4. Connect to MongoDB:
Set up your MongoDB connection URL in an environment variable or directly in your Prisma schema. Ensure the MongoDB instance is accessible.

Working with Prisma and MongoDB

Now that your environment is set up, let’s dive into using Prisma ORM with MongoDB:

CRUD Operations

Creating Records:
Use Prisma Client methods to create records:

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function createUser(email: string, name?: string) {
return await prisma.user.create({
data: {
email,
name,
},
});
}

// Usage
createUser('example@email.com', 'John Doe');

Reading Records:
Fetch data using Prisma Client’s query methods:

async function getUsers() {
return await prisma.user.findMany();
}

// Usage
const users = await getUsers();

Updating Records:
Modify existing data with Prisma’s update method:

async function updateUser(id: string, newName: string) {
return await prisma.user.update({
where: { id },
data: { name: newName },
});
}

// Usage
await updateUser('userId123', 'Jane Doe');

Deleting Records:
Remove data using Prisma’s delete method:

async function deleteUser(id: string) {
return await prisma.user.delete({
where: { id },
});
}

// Usage
await deleteUser('userId123');

Using Prisma Studio

Prisma Studio provides a graphical interface to interact with your database. Follow these steps to integrate Prisma Studio into your TypeScript and MongoDB project:

Launch Prisma Studio:
Run the following command in your terminal:

npx prisma studio

Explore Your Data:
Prisma Studio opens in your default browser, displaying your MongoDB database schema. You can:

  • View tables and relationships.
  • Add, edit, or delete records.
  • Perform complex queries visually.

Conclusion

Integrating Prisma ORM with MongoDB using TypeScript empowers developers to build scalable and efficient applications. Leveraging Prisma’s intuitive API and MongoDB’s flexibility enables seamless data operations. Start harnessing the power of Prisma and MongoDB to elevate your development experience today.

Additional Resources

--

--

Max Headway

Programming nerd just trying to share what I learn with the world.