'Dynamic database connection/initialization in typeorm `0.3.6` with Express

typeorm 0.3.6 deprecated createConnections() and and introduced DataSource. It is causing some issues with multi-tenant architecture.

Either I have to initialize() all the data sources with different databases in the bootstrap index.ts or have to initialize() the Datasource much later where I have access to req. Because based on a request parameter, I know which database to use.

Is it possible to dynamically initialize all the data sources during bootstrap in index.ts? Currently, I do like

Promise.all([dss.AppDataSource.initialize(), dss.AppDataSource2.initialize()])
.then((values) => {
  //console.log(values);
  console.log('... ... ... all Database initialized');
})
.catch(error => {
  console.error(error.message)
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source