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:
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 pushlets 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.
- 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
/prismafolder with a
schema.prismafile at the root of the project. There’s also new
/.envthat you should add to
- To connect your database, you need to set the
urlfield of the
datasourceblock 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 pullcommand, Prisma can pull the pre-existing database schema and on its own copy and refactor it into the
schema.prismafile. Alternatively, you can also install and generate Prisma Client. Simply run
npm install @prisma/client, followed by
npx prisma generate. This command reads your Prisma schema and generates your Prisma Client library.
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.
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.