Change database runtime

Hi, good morning, I have a question, is it possible to change the DB dynamically according to some condition that occurs in a middleware, in a generic way for all types of content?