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.

What is Prisma?

Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.

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 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. It is typically used within your NestJS services to fulfill the data needs of the controllers.

Diagram showing Prisma fitting in the Server part of the Client-server-database stack
clientnestjs serverdatabase

Prisma and NestJS Code Example

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, OnModuleDestroy } from '@nestjs/common';
2import { PrismaClient } from '@prisma/client';
3
4@Injectable()
5export class PrismaService extends PrismaClient
6 implements OnModuleInit, OnModuleDestroy {
7 async onModuleInit() {
8 await this.$connect();
9 }
10
11 async onModuleDestroy() {
12 await this.$disconnect();
13 }
14}
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, OnModuleDestroy } from '@nestjs/common';
2import { PrismaClient } from '@prisma/client';
3
4@Injectable()
5export class PrismaService extends PrismaClient
6 implements OnModuleInit, OnModuleDestroy {
7 async onModuleInit() {
8 await this.$connect();
9 }
10
11 async onModuleDestroy() {
12 await this.$disconnect();
13 }
14}

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.

How to query your database in NestJS with Prisma

A comprehensive tutorial about querying your database in a NestJS app 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.

Get Involved

Join the NestJS + Prisma Community

Join us on Slack

Get help and engage with thousands of NestJS and Prisma developers on Slack

Join Prisma Slack

Showcase on Twitter

We’d love to see what you’re building with NestJS and Prisma

Tweet #prisma+nestjs

Get Stickers

Show your NestJS + Prisma love with custom free stickers

Order stickers