Use multiple datastore connections with Sequelize

I defined 2 connections in the /config/connections.js file:

  monolithMysql: { user: 'store_app', database: 'store', dialect: 'mysql', options: { dialect: 'mysql', host: 'dockerhost', port: 3306, logging: console.log } }, postgres: { user: 'user_app', database: 'user_authentication', dialect: 'postgres', options: { dialect: 'postgres', host: 'dockerhost', port: 8201, logging: console.log } } 

and in different models I put the connection property so that they differ as follows:

 module.exports = { options: { connection: 'monolithMysql', // or 'postgres' in other models. ... 

In /config/models.js I set the default connection monolithMysql . But this should be overridden by the connection property, if specified, in models. If I comment or do not specify the connection property in /config/models.js , then the bootloader with the Sequelize label cannot be loaded.

However, when trying to query models having postgres as the connection, it still queries them in the MySQL database and fails ... If I set postgres as the default connection, then it will always query in this database, regardless of what the local connection property says for different models.

Any suggestions for setting up two connections at the same time?

Update: it turned out that it initializes only 1 instance of Sequelize - the instance with the default connection specified in /config/models.js

0
source share
1 answer

In the end, I ended up writing an upgrade to sails-hook-sequelize to support multiple database connection instances at the same time.

At this point (April 13, '16), the pull request was not considered, but it passed all the tests and CI checks, so no worries. (the repo owner seems to be neglecting PR reviews over the past couple of months).

You can find the pull request here .

To the module, along with updating the connections of several db, in your package.json file, just write:

 "sails-hook-sequelize": " git@github.com :galioy/sails-hook-sequelize.git#f0c0c5d72ee97ac5504e6f3a0825e37c3587909a" 

The configuration is basic, as you usually do this for Sequelize:

  • add connection configurations to /config/connections.js (see OP)

  • for each model for which you want to use a secondary connection, just add the connection name to the options.connection property in the model itself (see OP).

For a detailed explanation, see the main pull-request comment.

0
source

All Articles