When working with databases in Node.js, using an Object-Relational Mapping (ORM) tool like Prisma can simplify your workflow, make your code more readable, and reduce bugs. Prisma is a modern ORM that supports TypeScript out of the box, provides a declarative schema, and generates type-safe queries.

What is prisma?

Prisma is an open-source next-generation ORM that helps you query your database in a type-safe way. Unlike traditional ORMs, Prisma uses a schema file to model your data and generate a tailored client.

Why use prisma?

  • Type safety: You get autocompletion and compile-time checks
  • Productivity: Define your models once and let Prisma generate the client
  • Maintainability: Clear separation between schema, migrations, and code
  • Support for major databases: PostgreSQL, MySQL, SQLite, SQL Server, MongoDB

Installation

Start by installing Prisma in your Node.js project:

npm install prisma --save-dev
npm install @prisma/client

Initialize Prisma in your project:

npx prisma init

This creates a prisma/schema.prisma file and a .env file for your database credentials.


Defining your schema

Let’s define a simple User model:

// prisma/schema.prisma
generator client {
  provider = "prisma-client-js"
}

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

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

Then generate the Prisma client:

npx prisma generate


Applying migrations

To create the initial database tables:

npx prisma migrate dev --name init

This generates and applies a migration based on your schema.


Using prisma client

You can now use the generated Prisma Client to interact with your database:

// src/index.js
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
  const user = await prisma.user.create({
    data: {
      email: 'jane.doe@example.com',
      name: 'Jane Doe',
    },
  });

  console.log('New user:', user);

  const users = await prisma.user.findMany();
  console.log('All users:', users);
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });


Example query patterns

  • Create:
await prisma.user.create({ data: { email: 'a@example.com' } });

  • Read:
await prisma.user.findUnique({ where: { id: 1 } });

  • Update:
await prisma.user.update({
  where: { id: 1 },
  data: { name: 'Updated Name' },
});

  • Delete:
await prisma.user.delete({ where: { id: 1 } });


Using prisma with typescript

Prisma works great with TypeScript out of the box. Just use .ts files instead of .js, and you’ll get full type support for your models and queries.

Example:

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

const prisma = new PrismaClient();

async function getUsers() {
  const users = await prisma.user.findMany();
  return users;
}


Conclusion

Prisma is a powerful and modern ORM for Node.js that simplifies database access with type safety and clean syntax. Whether you're building small applications or large-scale backends, Prisma helps you stay productive and maintainable. Try it in your next project—you might not want to go back.