How to dynamically connect to a database in nestjs?

Viewed 1580

I have multiple databases that I want to connect. but just one of them have static name. for example the name of that database is stores, and in that I have a table that indicates the name of the other databases. now I want to change the connection provider after the first route being called. how can I do that?

I tried to use the Dynamic modules, but I don't know how to use it.

1 Answers
@Injectable({ scope: Scope.REQUEST })
export class MongooseConfigService implements MongooseOptionsFactory {
    constructor(
        @Inject(REQUEST) private readonly request: Request,) {
    }

    createMongooseOptions(): MongooseModuleOptions {
        return {
            uri: request.params.uri, // Change this to whatever you want
        };
    }
}

You can read more about Injection Scopes

And can see the same issue here

Related