trilliumPrisma with NestJS

We'll configure your environment, perform migrations for each database, and interact with them using Prisma's powerful query engine by creating separate Prisma services


circle-info

This is a tutorial that provides an overview of using the tool. There are many ways to code the logic and organize your code. It's up to you to design your API according to your specific needs. My role is to guide you and offer a solid foundation.

Introduction to Prisma

Prisma is an open-source database toolkit that simplifies database interactions by providing:

  • Type-safe database queries

  • Easy-to-use migrations

  • Auto-generated database schemas and models

  • Excellent support for relational databases like MySQL, PostgreSQL, SQLite, and more.

Prisma integrates seamlessly with NestJS applications, enhancing database access safety and efficiency. This guide extends the integration to handle multiple MySQL databases within a single NestJS project.


Setting Up Prisma with NestJS

Step 1: Install Prisma CLI and Prisma Client

First, install Prisma and its necessary dependencies. Run the following commands in your project directory:

npm install prisma --save-dev
npm install @prisma/client

Step 2: Initialize Prisma for Multiple Databases

Since you're working with multiple databases, it's recommended to create separate Prisma schemas for each database. This approach ensures clear separation and easier management.

  1. Create Separate Schema Files

    Create two schema files: schema_db1.prisma and schema_db2.prisma inside a new prisma directory.

  2. Initialize Each Schema

    For each schema, specify the datasource and generator.

Step 3: Configure Database Connections

Your .env file is already set up to handle multiple databases. Here's how it looks:


Generating Prisma Clients

Step 1: Define Your Data Models

Define the data models for each database in their respective schema files.

prisma/schema_db1.prisma

prisma/schema_db2.prisma

In these schema files:

  • Datasources: Define the connection to each MySQL database using environment variables.

  • Generators: Generate separate Prisma clients for each database, outputting them to different directories.

  • Models: Define the User model for db1 and the Product model for db2.

Step 2: Generate Prisma Clients

Add the following scripts to your package.json to handle migrations and client generation for each database:

Run the following commands to generate the Prisma clients:

This will create two separate Prisma clients:

  • @prisma/client_db1 for db1

  • @prisma/client_db2 for db2


Configuring Prisma in Modules

Step 1: Create Prisma Services

Create separate Prisma services for each database to manage their respective Prisma clients.

Create src/prisma/db1.service.ts

Create src/prisma/db2.service.ts

Step 2: Create Prisma Modules

Create separate Prisma modules to provide the respective Prisma services.

Create src/prisma/db1.module.ts

Create src/prisma/db2.module.ts

Update src/app.module.ts to Import Prisma Modules


Running Migrations

Run migrations for each database separately using the scripts defined earlier.

For db1:

For db2:

These commands will generate and apply SQL migration files to their respective databases based on the defined schemas.


Using Prisma in Services

Step 1: Inject Prisma Services in Your Services

Create separate services for each database, injecting the respective Prisma service.

Create src/user/user.service.ts for db1

Create src/product/product.service.ts for db2

Step 2: Using the Services in Controllers

Create controllers to expose API endpoints for each service.

Create src/user/user.controller.ts

Create src/product/product.controller.ts

Register Services and Controllers in Their Modules

src/user/user.module.ts

src/product/product.module.ts

Now, you have separate APIs for managing users (db1) and products (db2), each interacting with their respective databases.


Handling Errors

Prisma throws specific errors when operations fail. Handle these errors gracefully in each service.

Example: Handling Unique Constraint Error in UserService


Example: Handling Errors in ProductService

This error handling ensures that Prisma errors are caught and meaningful HTTP exceptions are thrown, providing clear feedback to API clients.

Last updated