将多个数据存储连接与 Sequelize 结合使用 [英] Use multiple datastore connections with Sequelize

查看:41
本文介绍了将多个数据存储连接与 Sequelize 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 /config/connections.js 文件中定义了 2 个连接:

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
    }
  }

并且在不同的模型中我已经放置了属性connection,以便区分它们,如下所示:

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.
    ...

/config/models.js 中,我将 monolithMysql 设置为默认连接.但这应该被模型中的 connection 属性覆盖(如果指定).如果我在 /config/models.js 中注释掉或未指定 connection 属性,则 Sequelize 钩子无法加载.

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.

然而,当尝试查询以 postgres 作为连接的模型时,它仍然在 MySQL 数据库中查询它们,但失败了......如果我将 postgres 设置为默认值连接,那么它将始终在该数据库中进行查询,无论不同模型的本地 connection 属性是什么.

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.

有什么建议可以同时设置 2 个连接吗?

Any suggestions how to setup 2 connections at the same time?

更新:发现它只初始化了 1 个 Sequelize 实例 - 一个具有默认连接的实例,在 /config/models.js

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

推荐答案

最终,我写了升级到 sails-hook-sequelize 同时支持多个数据库连接实例.

Eventually, I ended up writing an upgrade to sails-hook-sequelize to support multiple database connection instances simultaneously.

此时(2016 年 4 月 13 日)尚未审查拉取请求,但它已通过所有测试和 CI 检查,所以不用担心.(最近几个月,repo 的所有者似乎忽略了 PR 审查).

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).

您可以在此处找到拉取请求.

为了需要模块,以及多个数据库连接升级,在你的package.json文件中,只需写:

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"

配置是基本的,就像你通常为 Sequelize 做的那样:

The configuration is the basic one, as you would do it normally for Sequelize:

  1. 将连接配置添加到/config/connections.js(见OP)

对于要使用辅助连接的每个模型 - 只需添加连接到模型本身中的 options.connection 属性的名称(请参阅操作).

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).

详细解释见pull-request.

这篇关于将多个数据存储连接与 Sequelize 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