5

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.

Rez
  • 514
  • 1
  • 9
  • 31
  • Create your own Database module, and pass a database name to provider, or uri that you want to connect with, store somewhere these information with relations to the connection, like string -> connection, fe: Map with DB URI or DB Name with key as connection handler. Then you should be able to achieve your goal. – cojack Oct 09 '20 at 06:51
  • @cojack, thanks. but can you give an example on how to do that? – Rez Oct 10 '20 at 04:02

1 Answers1

1
@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

Ashok
  • 3,190
  • 15
  • 31