P
Prisma•8mo ago
timmy00979

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
RaphaelEtim•8mo 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
timmy00979OP•8mo 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
RaphaelEtim•8mo ago
You're welcome.
Want results from more Discord servers?
Add your server