Prisma - A Modern ORM
Published: 2024-03-19 04:50:00

## What is Prisma❓
**Prisma** is a modern [ORM](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) (Object-Relational Mapping) library which integrates seamlessly with TypeScript and JavaScript. 

By ‘modern’, [Prisma documentation](https://www.prisma.io/docs/orm/overview/prisma-in-your-stack/is-prisma-an-orm) emphasizes that Prisma ORM is fundamentally a new type of ORM, which does not suffer from the problems commonly associated with traditional ORMs such as [bloated model instances and lack of type-safety](https://www.prisma.io/docs/orm/more/comparisons/prisma-and-typeorm#type-safety). 

Prisma was created to abstract away the low-level database interactions and increase developer control and productivity. I will touch on my experience using Prisma in this article.

## What is an ORM? 🧐
If you are not familiar with ORMs, this is a programming technique used to convert data between incompatible type systems, typically between object-oriented programming languages and relational databases. 

This abstraction simplifies database operations and makes it easier to work with databases in object-oriented applications.

![ORM Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjk6cj5z6j9q6a1int1u.jpeg)

## Using Prisma 🪄
Last weekend, I created [a simple API](https://github.com/glowiep/country-api) project which uses Prisma for database operations, built on top of ExpressJS. 

Here is a conceptual mental model: 

> Now imagine if you’re building a really cool treehouse with your friends, Prisma is your architecture plan for this project. It is where you decide how many floors your treehouse will have, where the rooms are located and what color the walls will be. 

In my simple API project, Express (web framework for Node.js) is the magical framework that helps build and decorate the treehouse. It provides the tools and materials to add features such as fun slides, swings and ladders.

![Treehouse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juwef3vf2kse785pueoq.png)

### What I learned 💡
In my experience using Prisma, I noticed that a lot can be done in a few steps. 

Need to create a new table? 

> 1. Define your model in Prisma schema.
> 2. Generate migration.
> 3. Apply migration.
> 4. Populate the new table with seed data.

Prisma Studio was an _exciting_ discovery for me. [Prisma Studio](https://www.prisma.io/docs/orm/tools/prisma-studio) is a visual editor for the data in your database which runs on your browser. 

You can edit data directly in the model cells, as well as add and delete records. This may appeal to visual thinkers/ learners. You may also sort and filter data in the model (table) in Prisma Studio, which is more efficient than manually running SQL queries to check the status of your database.


![Prisma Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fv0n35z6kkb12nv9px0v.png)


![Country API in Prsma Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9k61v0fm7m0127fkeli0.png)


### Benefits 🔱
After my first taste of working with Prisma, I can see the immense benefits it offers. 

Here are some of the greatest benefits (not limited to) of using Prisma:


💼 **Database Agnostic**: Prisma supports various databases such as PostgreSQL, MySQL, SQLite and SQL Server. This allows you to switch databases easily without worrying about making major changes in your application code.

🧰 **Migrations**: Prisma supports database migrations, this allows you to evolve your database schema over time and keep track of these changes.

🗂 **Schema-based Relationships**: Just as an architecture plans serves as a blueprint for building a treehouse, Prisma schema serves as a blueprint for developing and interacting with your database. You can define your data models, and the relationships between your data models in the Prisma Schema. This declarative Prisma schema provides a clear overview of the database current state.

🤝 **Collaboration**: If you are working in a team, a great benefit of using Prisma is it is easy for everyone to understand. Compare this to traditional workflows where if a developer is trying to understand the current table structure, they will likely have to spend some time digging into migration files. 

🖋 **Type Safety**: [Prisma Client](https://www.prisma.io/docs/orm/reference/prisma-client-reference) abstract away the low-level database interactions and provides an easily understandable API for querying and mutating data.

In short, Prisma can have a positive impact on productivity as well as developer experience. 

## When should you use Prisma? ✅

Prisma would be a good fit for your project if you are building a server-side application, which interacts with a database. Prisma’s ability to work with both TypeScript and JavaScript does make for a versatile choice for database interactions in Node.js applications.

## When to NOT use Prisma? ❎
Like any other tool, Prisma does come with its own [tradeoffs](https://www.prisma.io/docs/orm/overview/introduction/should-you-use-prisma). If you need full control over your database queries, you would have to find an alternative. This is because Prisma ORM is an abstraction which takes away some of the control you might have over your database operations if you were to use plain SQL.

## Summary
I hope this inspires you to embark on a journey to explore Prisma one day (just as I have), and benefit from Prisma’s role in providing a solid and organized foundation for your database structure (analogous to a scalable and maintainable architectural guide for the construction of a treehouse).

### Reference:
Prisma Documentation: https://www.prisma.io/docs