【问题标题】:How to define two MySQL database connections in Sails.js如何在 Sails.js 中定义两个 MySQL 数据库连接
【发布时间】:2014-07-16 08:44:59
【问题描述】:

使用sails.js v0.10 rc8,我的应用有1个到mysql数据库的连接,我希望给定模型的数据是从另一个数据库中获取的。

有可能吗?如果是,如何实现?

谢谢

【问题讨论】:

    标签: mysql sails.js


    【解决方案1】:

    是的,您可以定义多个连接并将每个模型设置为使用不同的模型。请查看documentation on connections 以获得完整评论。

    您可以在 config/connections.js 文件中设置任意数量的连接。您甚至可以使用同一个适配器设置多个连接:

    module.exports.connections = {
      // A MySQL connection
      mysql1: {
        adapter: 'sails-mysql',
        user: 'root',
        host: 'localhost',
        database: 'database1'
      },
      // Another MySQL connection, same server, different database
      mysql2: {
        adapter: 'sails-mysql',
        user: 'root',
        host: 'localhost',
        database: 'database2'
      },
      // A Postgresql connection
      postgres: {
        adapter: 'sails-postgresql',
        user: 'postgres',
        host: 'localhost',
        database: 'mypsqldb'
      }
    };
    

    然后在您的模型类文件中,指定用于该模型的连接:

    module.exports = {
        connection: 'mysql1',
        attributes: {...}
    }
    

    要指定模型的默认连接,请在 config/models.js 中进行设置:

    module.export.models = {
        connection: 'mysql2'
    };
    

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 2014-05-14
      • 2016-01-09
      • 2017-10-13
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多