I want to use multiple database in prisma orm
Asked Answered
B

3

27

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.

Bulbul answered 7/9, 2021 at 9:21 Comment(1)
A little late to the party, but this article helped me a great deal setting up multiple database sources in prisma zach.codes/multiple-prisma-clients-one-appAssault
Q
28

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.

Quirita answered 8/9, 2021 at 5:24 Comment(1)
Tested and working in the same folder, separate files: /prisma/schema1.prisma adding the "output: "./generated/client1" parameter to the client and /prisma/schema2.prisma adding the "output: "./generated/client2" parameter to the client When generate need to use --schema parameter like this: npx prisma generate --schema prisma/schema2.prisma Tested with mysql 8, prisma 5.3.1 and next 13.4.19Demarcusdemaria
D
1

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
    }
  };
Delta answered 1/6, 2023 at 14:38 Comment(0)
A
0

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

Addle answered 7/9, 2021 at 17:48 Comment(2)
In this issue you can find the following solution, in my case, this was very useful: github.com/prisma/prisma/issues/2443#issuecomment-630679118Peggie
@Peggie that's the exact link from the top answer here. OP should be accepting that - it's the best approach currently.Endres

© 2022 - 2024 — McMap. All rights reserved.