Support for MongoDB has been one of the most requested features since the initial release of the Prisma ORM. Using both technologies together makes developers more productive and allows them to ship more ambitious software faster.
Node.js developers love MongoDB for the flexibility it provides and a familiar JSON-based data model! However, being a schemaless database means that MongoDB users can easily run into data inconsistencies as their projects start to evolve and grow in size.
With Prisma, developers can easily define a schema for the data that they want to store in MongoDB. This has several benefits:
- The data structures inside of MongoDB become explicit.
- The consistency of the data stored in MongoDB is ensured by Prisma.
- All team members are aware of the data stored inside of MongoDB.
Prisma schemas are written in Prisma's intuitive and human-readable modeling language, here's what an example looks like:
Prisma's data model makes it particularly easy to work with relations. In the schema example above, you see a one-to-many relation from
Post via a reference as well as the
Profile type which represent an embedded document on the
Similar to MongoDB, Prisma also provides a database client that you can use to query your MongoDB database: Prisma Client. One of the main differences between the two is that Prisma Client is aware of the schema you define, so it can help you write your queries via auto-completion.
Prisma also allows you to fall back to MongoDB's native query API when you need to run more specialized queries against your MongoDB instance.
MongoDB doesn't natively support referential actions (i.e. cascading deletes/updates). Prisma "polyfills" this functionality and allows developers to configure these in the Prisma schema and can therefore can contribute to the referential integrity of the data in your MongoDB database.
In the data model above, the
@relation attribute on the
author field uses
onDelete: Cascade. This means that when a
User document is deleted, all the
Post documents that were related to it are deleted as well.
Prisma especially starts to shine when it's used in a TypeScript application as it provides extremely strong type-safety guarantees.
For example, consider this query where we specify via the
select option which fields of the
User model (and its relations) should be returned in the response:
If you select the Generated Type tab in the code snippet above, you see that the type of the resulting
userData constant is now tailored to the fields that are selected in the query.
This has two main benefits:
- The TypeScript compiler will catch cases where you accidentally access a field that was not returned from the query.
- Your auto-completion suggestions are tailored to the given type.
We are excited to see what you're going to build with MongoDB and Prisma! You can get started with MongoDB in our documentation (see buttons below).
To get started with MongoDB and Prisma, you can follow our guide to set up a new project from scratch.
Start from scratch with MongoDB
If you already have an existing project that uses a MongoDB database, you can easily start to incrementally adopt Prisma.
Prisma has an introspection features that reads the structures of the documents you have stored in your MongoDB collections and creates according Prisma models. We are also going to publish a migration guide from Mongoose soon.
Add Prisma to existing MongoDB project
To celebrate MongoDB support in Prisma, we are planning a launch week at the end of April with lots of fun activities, new content and awesome events!
- Date: April 25 - 29
- Location: Virtual
- Website: https://www.prisma.io/mongodb-launch
Sign up via the website above to join us for the launch week! You can also submit a talk proposal for our Showcase Lightning Talks (~3min) if you are building something with MongoDB and Prisma and want to talk about it during the launch week!
Don’t miss the next post!
Sign up for the Prisma Newsletter