What is Prisma?
Before diving in, let’s get acquainted with Prisma. Prisma is a next-generation ORM (Object Relational Mapping) tool that offers several components for database management and access. It is a server-side library designed to help developers read and write data to databases in an intuitive, efficient, and safe manner. Unlike traditional ORMs, Prisma leverages TypeScript, allowing developers to define a schema from which Prisma generates all necessary models and TypeScript types.
Steps:
Project Setup:
To kick things off, initialize your Node project:
npm init
Next, install the required dependencies:
npm i --save-dev prisma typescript ts-node @types/node nodemon
Configure TypeScript by creating a tsconfig.json
file with the following settings:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
Initialize Prisma with:
npx prisma init
For PostgreSQL, use:
npx prisma init --datasource-provider postgresql
These commands will generate essential configuration and setup files for your project.
Tip: Use the Prisma Extension for code highlighting!
Prisma Model Setup:
Define the User Model in the schema.prisma
file:
model User {
id String @id @default(uuid())
email String @unique
name String?
password String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
With the User model defined, we need to connect our database before utilizing these models.
Prisma Migration:
To synchronize Prisma with your database, perform a migration:
npx prisma migrate dev --name init
Note: The
--name
parameter is optional.
Ensure your DATABASE_URL
is correctly set before migrating. This command will create a migration file containing SQL commands reflecting your schema.
Prisma Client:
The Prisma Client is the code used to interact with your database. After migration, Prisma generates a new client and adds it to the node modules. To install the client library, run:
npm i @prisma/client
To manually generate or regenerate the Prisma client, use:
npx prisma generate
To use the Prisma Client, add the following code:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
CRUD Operations with Prisma Client:
Create a new file script.ts
and start implementing CRUD operations:
CREATE:
To create a user:
npm i --save-dev prisma typescript ts-node @types/node nodemon
0
Note: Add a script to
package.json
to run the code.
To create multiple users:
npm i --save-dev prisma typescript ts-node @types/node nodemon
1
READ:
To view all records:
npm i --save-dev prisma typescript ts-node @types/node nodemon
2
To retrieve a single user by ID:
npm i --save-dev prisma typescript ts-node @types/node nodemon
3
UPDATE:
To update a user’s name:
npm i --save-dev prisma typescript ts-node @types/node nodemon
4
DELETE:
To delete a user by ID:
npm i --save-dev prisma typescript ts-node @types/node nodemon
5
With these steps, you’ve successfully implemented basic CRUD operations in your Node.js and PostgreSQL app using Prisma.