Loopback + multi-database connection

I am using loopback framework with nodejs.

Is it possible to connect multiple databases simultaneously.

For example, I have two different databases.

1. Mysql Database - A 2. Postgresql - B 

Some pages retrieve data from database A , and some pages must retrieve data from database B. can this be done?

More details:

Let's say we have two modules. One module interacted with MySQL, and the other module interacted with postgreSQL.

+7
mysql postgresql loopbackjs strongloop
source share
1 answer

You can create multiple data sources inside datasources.json or dynamically create data sources. For your specific case, you need to install loopback-connector-mysql and loopback-connector -PosgreSQL

datasourcses.json

 { "mysql": { "name": "mysql", "connector": "mysql" }, "postgresql": { "name": "postgresql", "connector": "postgresql" } } 

Remember to add the host, port, username, password, and other properties to establish the connection correctly.

The next thing to do is use the attachTo () method to change the model data source when you want to switch the database.

 app.models.YourModel.attachTo(app.dataSources.mysql); ... or ... app.models.YourModel.attachTo(app.dataSources.postgresql); 

Also check this answer

+1
source share

All Articles