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.
I want to use multiple database in prisma orm
Asked Answered
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-app –
Assault
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.
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.19 –
Demarcusdemaria
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
}
};
It is not possible right now, see https://github.com/prisma/prisma/issues/2443
In this issue you can find the following solution, in my case, this was very useful: github.com/prisma/prisma/issues/2443#issuecomment-630679118 –
Peggie
@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.