Prisma

Prisma is an open-source ORM for Node.js and TypeScript. It is used as an alternative to writing plain SQL, or using another database access tool such as SQL query builders (like knex.js) or ORMs (like TypeORM and Sequelize). Prisma currently supports PostgreSQL, MySQL, SQL Server and SQLite.

While Prisma can be used with plain JavaScript, it embraces TypeScript and provides a level to type-safety that goes beyond the guarantees other ORMs in the TypeScript ecosystem. You can find an in-depth comparison of the type-safety guarantees of Prisma and TypeORM here.

info Note If you want to get a quick overview of how Prisma works, you can follow the Quickstart or read the Introduction in the documentation. There also are ready-to-run examples for REST and GraphQL in the prisma-examples repo.

Getting started

In this recipe, you’ll learn how to get started with NestJS and Prisma from scratch. You are going to build a sample NestJS application with a REST API that can read and write data in a database.

For the purpose of this guide, you’ll use a SQLite database to save the overhead of setting up a database server. Note that you can still follow this guide, even if you’re using PostgreSQL or MySQL – you’ll get extra instructions for using these databases at the right places.

info Note If you already have an existing project and consider migrating to Prisma, you can follow the guide for adding Prisma to an existing project. If you are migrating from TypeORM, you can read the guide Migrating from TypeORM to Prisma.

Create your NestJS project

To get started, install the NestJS CLI and create your app skeleton with the following commands:

  1. $ npm install -g @nestjs/cli
  2. $ nest new hello-prisma

See the First steps page to learn more about the project files created by this command. Note also that you can now run npm start to start your application. The REST API running at http://localhost:3000/ currently serves a single route that’s implemented in src/app.controller.ts. Over the course of this guide, you’ll implement additional routes to store and retrieve data about users and posts.

Set up Prisma

Start by installing the Prisma CLI as a development dependency in your project:

  1. $ cd hello-prisma
  2. $ npm install prisma --save-dev

In the following steps, we’ll be utilizing the Prisma CLI. As a best practice, it’s recommended to invoke the CLI locally by prefixing it with npx:

  1. $ npx prisma

Expand if you’re using Yarn

If you’re using Yarn, then you can install the Prisma CLI as follows:

  1. $ yarn add prisma --dev

Once installed, you can invoke it by prefixing it with yarn:

  1. $ yarn prisma

Now create your initial Prisma setup using the init command of the Prisma CLI:

  1. $ npx prisma init

This command creates a new prisma directory with the following contents:

  • schema.prisma: Specifies your database connection and contains the database schema
  • .env: A dotenv file, typically used to store your database credentials in a group of environment variables

Set the database connection

Your database connection is configured in the datasource block in your schema.prisma file. By default it’s set to postgresql, but since you’re using a SQLite database in this guide you need to adjust the provider field of the datasource block to sqlite:

  1. datasource db {
  2. provider = "sqlite"
  3. url = env("DATABASE_URL")
  4. }
  5. generator client {
  6. provider = "prisma-client-js"
  7. }

Now, open up .env and adjust the DATABASE_URL environment variable to look as follows:

  1. DATABASE_URL="file:./dev.db"

SQLite databases are simple files; no server is required to use a SQLite database. So instead of configuring a connection URL with a host and port, you can just point it to a local file which in this case is called dev.db. This file will be created in the next step.

Expand if you’re using PostgreSQL or MySQL

With PostgreSQL and MySQL, you need to configure the connection URL to point to the database server. You can learn more about the required connection URL format here.

PostgreSQL

If you’re using PostgreSQL, you have to adjust the schema.prisma and .env files as follows:

schema.prisma

  1. datasource db {
  2. provider = "postgresql"
  3. url = env("DATABASE_URL")
  4. }
  5. generator client {
  6. provider = "prisma-client-js"
  7. }

.env

  1. DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"

Replace the placeholders spelled in all uppercase letters with your database credentials. Note that if you’re unsure what to provide for the SCHEMA placeholder, it’s most likely the default value public:

  1. DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public"

