回送模型中的动态属性或聚合函数 [英] Dynamic Properties or Aggregate Functions in Loopback Models

查看:77
本文介绍了回送模型中的动态属性或聚合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何在Loopback模型中使用聚合函数?如果我有一个由mysql数据库支持的模型,是否可以让Model1具有与Model2的hasMany关系(具有给定的数字属性),并在Model1中具有从Model2抓取该字段的SUM的属性?

How would I go about using aggregate functions in Loopback models? If I have a model backed by a mysql database, could I have Model1 with a hasMany relation to Model2 (with a given numeric property), and have a property in Model1 that grabs the SUM of that field from Model2?



    {
        "Model1" : {
            "Relations" : {
                "model2s" : {
                    "type": "hasMany",
                    "model": "Model2",
                    "foreignKey": "model1Id"
                }
            },
            "Properties" : {
                "total" : {
                    "type": "Number"
                    [SUM of Model2 'amount' field]
                }
            }
        },
        "Model2" : {
            "Relations" : {
                "model1s" : {
                    "type": "belongsTo",
                    "model": "Model1",
                    "foreignKey": "model1Id"
                }
            },
            "Properties" : {
                "amount" : {
                    "type": "Number"
                }
            }
        }
    }

在另一件事上,将条件放入模型的正确方法是什么,以使getter返回的值取决于某个表达式?我想从关系中返回一个值(如果存在),否则返回主模型上存在的值.

On a separate matter, what is the correct way to put a conditional in a model, so that the value returned by a getter depends on some expression? I want to return a value from a relation if it exists, otherwise return the one that exists on the primary model.

我已经尝试过这个(伪代码):

I have tried this (pseudocode):



    module.exports = function(MyModel) {
        MyModel.on('attached', function() {
            var app = MyModel.app;

            MyModel.getter['total'] = function() {
                return (this.model1Id ? this.model1.total : this.total);
            };
        });

    };

但是,我最终收到一个RangeError: Maximum call stack size exceeded错误(类似于

However, I end up getting a RangeError: Maximum call stack size exceeded error (similar to what is noted in this question). I'm assuming that is because it recursively calls the getter over and over, but I'm not sure of the way to resolve the issue.

预先感谢...

推荐答案

AFAIK回送目前不支持聚合函数/属性.请打开github问题,以将其作为功能请求进行跟踪.

AFAIK loopback does not support aggregate functions/properties at the moment. Please open a github issue to track this as a feature request.

请注意,访问相关模型的数据是异步操作,因此无法可靠地实现属性(getter函数)以返回汇总结果.

Note that accessing data of related models is an asynchronous operation, thus it's not possible to reliably implement a property (a getter function) to return the aggregated result.

以下是一个模型,显示了如何在计算出的total上正确实现:

Here is a mock-up showing how to correctly implementat a computed total:

MyModel.prototype.getTotal = function(cb) {
  if (!this.model1Id) {
    // No related model, return the total from this model.
    return cb(null, this.total);
  }

  // Fetch the related model and return its total
  this.model1(function(err, model1) {
    if (err)
      cb(err);
    else
      cb(null, model1.total);
  });
}

在另一件事上,将条件放入模型的正确方法是什么,以使getter返回的值取决于某个表达式? 我最终收到一个RangeError: Maximum call stack size exceeded错误

On a separate matter, what is the correct way to put a conditional in a model, so that the value returned by a getter depends on some expression? I end up getting a RangeError: Maximum call stack size exceeded error

正如我在您链接到的答案中所解释的那样,this.total调用您的自定义getter函数,该函数依次调用this.total等.

As I explained in the answer you have linked to, this.total calls your custom getter function, which in turns calls this.total and so on.

解决方案是从内部数据对象中读取值:

The solution is to read the value from the internal data object:

MyModel.getter['total'] = function() {
   return this.__data.total;
};

这篇关于回送模型中的动态属性或聚合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