KSDaemon/sails-hook-sequelize

The hook initializes only the default models connection, even if there are multiple connections defined in config.

gvko opened this issue · 3 comments

gvko commented

I've 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 the different models I've put the property connection, so that they're distinguished, as follows:

/**
  * model User
  */
    module.exports = {

      options: {
        connection: 'monolithMysql',
        ...
/**
  * model Token
  */
    module.exports = {

      options: {
        connection: 'postgres',
        ...

In /config/models.js I set as default connection the monolithMysql one. If I comment out or do not specify the connection property in /config/models.js then Sequelize hook fails to load.
But that should be overridden by the connection property, if specified in the models.

Nevertheless, when trying to query models that have, eg. postgres as connection it still queries them in the MySQL DB, and obviously fails... If I set postgres as default connection, then it will always query in the Postgres DB connection, no matter what the local connection property of the different models says.

After some debugging in the module code, I found out that it initializes only 1 instance of Sequelize - an instance with the default connection, specified in /config/models.js

Dialect: mysql and postgres
Sequelize version: ^3.21.0

gvko commented

Nevermind. It's not possible atm.
I'm writing an upgrade to support generic initialization of multiple connections.
Gonna make a PR today or tomorrow.

gvko commented

Aaand, here's the PR (sorry for the late comment. The PR was actually done the same day. :D )
#22

Now we're just waiting for @festo to review and merge...

P.S. The hook, with the extended functionality from my PR, has been running in a production environment, connected to a MySQL and a PostgreSQL databases, used by thousands of users daily, since May 2016, so, I guess it's kinda' proven that it works flawlessly.

I think current version supports multiple connections. Please check it out and feel free to reopen!