This document describes how to integrate Prisma, a modern type-safe database client, with NestJS applications. Prisma provides an alternative to traditional ORMs with a focus on developer experience and compile-time type safety.
For SQL database integration using TypeORM, see TypeORM Integration. For MongoDB integration using Mongoose, see Mongoose Integration. For GraphQL schema generation that works with Prisma, see Code-First Approach and Schema-First Approach.
Prisma is a next-generation database toolkit that includes a type-safe query builder (Prisma Client), a migration system (Prisma Migrate), and a database browser (Prisma Studio). Unlike TypeORM, Sequelize, and Mongoose, which have dedicated NestJS wrapper packages (@nestjs/typeorm, @nestjs/sequelize, @nestjs/mongoose), Prisma is integrated directly into NestJS applications without a first-party adapter package.
| Feature | Description |
|---|---|
| Type Safety | Auto-generated TypeScript types from database schema |
| Schema-First | Database schema defined in declarative Prisma schema language |
| Query Builder | Fluent API for constructing type-safe database queries |
| Migration System | Built-in migration generation and management |
| Multi-Database Support | PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, CockroachDB |
The primary Prisma sample application demonstrates GraphQL integration using the code-first approach with Apollo Server.
Sources: sample/22-graphql-prisma/package.json package.json
Prisma integration in NestJS follows a service-based pattern where a PrismaService wraps the Prisma Client and is injected throughout the application.
Diagram: Prisma Integration Architecture
The architecture consists of three main layers:
schema.prisma file defines data models, relations, and database configurationPrismaService class extends the generated client and provides lifecycle managementSources: sample/22-graphql-prisma/package.json22-37
The Prisma integration requires three key packages:
Diagram: Prisma Package Dependencies
| Package | Version | Purpose | Type |
|---|---|---|---|
@prisma/client | 7.4.0 | Runtime query client with generated types | Runtime |
@prisma/adapter-better-sqlite3 | 7.4.0 | SQLite database adapter | Runtime |
prisma | ^7.0.0 | CLI for schema management, migrations, and code generation | Development |
The @prisma/adapter-* packages provide database-specific implementations. The sample application uses SQLite via the better-sqlite3 adapter, but other adapters exist for PostgreSQL, MySQL, and other databases.
Sources: sample/22-graphql-prisma/package.json29-30 sample/22-graphql-prisma/package.json57
The standard integration pattern involves creating a PrismaService that extends PrismaClient and implements NestJS lifecycle hooks for connection management.
Diagram: PrismaService Lifecycle
The PrismaService typically implements:
onModuleInit(): Called during module initialization to establish database connection using $connect()onModuleDestroy(): Called during module destruction to gracefully close connection using $disconnect()PrismaClient: Inherits all generated query methods (user.findMany(), post.create(), etc.)This service is then registered as a provider in a PrismaModule and exported for use throughout the application.
Sources: sample/22-graphql-prisma/package.json22-37
Prisma uses a declarative schema file (schema.prisma) to define the database structure. The schema is then used to generate both database migrations and the TypeScript client.
Diagram: Schema-to-Code Generation Flow
The sample application includes a dedicated script for generating TypeScript types:
| Script | Command | Purpose |
|---|---|---|
generate:typings | ts-node generate-typings.ts | Generates GraphQL TypeScript definitions |
This script likely generates TypeScript types from the GraphQL schema, which works in conjunction with Prisma-generated database types to provide end-to-end type safety from database to API.
Sources: sample/22-graphql-prisma/package.json9
The primary Prisma sample demonstrates integration with GraphQL using Apollo Server and NestJS GraphQL module. This combination provides type-safe database queries with auto-generated GraphQL resolvers.
Diagram: Prisma + GraphQL Integration Stack
| Package | Version | Purpose |
|---|---|---|
@apollo/server | 5.4.0 | GraphQL server implementation |
@nestjs/graphql | 13.2.4 | NestJS GraphQL module |
@nestjs/apollo | 13.2.4 | Apollo Server integration for NestJS |
graphql | 16.10.0 | GraphQL.js core library |
graphql-subscriptions | 3.0.0 | GraphQL subscription support |
class-transformer | 0.5.1 | Object transformation for DTOs |
class-validator | 0.14.3 | Decorator-based validation |
The combination of Prisma's type-safe database client with GraphQL's type-safe API layer provides compile-time guarantees across the entire stack.
Sources: sample/22-graphql-prisma/package.json22-37
Prisma supports multiple database providers through adapter packages. The sample application uses SQLite with the Better SQLite3 adapter, which is suitable for development and embedded use cases.
Diagram: Database Adapter Architecture
The adapter is specified in the datasource block of schema.prisma and determines which database driver is used at runtime. The Better SQLite3 adapter provides synchronous API access, making it particularly suitable for serverless environments and embedded databases.
Sources: sample/22-graphql-prisma/package.json29-30
Unlike TypeORM, Sequelize, and Mongoose, Prisma does not have a dedicated NestJS wrapper package. This table compares the integration approaches:
| ORM | NestJS Package | Integration Pattern | Type Generation |
|---|---|---|---|
| TypeORM | @nestjs/typeorm | Decorator-based entities, auto-wiring | Metadata reflection |
| Sequelize | @nestjs/sequelize | Model classes, auto-wiring | Decorator metadata |
| Mongoose | @nestjs/mongoose | Schema definitions, auto-wiring | Schema inference |
| Prisma | None (direct integration) | Manual service creation | CLI code generation |
Diagram: Prisma vs Traditional ORM Integration
Prisma's approach trades convenience (no forRoot() configuration) for explicit control and stronger type safety through build-time code generation rather than runtime reflection.
Sources: package.json99-100 sample/22-graphql-prisma/package.json22-37 sample/06-mongoose/package.json24
The repository includes a complete GraphQL + Prisma sample application demonstrating the integration pattern.
Diagram: Sample Application Structure
| Script | Command | Purpose |
|---|---|---|
prebuild | rimraf dist | Clean build directory |
build | nest build | Compile TypeScript to JavaScript |
generate:typings | ts-node generate-typings.ts | Generate GraphQL TypeScript definitions |
start:dev | nest start --watch | Start development server with hot reload |
The generate:typings script is specific to the GraphQL integration and generates TypeScript types from the GraphQL schema, complementing Prisma's database type generation.
Sources: sample/22-graphql-prisma/package.json1-67
The Prisma sample includes several development tools for code generation and type manipulation:
| Package | Version | Purpose |
|---|---|---|
prisma | ^7.0.0 | Prisma CLI for schema management and migrations |
ts-morph | 27.0.2 | TypeScript AST manipulation for code generation |
ts-node | 10.9.2 | TypeScript execution for build scripts |
The ts-morph package is particularly notable as it enables programmatic TypeScript code generation, likely used in the generate-typings.ts script to create GraphQL resolver types that integrate with Prisma's generated types.
Sources: sample/22-graphql-prisma/package.json57-62
Prisma integration requires proper TypeScript configuration to ensure generated types are recognized by the compiler.
strictNullChecks: Prisma's generated types rely on TypeScript's strict null checks to represent nullable database fields accuratelyskipLibCheck: Recommended to avoid type errors in the generated Prisma Clientnode_modules/@prisma/client by defaultThe build process follows this sequence:
Diagram: Build Process Flow
Sources: sample/22-graphql-prisma/package.json7-14
The sample application includes Jest configuration for unit and integration testing with Prisma:
| Configuration | Value | Purpose |
|---|---|---|
moduleFileExtensions | ["js", "json", "ts"] | Support TypeScript and JavaScript |
rootDir | "src" | Test files located in source directory |
testRegex | ".spec.ts$" | Match test files |
transform | "^.+\\.(t|j)s$": "ts-jest" | Transform TypeScript files |
testEnvironment | "node" | Use Node.js environment |
Testing with Prisma typically involves:
Sources: sample/22-graphql-prisma/package.json15-19
The NestJS repository demonstrates multiple database integration approaches, each suited to different use cases:
Diagram: Database Integration Options
For detailed information on these alternatives, see TypeORM Integration, Sequelize Integration, and Mongoose Integration.
Sources: sample/22-graphql-prisma/package.json sample/06-mongoose/package.json package.json99-100
Refresh this wiki