Connect to multiple db?

Hey guys is there a way to connect to different db on user selection? for example i have 3 databases 1 for each project and i want to select which app to use and then connect to this db to fetch data is it possible?
1 Reply
Prisma AI Help
You opted for speed from this bot. Let's conjure up an answer right now! Yes, it is possible to connect to different databases based on user selection using Prisma. You can achieve this by creating multiple PrismaClient instances, each configured to connect to a different database. Here's how you can do it: 1. Define Multiple Datasources: You can define multiple datasources in your Prisma schema, each pointing to a different database. However, Prisma currently does not support multiple datasources in a single schema file directly. Instead, you can manage this by creating separate schema files for each database. 2. Create Multiple PrismaClient Instances: You can create different PrismaClient instances for each database by overriding the connection string at runtime. Here's an example of how you can do this:
import { PrismaClient } from '@prisma/client';

// Create a PrismaClient instance for each database
const client1 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db1' }} });
const client2 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db2' }} });
const client3 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db3' }} });

// Use the appropriate client based on user selection
function getClientForUserSelection(selection) {
switch (selection) {
case 'project1':
return client1;
case 'project2':
return client2;
case 'project3':
return client3;
default:
throw new Error('Invalid selection');
}
}

import { PrismaClient } from '@prisma/client';

// Create a PrismaClient instance for each database
const client1 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db1' }} });
const client2 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db2' }} });
const client3 = new PrismaClient({ datasources: { db: { url: 'postgres://localhost/db3' }} });

// Use the appropriate client based on user selection
function getClientForUserSelection(selection) {
switch (selection) {
case 'project1':
return client1;
case 'project2':
return client2;
case 'project3':
return client3;
default:
throw new Error('Invalid selection');
}
}

3. Dynamic Connection Strings: You can also dynamically set the connection string based on user input. This can be done by changing the environment variable or using the datasourceUrl property to override it at runtime. More details can be found in the Prisma Client Reference. For more detailed information, you can refer to the discussion on Multiple Con nections / Databases / Datasources and Dynamic Database Connection String. By following these steps, you can effectively manage connections to multiple databases based on user selection. ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?