Model Sails.js中单个模型的多个表?

Model Sails.js中单个模型的多个表?,model,sails.js,sails-postgresql,Model,Sails.js,Sails Postgresql,我目前有一个用Perl编写的网站,使用和。它所做的一件事就是从我的忍者座的温度传感器中提取并显示数据。我有四张Postgres表格,室外温度,室外湿度,室内也一样。这些表如下所示: Column | Type | Modifiers --------+---------+----------- time | bigint | not null value | numeric | not null date | date | not null sub curre

我目前有一个用Perl编写的网站,使用和。它所做的一件事就是从我的忍者座的温度传感器中提取并显示数据。我有四张Postgres表格,
室外温度
室外湿度
,室内也一样。这些表如下所示:

 Column |  Type   | Modifiers 
--------+---------+-----------
 time   | bigint  | not null
 value  | numeric | not null
 date   | date    | not null
sub current {
    my ( $self, $table ) = @_;
    return $self->pg->db->query( "select time, value from $table where time = (select max(time) from $table)" )->hash;
}
其中,
time
是以毫秒为单位的历元值(每分钟一条记录),
value
是温度或湿度,
date
就是温度或湿度(因此给定日期的每条记录在
date
列中的值相同)

我正在使用Node和Sails.js重写站点,主要是为了学习机会,我在这里停留在最好的方式上。在它的Perl版本中,我为每个忍者区块相关函数(显示今天的当前温度,显示给定一天的最高/最低温度等)提供了一个单独的子例程,我传递了它用于哪个位置(室内或室外),然后一切都从那里开始,如下所示:

 Column |  Type   | Modifiers 
--------+---------+-----------
 time   | bigint  | not null
 value  | numeric | not null
 date   | date    | not null
sub current {
    my ( $self, $table ) = @_;
    return $self->pg->db->query( "select time, value from $table where time = (select max(time) from $table)" )->hash;
}
但对于帆/水线,一个模型似乎只包含一个表。在我的Perl版本中,任何表之间都没有连接,只是对每个表进行一些半复杂的SQL查询,我可以使用
.query
在Sails中完成这些查询,但我不确定如何在Sails下保留相同的设置,而不必在检查室内和室外表时重复一堆代码


还是我完全错误地认为有更好的方法来完成这一切?

我最终选择了一个位于控制器和模型之间的服务。控制器函数如下所示(使用与上面相同的示例):

以及服务功能:

getCurrentTemperature: function(location, callback) {
    var query = "select time, value from " + location + "_temperature where time = (select max(time) from " + location + "_temperature)";

    var locations = {
        outdoor: function() {
            NinjaBlockOutdoorTemperature.query(query, function(err, results) {
                return callback(results.rows);
            });
        },
        indoor: function() {
            NinjaBlockIndoorTemperature.query(query, function(err, results) {
                return callback(results.rows);
            });
        }
    };

    locations[location]();
}
它可能会更优雅一点,但对我来说效果很好

因此,如果您使用的是Waterline的PostgreSQL本机
.query
,那么您甚至不需要为多个表使用多个模型。我添加了一个名为
NinjaBlock
的新模型,它没有属性或任何东西,可以运行我的查询,而不管它们在哪个表中。因此,上面的函数现在看起来像这样:

getCurrentTemperature: function(location, callback) {
    var query = "select time, value from " + location + "_temperature where time = (select max(time) from " + location + "_temperature)";

    NinjaBlock.query(query, function(err, results) {
                return callback(results.rows);
    });
}
简单