如何使Sails模型指向不同的数据库?

我创build了一个Sails应用程序,我需要对模型数据库进行事务处理的面板pipe理。 关键是,后端架构和数据pipe理需要Sails应用程序中相同模型的不同数据库。

所以,我的模型看起来像这样:

module.exports = { connection: 'seasonDB', autoPK: false, attributes: { id: { type: 'string', primaryKey: true, required: true }, avatar: { type: 'string', defaultsTo: '' }, price: { type: 'integer', defaultsTo: function () { var prices = [ 7000000, 5000000, 4000000, 2500000, 1000000 ]; return prices[_.random(prices.length -1)]; } }, description: { type: 'string', defaultsTo: '' }, status: { type: 'string', enum: ['suspended', 'inSquad','injured','out'] } } }; 

config/connections.js定义'seasonDB'位置:

  seasonDB: { adapter: process.env.SEASON_DB_ADAPTER, host: process.env.SEASON_DB_HOST, port: process.env.SEASON_DB_PORT, user: process.env.SEASON_DB_USER, password: process.env.SEASON_DB_PASSWORD, database: process.env.SEASON_DB_DB } 

那么,有没有办法改变环境variables,并以某种方式重新加载整个应用程序重新加载模型?

有一个名为sails-hook-autoreload的sails.js钩子。 它跟踪对控制器,模型的更改并应用更改,而无需手动重新加载。 你可以尝试使用它,或者只为模型创build类似的东西。