Query data from MySQL, PostgreSQL & SQL Server databases in NestJS apps using Prisma – a better ORM for JavaScript and TypeScript.
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.
// Creating a new recordawait prisma.user.create({ firstName: “Alice”, email: “alice@prisma.io”})
id firstName email 1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
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.
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.
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 PrismaClient6 implements OnModuleInit {7
8 async onModuleInit() {9 await this.$connect();10 }11}
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 PrismaClient6 implements OnModuleInit {7
8 async onModuleInit() {9 await this.$connect();10 }11}
Prisma is the first ORM that provides full type-safety, even when querying partial models and relations.
Prisma fits perfectly into the modular architecture of NestJS and provides a powerful database access layer.
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.
Generate predictible and customizable SQL migrations from the declarative Prisma schema.
Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).
A starter kit covering everything you need to build NestJS with Prisma in production.
Learn how to use Prisma with NestJS in the official NestJS documentation.
A comprehensive workshop and series about building a NestJS REST API with Prisma.
An in-depth article about the migration process of a NestJS app from TypeORM to Prisma.
We have multiple channels where you can engage with members of our community as well as the Prisma team.