---
title: "Discussion on \"Schema-Based Multi-Tenancy with NestJS and Prisma\" | Hashnode"
description: "Discussion on \"Schema-Based Multi-Tenancy with NestJS and Prisma\". We recently migrated a big NestJS project to adopt Prisma as the ORM. It turned out to be a great decision, and I described our wonderful experience in a recent blog post.\nThe project is an API backend for a multi-tenant system, where each tenant has..."
url: "https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Schema-Based Multi-Tenancy with NestJS and Prisma](https://darioielardi.dev/schema-based-multi-tenancy-with-nestjs-and-prisma?utm_source=hashnode&utm_medium=feed)

We recently migrated a big NestJS project to adopt Prisma as the ORM. It turned out to be a great decision, and I described our wonderful experience in a recent blog post. The project is an API backend for a multi-tenant system, where each tenant has...

Dec 22, 2020 · 6 min read · darioielardi.dev  
Updated Mar 29, 2022  
Read the article

-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#postgresql](https://hashnode.com/tag/postgresql)
-   [#multi-tenant](https://hashnode.com/tag/multi-tenant)

## Written by

[Dario Ielardi](https://hashnode.com/@darioielardi)

@darioielardi · Rome, Italy

  
[View profile](https://hashnode.com/@darioielardi)

Fullstack Web Developer & Traveler. CTO & Co-Founder @myothis

## Responses 6

Markdown supported · ⌘↵ to post

[Levandoski Levi](https://hashnode.com/@levandoski)

[Feb 24, 2024](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/65da03152135f691ea3796c8)

[Dario Ielardi](https://hashnode.com/@darioielardi) Thank you very much for this article. I have a project to develop that respects this architecture. I would like to know if the tenant databases must be created manually? if I have a frontent with angular for example, how to identify a tenant when creating an account from the front for example and save it in the right database?. A code example would help me more. Thanks in advance

[Ali Toshmatov](https://hashnode.com/@alitoshmatov)

[Aug 25, 2022](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/6306edcb79f56556d0cb8f69)

Do you have any tips on how to manage migrations on multi-tenant schema

[Antonio Jose](https://hashnode.com/@antonio609)

[Nov 9, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/618aad5cf991b025366dff68)

This is wonderfull!!!

[Duane Creates](https://hashnode.com/@duanecreates)

[Aug 12, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/611585f71f1bd93c4f35b8de)

Hi, I'm curious. Since you have a database per tenant, how do you make sure all those databases share the same structure and get all the latest migrations?

[Dario Ielardi](https://hashnode.com/@darioielardi)

[Aug 13, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/611585f71f1bd93c4f35b8de#reply-6116598913a99e48922ee7c8)

Hi, We have a non-breaking migrations policy and we migrate with a custom script to keep schemas in sync.

[Carlos Perché](https://hashnode.com/@cfpperche)

[Sep 28, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/611585f71f1bd93c4f35b8de#reply-6153ab669c8bfd08f5ba7e06)

[Dario Ielardi](https://hashnode.com/@darioielardi) Could you give a brief example of how to synchronize tenants' databases synced?

[Antonio Jose](https://hashnode.com/@antonio609)

[Nov 9, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/611585f71f1bd93c4f35b8de#reply-618aaca8468c06249afc51ae)

[Dario Ielardi](https://hashnode.com/@darioielardi) How you synchronize the shemas? How you run your migrations?

[Yoel Navas Escalante](https://hashnode.com/@ynavas)

[Apr 11, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/6073634b1494367be6d2b4ec)

Very neat solution, congrats; I just want to point out a little detail (and please correct me if I'm wrong), you don't need to explicitly make prismaClientProvider request-scpoed, since you are injecting REQUEST into it, wich is in itself a request-scpoed provider, then his scope is bubbled up by the injection chain.

[Dario Ielardi](https://hashnode.com/@darioielardi)

[Apr 12, 2021](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/6073634b1494367be6d2b4ec#reply-6074849385f3a21e3926c07c)

Hello, thank you for your kind words.

You're right, there's no need to explicitly make the custom provider request-scoped, I did that just to be totally explicit about the intended behavior.

[Jonathan Mitnik](https://hashnode.com/@Jonik)

[Dec 22, 2020](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/5fe1d42faaa31068bc22b430)

Hi Dario,

thanks for the excellent write-up!

I was wondering, what alternative would you consider if you do have potentially thousands of tenants? I suppose you would then simply have to create the prisma connections based on incoming request?

[Dario Ielardi](https://hashnode.com/@darioielardi)

[Dec 22, 2020](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/5fe1d42faaa31068bc22b430#reply-5fe1e1fe2d84df6a9bc97c38)

Hi Jonathan, thanks for your kind words!

In a situation where the number of tenants is so big I'd probably avoid a schema-based architecture, and rather go with row-level data separation. That way I'd only need one database connection for one Prisma client.

[Jonathan Mitnik](https://hashnode.com/@Jonik)

[Dec 22, 2020](https://hashnode.com/posts/schema-based-multi-tenancy-with-nestjs-and-prisma/5fe1c2b1a669d865551f6756/comment/5fe1d42faaa31068bc22b430#reply-5fe1e255227cbd6ac0517717)

Ah cool, indeed that's what we are currently "confined" to so to speak as well, nothing to do about it I suppose :). Thanks for your input on it!
