Sequelize :: Limit and Order INSIDE a Include []

I have the following model hierarchy:

User.hasMany(Child); Child.hasMany(Profile); 

Once I have a User object loaded, I need to load its Children and its associated Profiles according to the following logic:

  • Download all Children for User , sorted by name .
  • For each Child load the first three Profiles back, sorted by id .

Is there a way to limit and sort the downloaded Profiles file? I can limit and sort Children , but not Profiles .

 user.getChildren({ limit: 10, order: [['name', 'ASC']], include: [{ model: Profile, limit: 3, <-- HAS NO EFFECT!!! order: [['id', 'DESC']] <-- HAS NO EFFECT!!! }] }); 

The only thing I can do is do the following:

 user.getChildren({ limit: 10, order: [['name', 'ASC']] }).then(function(children) { user.children = children; _.each(children, function(child) { child.getProfiles({ limit: 3, order: [['id', 'DESC']] }); }); }); 

This, in my opinion, is both bad and requires additional code so that I do not get access to Children before all Children have been downloaded.

Is there a way to specify the limit and order options right inside the include[] construct?

+5
source share
1 answer

Not sure about limit , but I tried with a simple order and it worked fine:

 user.getPets({ order: 'type ASC' }) 

In addition, would I suggest that limits work too?

+1
source

All Articles