问题
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:
module.exports = {
options: {
connection: 'monolithMysql', // or 'postgres' in other models.
...
In /config/models.js
I set as default connection the monolithMysql
one. But that should be overridden by the connection
property, if specified, in the models. If I comment out or do not specify the connection
property in /config/models.js
then Sequelize hook fails to load.
Nevertheless, when trying to query models that have postgres
as connection, it still queries them in the MySQL DB, and fails... If I set postgres
as default connection, then it will always query in that DB, no matter what the local connection
property of the different models says.
Any suggestions how to setup 2 connections at the same time?
Update: found out that it initializes only 1 instance of Sequelize - an instance with the default connection, specified in /config/models.js
回答1:
Eventually, I ended up writing an upgrade to sails-hook-sequelize to support multiple database connection instances simultaneously.
The pull-request hasn't been reviewed at this point (13 April '16), but it has passed all tests and CI checks, so no worries. (the owner of the repo seems to be neglecting PR reviews in the last couple of months).
You can find the pull-request here.
In order to require the module, along with the multiple db connections upgrade, in your package.json
file, just write:
"sails-hook-sequelize": "git@github.com:galioy/sails-hook-sequelize.git#f0c0c5d72ee97ac5504e6f3a0825e37c3587909a"
The configuration is the basic one, as you would do it normally for Sequelize:
add connections configurations to
/config/connections.js
(see OP)for each model that you want to use secondary connection - just add the name of the connection to
options.connection
property in the model itself (see OP).
For detailed explanation, see the main comment of the pull-request.
来源:https://stackoverflow.com/questions/36565379/use-multiple-datastore-connections-with-sequelize