If you want to learn how to set up a PostgreSQL database, you can follow this guide on setting up a free PostgreSQL database on Heroku.

MySQL

If you’re using MySQL, you have to adjust the schema.prisma and .env files as follows:

schema.prisma

  1. datasource db {
  2. provider = "mysql"
  3. url = env("DATABASE_URL")
  4. }
  5. generator client {
  6. provider = "prisma-client-js"
  7. }

.env

  1. DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/DATABASE"

Replace the placeholders spelled in all uppercase letters with your database credentials.

Create two database tables with Prisma Migrate

In this section, you’ll create two new tables in your database using Prisma Migrate. Prisma Migrate generates SQL migration files for your declarative data model definition in the Prisma schema. These migration files are fully customizable so that you can configure any additional features of the underlying database or include additional commands, e.g. for seeding.

Add the following two models to your schema.prisma file:

  1. model User {
  2. id Int @default(autoincrement()) @id
  3. email String @unique
  4. name String?
  5. posts Post[]
  6. }
  7. model Post {
  8. id Int @default(autoincrement()) @id
  9. title String
  10. content String?
  11. published Boolean? @default(false)
  12. author User? @relation(fields: [authorId], references: [id])
  13. authorId Int?
  14. }

With your Prisma models in place, you can generate your SQL migration files and run them against the database. Run the following commands in your terminal:

  1. $ npx prisma migrate dev --name init

This prisma migrate dev command generates SQL files and directly runs them against the database. In this case, the following migration files was created in the existing prisma directory:

  1. $ tree prisma
  2. prisma
  3. ├── dev.db
  4. ├── migrations
  5. └── 20201207100915_init
  6. └── migration.sql
  7. └── schema.prisma

Expand to view the generated SQL statements

The following tables were created in your SQLite database:

  1. -- CreateTable
  2. CREATE TABLE "User" (
  3. "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  4. "email" TEXT NOT NULL,
  5. "name" TEXT
  6. );
  7. -- CreateTable
  8. CREATE TABLE "Post" (
  9. "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  10. "title" TEXT NOT NULL,
  11. "content" TEXT,
  12. "published" BOOLEAN DEFAULT false,
  13. "authorId" INTEGER,
  14. FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE
  15. );
  16. -- CreateIndex
  17. CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");

Install and generate Prisma Client

Prisma Client is a type-safe database client that’s generated from your Prisma model definition. Because of this approach, Prisma Client can expose CRUD operations that are tailored specifically to your models.

To install Prisma Client in your project, run the following command in your terminal:

  1. $ npm install @prisma/client

Note that during installation, Prisma automatically invokes the prisma generate command for you. In the future, you need to run this command after every change to your Prisma models to update your generated Prisma Client.

info Note The prisma generate command reads your Prisma schema and updates the generated Prisma Client library inside node_modules/@prisma/client.

Use Prisma Client in your NestJS services

You’re now able to send database queries with Prisma Client. If you want to learn more about building queries with Prisma Client, check out the API documentation.

When setting up your NestJS application, you’ll want to abstract away the Prisma Client API for database queries within a service. To get started, you can create a new PrismaService that takes care of instantiating PrismaClient and connecting to your database.

