Managing Multiple Databases with Prisma ORM


Managing Multiple Databases with Prisma ORM

What is Prisma ORM?

Prisma ORM is a type-safe Object Relational Mapping (ORM) tool used in modern Node.js and TypeScript projects. It offers developers advantages such as easily managing database operations, automatic type generation, and facilitating transitions between databases. Prisma ORM stands out especially with its ability to control the database schema integrated with your code, powerful migration modules, and modular structure.

Why is Multiple Database Management Necessary?

In large-scale projects or microservice architectures, it may be necessary to store data in different databases. In some applications, using multiple databases is mandatory for reasons such as data security, performance, or regulations. Managing multiple databases with Prisma ORM simplifies these processes and gives control to the developer. Thus, even if your data models are in different sources, you can manage your code from a central point.

Configuring Multiple Databases with Prisma ORM

Preparing the Environment

First, add the prisma package to your project and then create separate prisma.schema files for each database. In this way, you can define independent configurations for each data source.

npm install prisma --save-dev
npx prisma init --schema=prisma/db1.prisma
npx prisma init --schema=prisma/db2.prisma

Defining Multiple Data Sources

You need to specify your own data source in each prisma.schema file. Below is an example schema for two different PostgreSQL databases:

// prisma/db1.prisma
datasource db1 {
  provider = "postgresql"
  url      = env("DATABASE1_URL")
}

generator client {
  provider = "prisma-client-js"
  output   = "../generated/db1"
}

model User {
  id   Int    @id @default(autoincrement())
  name String
}
// prisma/db2.prisma
datasource db2 {
  provider = "postgresql"
  url      = env("DATABASE2_URL")
}

generator client {
  provider = "prisma-client-js"
  output   = "../generated/db2"
}

model Product {
  id    Int    @id @default(autoincrement())
  title String
}

Using Prisma Client and Accessing from Within the Application

For each database, generate the Prisma Client and import it into your application as shown below:

// For db1
const { PrismaClient: PrismaClientDb1 } = require('./generated/db1');
const db1 = new PrismaClientDb1();

// For db2
const { PrismaClient: PrismaClientDb2 } = require('./generated/db2');
const db2 = new PrismaClientDb2();

// Usage example
db1.user.findMany().then(users => console.log(users));
db2.product.findMany().then(products => console.log(products));

Conclusion: The Advantages of Prisma ORM and Multiple Database Management

Managing multiple databases with Prisma ORM offers significant advantages in modern software development processes. You can centrally and type-safely manage data coming from different databases, making your code more maintainable and testable. Especially in projects with microservices, large scale, or multi-tenant requirements, this flexibility provided by Prisma increases your productivity.