我认为您不能动态更改connection
模型中定义的属性。但是,您可以尝试在连接将动态更改的模型中不设置任何连接,并根据用户设置默认配置连接。就像是:
User.find(1, function(err, user){
connection = select_connection_for_phones(user);
// changing the model connection dinamically
// You should not define any connection in the model.
sails.config.models.connection = connection;
Phone.find({user : user.id }, do_something_else);
});
这样,您select_connection_for_phones
将接收用户并返回字符串连接,为模型设置全局配置连接,然后开始查询。如果您的型号电话没有定义连接,它将使用默认值。
相反的方式,可能是这样的:
User.find(1, function(err, user){
connection = select_connection_for_phones(user);
// changing properties of the postgresql connection.
// You must define all your models with this connection.
sails.config.connections.postgresql.host = host_from_user(user);
sails.config.connections.postgresql.port = port_from_user(user);
Phone.find({user : user.id }, do_something_else);
});
在这种情况下,您应该使用相同的连接设置模型,并动态更改连接的属性。