Brief about Prisma
Let’s start this blog with a brief understanding of Prisma. Prisma is an open source ORM(Object-Relational Mapping). It is an alternative solution to writing plain SQL queries because writing plain SQL queries wont provide an ideal abstraction.
Why use Prisma?
- Prisma has it’s own schema declarative languages.
- You can write your schema from scratch or generate it by introspecting an existing database.
- Prisma allows you to easily manage and interact with your database.
- It supports: PostgreSQL ,MySQL, SQLServer, SQLite, MongoDB and CockroachDB.
- It helps in keeping the database schema in sync.
Prerequisites
Assuming that you already have NodeJs installed in your system, you need to setup your NestJs project at first. (Setup NestJs project.). After that, follow the given steps to setup your Prisma:
Prisma Installation and Setup
Step 1: To setup Prisma inside your NestJs project, simply install Prisma CLI(Prisma Command Line Interface) so that you can interact with your prisma project.
$ npm install prisma --save-dev
Step 2: Invoke CLI
$npx prisma
Step 3: Create initial Prisma Setup
$npx prisma init
It creates a prisma folder containing schema.prisma where you can modify your schema and it also creates a .env file where you can store your database credentials.
Step 4: Now, it is time to set database connection. You can set your connection with your desired database, whether it be relational or non-relational database, by going through the docs(Click Here).
Step 5: After connecting to your database, you can introspect your existing database by running the following command:
$npx prisma db pull
If you don’t have any existing database, then you can always create your own model schema in prisma.schema file as follow:
Step 6: Create model inside prisma.schema file:
model User {
id Int @id @default(autoincrement())
email String @unique
name String
}
Step 7: Now that you have created your own model, you have to generate a migration file in order to run them against the database.
$ npx prisma migrate dev --name init
Step 8 : Now, to query and perform CRUD operations, we need a query builder i.e Prisma Client. To install and generate prisma client, use the following commands:
$npm install @prisma/client
$npx prisma generate

Now that all the steps involved in installing prisma is finished, you can perform CRUD operations by querying the database.
To know more about Prisma, you can visit their official site by clicking here.