2-setting-up-a-multi-tenant-postgresql-database-with-prisma-orm.html

Setting Up a Multi-Tenant PostgreSQL Database with Prisma ORM

In the modern era of web development, the demand for scalable applications has surged, leading to the rise of multi-tenant architectures. Multi-tenancy allows a single instance of an application to serve multiple customers (or tenants), improving resource utilization and reducing operational costs. In this article, we'll explore how to set up a multi-tenant PostgreSQL database using Prisma ORM, a powerful and intuitive Node.js ORM that simplifies database interactions.

What is Multi-Tenancy?

Multi-tenancy is an architectural design where a single instance of a software application serves multiple tenants. Each tenant's data is isolated and remains invisible to others, even though they share the same application and database resources.

Use Cases for Multi-Tenancy

  • SaaS Applications: Software-as-a-Service providers use multi-tenancy to serve multiple clients from a single platform.
  • Cost Efficiency: By sharing resources, companies can reduce hosting and maintenance costs.
  • Scalability: New tenants can be added with minimal disruption to existing services.
  • Centralized Management: Easier updates and maintenance since everything is managed from a single codebase.

Why Use PostgreSQL with Prisma ORM?

PostgreSQL is a powerful, open-source relational database known for its robustness and performance. Prisma ORM is a modern database toolkit that simplifies database access for Node.js and TypeScript applications. It offers type safety, auto-completion, and migrations, making it a perfect match for building scalable, multi-tenant applications.

Setting Up Your Environment

Prerequisites

Before diving into the setup, ensure you have the following installed:

  • Node.js: The latest LTS version.
  • PostgreSQL: A running instance of PostgreSQL.
  • Prisma CLI: Install it globally with the following command:
npm install -g prisma

Step 1: Initialize Your Node.js Project

Create a new directory for your project and initialize it:

mkdir multi-tenant-prisma
cd multi-tenant-prisma
npm init -y

Step 2: Install Required Packages

Install Prisma and the PostgreSQL client:

npm install prisma @prisma/client pg

Step 3: Initialize Prisma

Run the following command to set up Prisma:

npx prisma init

This command creates a prisma folder with a schema.prisma file, where you'll define your database schema.

Step 4: Configure PostgreSQL Connection

Open the prisma/schema.prisma file and modify the datasource section like so:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Then, create a .env file in the root of your project and add your PostgreSQL connection string:

DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"

Replace USER, PASSWORD, HOST, PORT, and DATABASE with your PostgreSQL credentials.

Step 5: Define the Schema for Multi-Tenant Architecture

Next, we’ll define a model in schema.prisma that accommodates multiple tenants. A common approach is to use a Tenant model and associate it with other models. Here's an example:

model Tenant {
  id        Int      @id @default(autoincrement())
  name      String   @unique
  users     User[]
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  tenantId  Int
  tenant    Tenant   @relation(fields: [tenantId], references: [id])
}

In this schema:

  • The Tenant model represents each unique tenant.
  • The User model includes a foreign key tenantId to link users to their respective tenants.

Step 6: Run Migrations

After defining your schema, create the database tables by running:

npx prisma migrate dev --name init

This command generates the migration files and applies them to your PostgreSQL database.

Step 7: Interact with Your Database Using Prisma Client

Now that your database is set up, you can start interacting with it using Prisma Client. Create a file named index.js and write the following code to connect to your database and perform CRUD operations:

const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
  // Create a new tenant
  const tenant = await prisma.tenant.create({
    data: {
      name: 'Tenant A',
      users: {
        create: {
          email: 'user1@tenantA.com'
        }
      }
    }
  });

  console.log('Created tenant:', tenant);

  // Fetch tenants with their users
  const tenants = await prisma.tenant.findMany({
    include: {
      users: true,
    },
  });

  console.log('All tenants:', tenants);
}

main()
  .catch(e => console.error(e))
  .finally(async () => {
    await prisma.$disconnect();
  });

Step 8: Run Your Application

Execute the following command to run your script:

node index.js

You should see the created tenant and its users printed in the console.

Troubleshooting Common Issues

  • Connection Errors: Ensure your PostgreSQL server is running and your connection string is correct.
  • Migration Issues: If you encounter errors during migration, ensure your schema.prisma file is correctly defined and run npx prisma migrate reset to reset your migrations (be cautious as this deletes data).
  • TypeScript Issues: If using TypeScript, install the necessary types and ensure your configurations are correct.

Conclusion

Setting up a multi-tenant PostgreSQL database with Prisma ORM can significantly streamline your development process, allowing you to focus on building features rather than managing database connections. By following the steps outlined in this article, you can create a scalable, efficient architecture that serves multiple tenants seamlessly. Whether you're building a SaaS application or a platform for various clients, embracing multi-tenancy with PostgreSQL and Prisma can elevate your project to new heights. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.