How to connect multiple databases with NestJs and Prisma
6 min read
nestjsprismadatabasebackend
How to connect multiple databases with prisma and nestjs using multiple schema by generating multiple prisma clients
Why do you need multiple database connections?
You don’t typically need to use multiple databases. If you’re not sure why you probably don’t need it.
But you could be in a situation where you have an old database that’s maintained by a different API and you’re using a separate database for your new API and you need to use both databases, you can set up Prisma to do so.
For the database, we will use MySQL image with docker-compose.
Create a docker-compose.yml
Add variables to .env
Prisma needs to have a shadow database for it to be able to run migrations safely so let’s create a shadow database and a second database to demonstrate multiple database connections with Prisma.
Create a db/init.sql
This will create two databases, and grant all privileges for our user prisma
Let’s update docker-compose.yml to set db/init.sql as entry point for our MySQL container. In your compose file, replace the previous volumes with
Run the container with docker-compose up -d and start the development server with npm run start:dev
Set the first database connection
To keep things simple we will only create a User model with id and name. Now let’s add a script to run the migration in our package.json
And run the command npm run migrate This will generate a Prisma client inside node_modules/.prisma/client , generate migration files and create appropriate tables in our prisma database
Install and generate Prisma Client
Create prisma.service.ts in src folder
Update the API to get the list of users
You can create a route to add users and once you do that, you will get the list of the users.
Set the second database connection
Create a schema.prisma inside a different folder
For the second connection, the output directory for the client needs to point to a different directory. We will place it inside node_modules/@internal/prisma/client
Generate migrations
Let’s add a new command in package.json to generate migrations for the second schema file
Add BLOG_DATABASE_URL to .env file
Now when you run
a new folder @internal/prisma/client will be generated.
If you have an old database with data in it, you don’t need to generate migrations files. Instead of running migrate dev, you can generate Prisma client by running
Create Prisma service
We need to create a separate prisma.service that will connect with the second database. This service will be the same but the PrismaClient will be imported from the newly generated @internal/prisma/client instead of the default @prisma/client
Now let’s update the API to list the blogs using the second database connection
And there you have it. You now have connections to both databases with Prisma.
Tip:
If you’re running CI pipelines, you might come across an issue where @internal/prisma/client is not found. To resolve this, you can add a postinstall script in package.json which will run automatically after installing packages and generate the prisma clients for us.
Conclusion
You can use multiple databases with Prisma by creating different schema files and generating separate Prisma clients for each database.
If there’s a better way to do this, please leave some comments.