In very exciting news, Prisma.io recently announced the general availability of support for MongoDB. For those of you who are unfamiliar, Prisma is a next-generation objection-relational mapper (ORM) that makes development of web applications with Node.js and Typescript lightning fast. It’s worth noting that Prisma is the only fully type-safe ORM in the TypeScript ecosystem. The generated Prisma client ensures typed query results even for partial queries and relations. Tinkering with it was super easy - it’s very developer friendly and a welcome abstraction layer for error-prone querying.
How does it work?
Every project starts with a Prisma schema file. The schema file allows developers to define application models in an intuitive data modeling language. This helps prevent data inconsistencies and bloated data models. It also contains the connection to your MongoDB database and defines a generator (‘the prisma client’). The prisma client is an auto-generated and type-safe query builder for TypeScript (and Node.js). With autocompletion right in Prisma’s code editor, developers can more easily build queries of their MongoDB data without needing to look up reference documentation.
How to get started?
Despite Prisma support for MongoDB being fairly new, there are already a plethora of resources available on this topic, so I thought I would condense those into one quick guide. To get started, you’ll need to complete the following steps:
- Create a Prisma project
- Create a Prisma schema and start data modeling
- Generate a Prisma client
- Create an Atlas cluster
- Connect to your Atlas cluster
- Write data
- Query your data with the Prisma client
Note if you want to explore your data you can do that in the Prisma Studio visual editor, or you can use MongoDB Compass, or you can use the data explorer available in the Atlas UI, or you can use the MongoDB VS Code Extension... there are a lot of options here.
How does Prisma differ from Mongoose?
Mongoose is another ORM (or ODM, depending on who you ask…) for MongoDB. How are they different? Which one is the better fit? Here’s a simple comparison of various queries with each of them.
Most developers should find Prisma’s minimal API a welcome change from overly complex competitors in this space - certainly most of the features advertised, the average developer doesn’t really need. I’ve now built a couple of quick applications with Prisma and Node.js and I find it very intuitive and easy to get something up and running. As with all ORMs it is an abstraction layer, so you do sacrifice some flexibility in the name of usability and productivity - but I didn’t hit any of those alleged limitations and I’m not convinced most people would, even when building a more robust application.
We hope you’ll give it a try (for science!) and let us know your thoughts. We also have a whole week of activities with Prisma ahead where you can learn more about using Prisma with MongoDB - check out the launch website to learn more and join us!
You can also check out Prisma on github.
Top comments (0)