Next-generation & fully type-safe ORM for NestJS

Query data from MySQL, PostgreSQL & SQL Server databases in NestJS apps using Prisma – a better ORM for JavaScript and TypeScript.

tech

What is Prisma?

Prisma makes working with data easy! It offers a type-safe Node.js & TypeScript ORM, global database caching, connection pooling, and real-time database events.

Query
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
Table
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

How Prisma and NestJS fit together

Prisma ORM is a next-generation ORM that can be used to query a database in NestJS apps. It embraces TypeScript to avoid runtime errors and improve productivity. The type-safety it provides goes far beyond the guarantees of traditional ORMs like TypeORM or Sequelize (learn more).
Prisma integrates smoothly with the modular architecture of NestJS, no matter if you're building REST or GraphQL APIs.

You can also supercharge usage of Prisma ORM with our additional tools:
Prisma Accelerate is a global database cache and scalable connection pool that speeds up your database queries.
Prisma Pulse enables you to build reactive, real-time applications in a type-safe manner.

Prisma and NestJS code examples

Combining NestJS and Prisma provides a new level of type-safety that is impossible to achieve with any other ORM from the Node.js & TypeScript ecosystem. This example demonstrates how to use Prisma Client

following NestJS' modular architecture via Dependency Injection by implementing a UserService class that will provide CRUD or domain-specific operations to your application controllers.

PrismaService

PrismaService

A PrismaService class can be implemented by extending the generated PrismaClient in order to build an abstraction of Prisma Client that integrates with your NestJS architecture. It will be provided to other services and controllers via Dependency Injection.

1import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common'
2import { PrismaClient } from '@prisma/client'
3
4@Injectable()
5export class PrismaService extends PrismaClient
6 implements OnModuleInit {
7
8 async onModuleInit() {
9 await this.$connect();
10 }
11}
AppController
Prisma schema

PrismaService

A PrismaService class can be implemented by extending the generated PrismaClient in order to build an abstraction of Prisma Client that integrates with your NestJS architecture. It will be provided to other services and controllers via Dependency Injection.

1import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common'
2import { PrismaClient } from '@prisma/client'
3
4@Injectable()
5export class PrismaService extends PrismaClient
6 implements OnModuleInit {
7
8 async onModuleInit() {
9 await this.$connect();
10 }
11}

Why Prisma and NestJS?

Embracing TypeScript

Prisma is the first ORM that provides full type-safety, even when querying partial models and relations.

Smooth integration

Prisma fits perfectly into the modular architecture of NestJS and provides a powerful database access layer.

Type-safe database client

Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.

Intuitive data modeling

Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.

Easy database migrations

Generate predictible and customizable SQL migrations from the declarative Prisma schema.

Designed for building APIs

Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).

Featured Prisma & NestJS community examples

Production-ready starter kit with NestJS & Prisma

A starter kit covering everything you need to build NestJS with Prisma in production.

Prisma in the NestJS docs

Learn how to use Prisma with NestJS in the official NestJS documentation.

Building a REST API with NestJS and Prisma

A comprehensive workshop and series about building a NestJS REST API with Prisma.

Migrating a large production app from TypeORM To Prisma

An in-depth article about the migration process of a NestJS app from TypeORM to Prisma.