Inside the src directory, create a new file called prisma.service.ts and add the following code to it:

  1. import { INestApplication, Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
  2. import { PrismaClient } from '@prisma/client';
  3. @Injectable()
  4. export class PrismaService extends PrismaClient
  5. implements OnModuleInit {
  6. async onModuleInit() {
  7. await this.$connect();
  8. }
  9. async enableShutdownHooks(app: INestApplication) {
  10. this.$on('beforeExit', async () => {
  11. await app.close();
  12. });
  13. }
  14. }

info Note The onModuleInit is optional — if you leave it out, Prisma will connect lazily on its first call to the database. We don’t bother with onModuleDestroy, since Prisma has its own shutdown hooks where it will destroy the connection. For more info on enableShutdownHooks, please see Issues with enableShutdownHooks

Next, you can write services that you can use to make database calls for the User and Post models from your Prisma schema.

Still inside the src directory, create a new file called user.service.ts and add the following code to it:

  1. import { Injectable } from '@nestjs/common';
  2. import { PrismaService } from './prisma.service';
  3. import {
  4. User,
  5. Prisma
  6. } from '@prisma/client';
  7. @Injectable()
  8. export class UserService {
  9. constructor(private prisma: PrismaService) {}
  10. async user(userWhereUniqueInput: Prisma.UserWhereUniqueInput): Promise<User | null> {
  11. return this.prisma.user.findUnique({
  12. where: userWhereUniqueInput,
  13. });
  14. }
  15. async users(params: {
  16. skip?: number;
  17. take?: number;
  18. cursor?: Prisma.UserWhereUniqueInput;
  19. where?: Prisma.UserWhereInput;
  20. orderBy?: Prisma.UserOrderByInput;
  21. }): Promise<User[]> {
  22. const { skip, take, cursor, where, orderBy } = params;
  23. return this.prisma.user.findMany({
  24. skip,
  25. take,
  26. cursor,
  27. where,
  28. orderBy,
  29. });
  30. }
  31. async createUser(data: Prisma.UserCreateInput): Promise<User> {
  32. return this.prisma.user.create({
  33. data,
  34. });
  35. }
  36. async updateUser(params: {
  37. where: Prisma.UserWhereUniqueInput;
  38. data: Prisma.UserUpdateInput;
  39. }): Promise<User> {
  40. const { where, data } = params;
  41. return this.prisma.user.update({
  42. data,
  43. where,
  44. });
  45. }
  46. async deleteUser(where: Prisma.UserWhereUniqueInput): Promise<User> {
  47. return this.prisma.user.delete({
  48. where,
  49. });
  50. }
  51. }

Notice how you’re using Prisma Client’s generated types to ensure that the methods that are exposed by your service are properly typed. You therefore save the boilerplate of typing your models and creating additional interface or DTO files.

Now do the same for the Post model.

Still inside the src directory, create a new file called post.service.ts and add the following code to it:

  1. import { Injectable } from '@nestjs/common';
  2. import { PrismaService } from './prisma.service';
  3. import {
  4. Post,
  5. Prisma,
  6. } from '@prisma/client';
  7. @Injectable()
  8. export class PostService {
  9. constructor(private prisma: PrismaService) {}
  10. async post(postWhereUniqueInput: Prisma.PostWhereUniqueInput): Promise<Post | null> {
  11. return this.prisma.post.findUnique({
  12. where: postWhereUniqueInput,
  13. });
  14. }
  15. async posts(params: {
  16. skip?: number;
  17. take?: number;
  18. cursor?: Prisma.PostWhereUniqueInput;
  19. where?: Prisma.PostWhereInput;
  20. orderBy?: Prisma.PostOrderByInput;
  21. }): Promise<Post[]> {
  22. const { skip, take, cursor, where, orderBy } = params;
  23. return this.prisma.post.findMany({
  24. skip,
  25. take,
  26. cursor,
  27. where,
  28. orderBy,
  29. });
  30. }
  31. async createPost(data: Prisma.PostCreateInput): Promise<Post> {
  32. return this.prisma.post.create({
  33. data,
  34. });
  35. }
  36. async updatePost(params: {
  37. where: Prisma.PostWhereUniqueInput;
  38. data: Prisma.PostUpdateInput;
  39. }): Promise<Post> {
  40. const { data, where } = params;
  41. return this.prisma.post.update({
  42. data,
  43. where,
  44. });
  45. }
  46. async deletePost(where: Prisma.PostWhereUniqueInput): Promise<Post> {
  47. return this.prisma.post.delete({
  48. where,
  49. });
  50. }
  51. }

Your UserService and PostService currently wrap the CRUD queries that are available in Prisma Client. In a real world application, the service would also be the place to add business logic to your application. For example, you could have a method called updatePassword inside the UserService that would be responsible for updating the password of a user.

Implement your REST API routes in the main app controller

Finally, you’ll use the services you created in the previous sections to implement the different routes of your app. For the purpose of this guide, you’ll put all your routes into the already existing AppController class.

Replace the contents of the app.controller.ts file with the following code:

  1. import {
  2. Controller,
  3. Get,
  4. Param,
  5. Post,
  6. Body,
  7. Put,
  8. Delete,
  9. } from '@nestjs/common';
  10. import { UserService } from './user.service';
  11. import { PostService } from './post.service';
  12. import { User as UserModel, Post as PostModel } from '@prisma/client';
  13. @Controller()
  14. export class AppController {
  15. constructor(
  16. private readonly userService: UserService,
  17. private readonly postService: PostService,
  18. ) {}
  19. @Get('post/:id')
  20. async getPostById(@Param('id') id: string): Promise<PostModel> {
  21. return this.postService.post({ id: Number(id) });
  22. }
  23. @Get('feed')
  24. async getPublishedPosts(): Promise<PostModel[]> {
  25. return this.postService.posts({
  26. where: { published: true },
  27. });
  28. }
  29. @Get('filtered-posts/:searchString')
  30. async getFilteredPosts(
  31. @Param('searchString') searchString: string,
  32. ): Promise<PostModel[]> {
  33. return this.postService.posts({
  34. where: {
  35. OR: [
  36. {
  37. title: { contains: searchString },
  38. },
  39. {
  40. content: { contains: searchString },
  41. },
  42. ],
  43. },
  44. });
  45. }
  46. @Post('post')
  47. async createDraft(
  48. @Body() postData: { title: string; content?: string; authorEmail: string },
  49. ): Promise<PostModel> {
  50. const { title, content, authorEmail } = postData;
  51. return this.postService.createPost({
  52. title,
  53. content,
  54. author: {
  55. connect: { email: authorEmail },
  56. },
  57. });
  58. }
  59. @Post('user')
  60. async signupUser(
  61. @Body() userData: { name?: string; email: string },
  62. ): Promise<UserModel> {
  63. return this.userService.createUser(userData);
  64. }
  65. @Put('publish/:id')
  66. async publishPost(@Param('id') id: string): Promise<PostModel> {
  67. return this.postService.updatePost({
  68. where: { id: Number(id) },
  69. data: { published: true },
  70. });
  71. }
  72. @Delete('post/:id')
  73. async deletePost(@Param('id') id: string): Promise<PostModel> {
  74. return this.postService.deletePost({ id: Number(id) });
  75. }
  76. }

This controller implements the following routes:

GET
  • /post/:id: Fetch a single post by its id
  • /feed: Fetch all published posts
  • /filter-posts/:searchString: Filter posts by title or content
POST
  • /post: Create a new post
    • Body:
      • title: String (required): The title of the post
      • content: String (optional): The content of the post
      • authorEmail: String (required): The email of the user that creates the post
  • /user: Create a new user
    • Body:
      • email: String (required): The email address of the user
      • name: String (optional): The name of the user
PUT
  • /publish/:id: Publish a post by its id
DELETE
  • /post/:id: Delete a post by its id

Issues with enableShutdownHooks

Prisma interferes with NestJS enableShutdownHooks. Prisma listens for shutdown signals and will call process.exit() before your application shutdown hooks fire. To deal with this, you would need to add a listener for Prisma beforeExit event.

  1. // main.ts
  2. ...
  3. import { PrismaService } from './services/prisma/prisma.service';
  4. ...
  5. bootstrap() {
  6. ...
  7. const prismaService: PrismaService = app.get(PrismaService);
  8. prismaService.enableShutdownHooks(app)
  9. ...
  10. }

You can read more about Prisma handling of shutdown signal, and beforeExit event.

Summary

In this recipe, you learned how to use Prisma along with NestJS to implement a REST API. The controller that implements the routes of the API is calling a PrismaService which in turn uses Prisma Client to send queries to a database to fulfill the data needs of incoming requests.

If you want to learn more about using NestJS with Prisma, be sure to check out the following resources: