ORMs and Why I Recommend Prisma
Before you init Prisma onto your Next.js Project, read on to find out what an ORM exactly is, why it’s useful, and also why Prisma is the best ORM out there:
If you’ve previously written database queries for Relational Databases you probably understand that it’s not a cakewalk for someone from an OOPs background. This is where ORM or Object Relational Mapping jumps in, allowing us to query and manipulate data from a database through an object-oriented programming language of our choice. Traditionally, if I were to interact with my database, I’d have to write SQL queries. Using an ORM I can simply communicate with the same using code written in JavaScript, for example. In a nutshell, an ORM is nothing but a layer between your code and your database, facilitating communication using methods declared within the framework.
What’s the benefit of using an ORM?
The biggest advantage that you get out of implementing an ORM is that you get to write your queries comfortably in the language of your choice. This minimizes the constraints you have in terms of development tremendously. For someone like me who absolutely detests query languages, ORM adds a lot of flexibility in terms of the usage of database(s) as well. Moreover, you can easily reuse your code with minor to no changes.
What is Prisma?
Prisma is a server-side library that helps your app read and write data to the database in an intuitive and safe way. Prisma supports MongoDB, PostgreSQL, MySQL, and SQL Servers amidst a lot of other databases. You have the option to either start a new database with Prisma or connect an existing database to Prisma.
In short, it’s an open-sourced ORM segregated into three services, namely:
- Prisma Client: A query builder that gets auto-generated from the Prisma schema with types tailored to your application.
- Prisma Migrate: A service that automatically generates SQL database migrations, that are fully customizable. For quick data model prototyping in development,
prisma db push
lets you make changes to the database without generating migration files. - Prisma Studio: A visual database browser that opens in your browser to graphically view and edit the data.
For more details about Prisma, click here.
How does it work?
Every project that uses a tool from the Prisma toolkit starts with a Prisma schema file. This Prisma schema file is the main configuration file for your Prisma setup. It is typically called schema.prisma
and consists of Data Sources, Generators, and Data Model Definition. It allows developers to define their application models in an intuitive data modeling language. Whenever a prisma
command is invoked, the CLI typically reads some information from the schema file, which contains the connection to a database and defines a generator.
Find the documentation here.
Installing Prisma
- In order to get started, we’ll install the Prisma CLI tool as a development dependency using
npm install —-dev prisma
. - Now we’ll set up our Prisma project by creating the Prisma schema file template with
npx prisma init
. This creates a/prisma
folder with aschema.prisma
file at the root of the project. There’s also new/.env
that you should add to.gitignore
. - To connect your database, you need to set the
url
field of thedatasource
block in your Prisma schema to your database connection URL. The format of the connection URL for your database depends on the database you use. - With the
npx prisma db pull
command, Prisma can pull the pre-existing database schema and on its own copy and refactor it into theschema.prisma
file. Alternatively, you can also install and generate Prisma Client. Simply runnpm install @prisma/client
, followed bynpx prisma generate
. This command reads your Prisma schema and generates your Prisma Client library.
Why Prisma?
Prisma’s main goal is to make application developers more productive when working with databases.
It is an upgraded ORM that mitigates many problems of traditional ORMs, such as bloated model instances, mixing business with storage logic, lack of type-safety, or unpredictable queries.
It uses the Prisma schema to define application models in a declarative way. Prisma Migrate then allows to generate SQL migrations from the Prisma schema and executes them against the database. CRUD queries are provided by Prisma Client, a lightweight and entirely type-safe database client for Node.js and TypeScript.
Ending Notes
ORM is advantageous for the folks who have a stronger background in OOPs and not SQL or query languages. Only consider implementing this system if you’re ready to learn more about the ORM library, what it offers and how to set it up error-free, otherwise, you might end up wasting your time instead of trying to save it. Even if you dive into ORM, continue to practice and have a good hold over SQL and other query languages.
Of all the different kinds of ORMs out there, I felt that Prisma was way ahead of all the competitors, thanks to the vast beginner-friendly documentation and this comparison page too.