19

I am not able to use multiple database in same application . How can we use multiple data sources. Can we generate multiple "schema.prisma" for different database connections.

  • A little late to the party, but this article helped me a great deal setting up multiple database sources in prisma https://zach.codes/multiple-prisma-clients-one-app/ – PedroMiotti Nov 27 '22 at 20:00

3 Answers3

21

You actually can with a workaround as specified here. Create two different schema.prisma in separate folders and initialise PrismaClient for each schema.prisma that will point to the specific database.

Ryan
  • 5,229
  • 1
  • 20
  • 31
0

It is not possible right now, see https://github.com/prisma/prisma/issues/2443

Danila
  • 15,606
  • 2
  • 35
  • 67
  • 2
    In this issue you can find the following solution, in my case, this was very useful: https://github.com/prisma/prisma/issues/2443#issuecomment-630679118 – Gaalvarez Feb 11 '22 at 20:42
  • @Gaalvarez that's the exact link from the top answer here. OP should be accepting that - it's the best approach currently. – bsplosion Dec 19 '22 at 18:09
0

Video reference from codeGrepper generate two prisma schema one by default scehma.prisma and another accoring to your will 2) in second scehema generate extra output path where prisma client will store //prisma/retspy.prisma

 generator client {
        provider = "prisma-client-js"
        output   = "client/retspy"
    }

datasource db {
    provider = "mysql"
    url      = env("RETSPY_DATABASE_URL")
}

3)Generate client

import { PrismaClient as NcomDBPrismaClient } from '@prisma/client';
import { PrismaClient as RetspyPrismaClient } from '../../prisma/client/retspy';

declare global {
  var ncomDBPrisma: NcomDBPrismaClient | undefined;
  var retspyPrisma: RetspyPrismaClient | undefined;
}   

const ncomDBClient = globalThis.ncomDBPrisma || new NcomDBPrismaClient();
const retspyClient = globalThis.retspyPrisma || new RetspyPrismaClient();

// Next.js hot reloading can cause issues with Prisma, so we store the clients globally.
if (process.env.NODE_ENV !== 'production') {
  globalThis.ncomDBPrisma = ncomDBClient;
  globalThis.retspyPrisma = retspyClient;
}

export { ncomDBClient, retspyClient };
export default ncomDBClient;

//use both client according to will

import { retspyClient } from "@/app/libs/prismadb";
 const fetchData = async () => {
    try {
      const data = await retspyClient.property_for_sale.findFirst({
        where: {
          state_or_province: {
            contains: "Albert",
          },
        },
      });
      // console.log(data);
      return data; // Return the fetched data
    } catch (err) {[enter link description here][1]
      console.log(err);
      return null; // Return null if there's an error
    }
  };
Shirshak kandel
  • 328
  • 3
  • 7