Dynamically changing DB connection during runtime

Hey y'all! Been looking far and wide and hoping someone has suggestions. Currently trying to figure out how to dynamically change a Prisma connection to a new database. We currently have about 800+ databases that all have the same schema, but need to be able to change databases based on certain params. So like our current Rails API setup is using the built in sharding that came from Octopus. We check a header value from the request and then change the database connection from that value. I'm hoping to achieve something similar where we use some type of hook, grab a value, and then adjust the Prisma connection accordingly. Has anyone achieved something similar? Thank you!
3 Replies
RaphaelEtim
RaphaelEtim3mo ago
Hi @timmy00979 👋 Would programmatically overriding the datasource url work for your usecase?
Prisma Client API | Prisma Documentation
API reference documentation for Prisma Client.
timmy00979
timmy009793mo ago
After spending all weekend looking into this, ^ that's what we gotta do, but the core of our issue is the library we pass Prisma to is a singleton. So nothing to do with Prisma! All of our microservices are currently using Prisma successfully for the connection switching. We're going to extend the class to accept a function that returns a factory for generating the Prisma connection. Writing software for our multi-tenent setup is always difficult unless we're on Rails lol. Thank you!
RaphaelEtim
RaphaelEtim3mo ago
You're welcome.
Want results from more Discord servers?
Add your server
More Posts
How to Handle Case Insensitivity?This is giving me an error with findFirst.Inconsistent query result: Field topics is required to return data, got `null` instead.All data exists in the db but this still happens. ``` where: {id}, include: { aDoes Prisma Studio work without Node (i.e. with Bun)I've got an environment where Node is not installed. I'm currently running the entire project with BHow to run safe migrationshello guys I am new to Backend. i am using prisma. and I have a hairstyle table containing hairstylPrisma Warn Console in developmentI have encountered the following warning message: prisma:warn. Despite searching extensively in the Type 'string[]' is not assignable to type 'string'.ts(2322) when trying to create a form using Prismso I'm trying to create a submitForm using prisma, react-hook-form and zod and mysql as my database.Strange @relation fields in auto-generated modelsUsing `prisma@5.13.0`, I ran `npx prisma db pull` to generate models for an existing SQLite databaseI feel this is simple but I fear i've just been at it too long I'm starting to not see straight.I have a parent record with a many to many relationship with a child, such as: ```js model User { findMany query is failing as it's breaching planetscale's 100k limit but rawquery is working fineIs findMany fetch all records before it applies distinct condition? await prisma.table.findMany({ Pulse listening to all schemas in my RDS instanceI want pulse to only subscribe to changes within one schema but it gives me changes of all schema, i