简体   繁体   English

在Sails.JS / Waterline中聚合嵌套模型数据

[英]Aggregate nested model data in Sails.JS/Waterline

I'm struggling with where to put my business logic in my SailsJS project. 我正在努力将我的业务逻辑放在我的SailsJS项目中。

My data model is as follows: 我的数据模型如下:

Portfolio (hasMany) -> Positions (hasOne) -> Asset-> (hasMany) Ticks 

I'm looking to aggregate data of the children using computed properties on the parent model cascading all the way up to Portfolio. 我正在寻找使用父模型上的计算属性来汇总子项的数据,一直到项目组合。 eG an Asset knows the newest tick (price), the position knows its current price (numberShares * newest Tick of its Asset), etc. Ideally i want to add this data to the JSON so i can move business logic away from the Ember client. eG an Asset知道最新的价格(价格),该位置知道其当前价格(numberShares *其资产的最新Tick)等。理想情况下,我想将此数据添加到JSON,以便我可以将业务逻辑从Ember客户端移开。

This works fine for one level, but if i try to use a computed property of a child, it's either non defined or when using toJSON() empty 这适用于一个级别,但如果我尝试使用子级的计算属性,它是非定义的或使用toJSON()为空时

Asset.js: Asset.js:

latestTick: function () {
    if (this.ticks.length> 0) 
    {

      this.ticks.sort(function(a, b){
       var dateA=new Date(a.date), dateB=new Date(b.date)
       return dateB-dateA
      })


      return this.ticks[Object.keys(this.ticks)[0]].price;
    }

    return 0;
  },


  toJSON: function() {
    var obj = this.toObject();


  if (typeof this.latestTick === 'function')
    obj.latestTick = this.latestTick();
  return obj;
}

This works (after adding the typeof check as depending on whether it was returned nested or not it didn't work). 这是有效的(在添加类型检查后,根据它是否被嵌套返回它不起作用)。

Now in Position.js 现在在Position.js

 assetID:{
      model: "Asset",
    },

i want to calculate currentValue: 我想计算currentValue:

   currentValue: function () {
         return this.assetID.latestTick() * this.numberShares;

    },

which does not work because the function is not available (only the hardcoded attributes). 这不起作用,因为该功能不可用(只有硬编码属性)。 I tried using Tick.Find()... but ran into async issues (JSON is returned before data is fetched). 我尝试使用Tick.Find()...但遇到异步问题(在获取数据之前返回JSON)。

I tried to force embedding / populate the associations in the JSON but it made no difference. 我试图强制嵌入/填充JSON中的关联,但它没有任何区别。 As soon as I'm crossing more than one hierarchy level i don't get any data. 一旦我跨越多个层次结构级别,我就不会获得任何数据。

Where should/can i do this aggregation? 我应该在哪里进行这种聚合? I don't want to use custom controller actions but leverage the REST API. 我不想使用自定义控制器操作,而是利用REST API。

Waterline does not support deep populates. 水线不支持深层人口。 If you want to implement a deep query like that, you can use native ( https://sailsjs.com/documentation/reference/waterline-orm/models/native ) if you are using Sails < 1, or manager ( https://sailsjs.com/documentation/reference/waterline-orm/datastores/manager ) if you are using Sails >= 1 如果你想实现这样的深度查询,你可以使用nativehttps://sailsjs.com/documentation/reference/waterline-orm/models/native ),如果你使用的是Sails <1或者managerhttps:/ /sailsjs.com/documentation/reference/waterline-orm/datastores/manager )如果你使用Sails> = 1

That way you can use whatever your database is capable for building this kind of "more advanced" queries. 这样,您可以使用您的数据库能够构建此类“更高级”查询的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM