A Beginner's Guide to Prisma ORM

3 days ago 2

deepvinci

Imagine you’re at a party where everyone speaks a different language, like Spanish, French, or Parseltongue. Total Chaos.

Here you met a charismatic guy. He’s a multilingual who can translate any language to English without a bother in the world. You come to realize his name is Prisma ORM.

Prisma at its core is an object-relational mapper, which is a fancy way of saying it helps you use JavaScript/TypeScript to talk to your database instead of writing long messy SQL.

https://www.prisma.io/studio

So instead of writing this:

SELECT * FROM users WHERE age > 18;

You write this:

const adults = await prisma.user.findMany({
where: { age: { gt: 18 } }
});
  • Initialize a Node project
npm init -y
npm install prisma --save-dev
npx prisma init

this creates a prisma/ folder and a .env file

  • Set up a Database

Inside .env, put a URL of your database. here you can use PostgreSQL, SQLite, MySQL, or even MongoDB

DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

For practicing and small scale applications you can even use SQLite.

DATABASE_URL="file:./dev.db"

Once you’re done with setting up a database source like PostgreSQL or SQLite, you can start working with a file inside the prisma/ folder that was created earlier you can name it schema.prisma.

Create a datasource block. This block configures your database connection.

datasource db {
provider = "postgresql" // use sqlite if you're using sqlite
url = env("DATABASE_URL")
}

Create a Prisma client generation. As of 2025, “prisma-client-js” is the standard provider for generating the JavaScript/TypeScript client.

generator client {
provider = "prisma-client-js"
}

Create a model of your database

model User {
id Int @id @default(autoincrement())
name String
email String @unique
age Int?
}

This is how you would create a User table with id, name, email, and age as the columns of the table. Once you have the datasource, generator and schema created in schema.prisma, you need to create and apply migrations to your database. This will create the actual tables in your database based on your schema.

npx prisma migrate dev --name init

and then you’re ready to create a custom prisma client with your schema types

npm install @prisma/client
npx prisma generate

You need to run npx prisma generate every time you update the schema file.

Here’s the schema.prismafile with the datasource, generator and model.

// schema.prisma file

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model User {
id Int @id @default(autoincrement())
name String
email String @unique
age Int?
}

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient(); // instantiate the custom prisma client

export default async function getAllUsers() {
const users = await prisma.user.findMany();
console.log('All Users:', users);
}

This is all you need to have Prisma return all the rows in theUser table. To get a list of all the typesafe query functions available in prisma-client-js you can go through their docs at here.

Now that you have experienced Prisma firsthand, you can understand the 3 main tools that make up Prisma ORM.

  1. Prisma Client: This auto-generates type-safe query functions for your database with JavaScript/TypeScript after you have made the schema file.
  2. Prisma Migrate: This helps you evolve your database over time, it even keeps track of the query run in the past to modify the database.
  3. Prisma Studio: This is nice tool that lets you view and edit your database in a visual way through a browser.

Run npx prisma studio to access the visual editor of your database.

Additional Resource to learn more about Prisma.

Happy building:)

Read Entire Article