【发布时间】:2014-07-16 08:44:59
【问题描述】:
使用sails.js v0.10 rc8,我的应用有1个到mysql数据库的连接,我希望给定模型的数据是从另一个数据库中获取的。
有可能吗?如果是,如何实现?
谢谢
【问题讨论】:
使用sails.js v0.10 rc8,我的应用有1个到mysql数据库的连接,我希望给定模型的数据是从另一个数据库中获取的。
有可能吗?如果是,如何实现?
谢谢
【问题讨论】:
是的,您可以定义多个连接并将每个模型设置为使用不同的模型。请查看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'
};
【讨论】: