SailsJS使用Model.query手动填充记录



假设我有这个例子作为我的应用程序http://sailsjs.org/#!/documentation/concepts/ORM/Associations/OnetoMany.html

由于一些大原因(复杂),我无法使用Model.populate(),并且我被困在使用Model.query()

有谁知道如何User.find().populate('pets')使用Model.query()获得结果

谢谢

您可以像水线适配器一样填充OneToMany:

  1. 检索父母 :
    select * from user ...
  2. 仅在一个查询中检索每个父项的子项,以免重载 DBMS:
    select * from pet where user = user1.id union select * from pet where user = user2.id union ... union select * from pet where user = userN.id .
  3. 通过 parentPk 重新分组子项(您可以使用 lodash 或下划线.js函数来执行此操作)例如:


    users.forEach(function(user){ user.pets = _.filter(pets,function(pet){ return pet.user === user.id; }); });

最新更新