Type Safety with Prisma & MongoDB
Rate this tutorial
Did you know that Prisma now supports MongoDB? In this article, we'll take a look at how to use Prisma to connect to MongoDB.
If you want to hear from Nikolas Burk and Matthew Meuller of Prisma, check out this episode of the MongoDB Podcast.
Schemas help developers avoid data inconsistency issues over time. While you can define a schema at the database level within MongoDB, Prisma lets you define a schema at the application level. When using the Prisma Client, a developer gets the aid of auto-completing queries, since the Prisma Client is aware of the schema.
However, there may be use cases where you'll need to store related data in separate collections. To do that in MongoDB, you can include one document’s
_idfield in another document. In this instance, Prisma can assist you in organizing this related data and maintaining referential integrity of the data.
We are going to take an existing example project from Prisma’s
One of the examples is a blog content management platform. This example uses a SQLite database. We'll convert it to use MongoDB and then seed some dummy data.
In this article, we’ll use a MongoDB Atlas cluster. To create a free account and your first forever-free cluster, follow the
We'll first need to set up our environment variable to connect to our MongoDB Atlas database. I'll add my MongoDB Atlas connection string to a
You can get your connection string from the Atlas dashboard.
Now, let's edit the
datasource dbobject, we'll set the provider to "mongodb" and the url to our environment variable
Usermodel, we'll need to update the
id. Instead of an
Int, we'll use
String. We'll set the default to
auto(). Since MongoDB names the
_id, we'll map the
_id. Lastly, we'll tell Prisma to use the data type of
We'll do the same for the
idfield. We'll also change the
Stringand set the data type to
This schema will result in a separate
Postcollection in MongoDB. Each post will have a reference to a user.
Now that we have our schema set up, let's install our dependencies and generate our schema.
If you make any changes later to the schema, you'll need to run
npx prisma generateagain.
Next, we need to seed our database. The repo comes with a
prisma/seed.tsfile with some dummy data.
So, let's run the following command to seed our database:
This also creates the
Postcollections that are defined in
Because we made some changes to the
iddata type, we'll need to update some of the example code to reflect these changes.
Here's one example. We need to remove the
Number()call from the reference to the
idfield since it is now a
Notice in this file, when hovering over the
postvariable, VS Code knows that it is of type
Post. If we just wanted a specific field from this, VS Code automatically knows which fields are included. No guessing!
Those are all of the updates needed. We can now run the app and we should see the seed data show up.
We can open the app in the browser at
From the main page, you can click on a post to see it. From there, you can delete the post.
We can go to the Drafts page to see any unpublished posts. When we click on any unpublished post, we can publish it or delete it.
The "Signup" button will allow us to add a new user to the database.
And, lastly, we can create a new post by clicking the "Create draft" button.
All of these actions are performed by the Prisma client using the API routes defined in our application.
Prisma makes dealing with schemas in MongoDB a breeze. It especially shines when using TypeScript by making your code readable and type-safe. It also helps to manage multiple collection relationships by aiding with referential integrity.
I can see the benefit of defining your schema at the application level and will be using Prisma to connect to MongoDB in the future.