Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add the "a single DB for all tenants" model of multi-tenancy

See original GitHub issue

Currently, Prisma-multi-tenant uses the “one DB per tenant” model of multi-tenancy. However, we could also add the possibility to use the “a single DB for all tenants” model of multi-tenancy.

Here is how it would work:

  1. pmt init
    • Asks which mode to use
    • If SingleDB, add a “Tenant { id String @id }” model, add “tenantId: String @default(‘dev’)” on all models
    • Migrate DB, then creates the first tenant: “dev”
  2. const multiTenant = new MultiTenantSingleDB({sharedModels: []})
    • An option for “shared models” which are shared between tenants (ex: Users?)
  3. const prisma = multiTenant.get('tenant_A')
    • Adds a middleware to the PrismaClient that adds { where: { tenantId: 'tenant_A' } } on every queries
  4. prisma.users.findMany()
    • Same as always!


Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

Dhaliascommented, May 11, 2022

Hey guys ! Any news about single DB for all tenants being supported by Prisma ? As stated, for SaaS entrepreneur this would be a massive plus for Prisma.

BjoernRavecommented, Nov 1, 2020

this is my WIP take on how to solve this:

export const injectTenant = (prisma: PrismaClient, subDomain: string) => {
  prisma.$use(async (params, next) => {
    if (params.model === 'Setting') {
      return next(params)

    if (params.action === 'delete') {
      return next(params)

    if (['create', 'update'].includes(params.action)) { = { ...params?.args?.data, tenantId: subDomain }

      return next(params)

    if (!params?.args) {
      params = { ...params, args: {} }

    if (!params?.args?.where) {
      params = { ...params, args: { ...params.args, where: {} } }

    if (params.action === 'findOne') {
      params.action = 'findFirst'

      params.args.where = Object.keys(params.args.where).reduce(
        (prev, next) => {
          return { ...prev, [next]: { equals: params.args.where[next] } }

    if (params?.args?.where?.AND) {
      params.args.where = {
        AND: [{ tenantId: { equals: subDomain } }, ...params.args.where.AND],
    } else {
      if (params?.args?.where && Object.keys(params?.args?.where).length > 0) {
        params.args.where = {
          AND: [
            { tenantId: { equals: subDomain } },
            ...Object.keys(params.args.where).map((key) => ({
              [key]: params.args.where[key],
      } else {
        params.args.where = {
          tenantId: { equals: subDomain },

    return next(params)

Every model in the db has a tenantId field, which sadly has to be marked optional, since the prisma calls dont know a middleware is injecting the tenantId

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-tenant SaaS patterns - Azure SQL Database
The simplest multi-tenant database pattern uses a single database to host data for all tenants. As more tenants are added, the database is ......
Read more >
Multi-tenancy in SingleStoreDB
In this model, you have one master schema and one version of each of your tables. All customer data is stored in the...
Read more >
How to use laravel multi tenant (stancl/tenancy) with single DB ?
We have to add BelongsToTenant trait to all of our tenant-specific models. Say if we want to add tenant_id into the users table...
Read more >
Multi-Tenancy Database Design Approaches with SQL Server ...
Approach #1: Single Database, Shared Schema · One database to hold the data for all tenants · Every tenant's data is stored in...
Read more >
Multi-tenant Application Database Design | by Blake Howe
As the name implies, a tenant (organization) has its own database. Each time a new tenant is added to the system, a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found