RSKMR
RSKMR

Reputation: 1892

Loopback + connect multiple database

I am using loopback framework with nodejs.

Is it possible to connect multiple database at a time.

For example I have two different database.

1. Mysql Database - A
2. Postgresql - B

Some pages get data from A database and some pages need get data from B database. could it be possible to do that?

More Details:

Lets say we have two modules.One module interacted with MySQL and another module interacted with postgreSQL.

Upvotes: 8

Views: 3446

Answers (1)

A.Z.
A.Z.

Reputation: 1648

You can create multiple datasources inside datasources.json or you can create datasources dynamically. For your particular case you have to install loopback-connector-mysql and loopback-connector-posgresql

datasourcses.json

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

Don't forget to add host, port, username, password and other properties to setup connection properly.

Next thing to do is to use attachTo() method to change model datasource when you want to switch database.

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

Also check this answer

Upvotes: 4

Related Questions