0

I've connected my SailsJs app to a Mongodb database. I'm working on an analytic application. These are the major models in my application:

User Project Report Event

A user can have many projects, a project can have many reports and a report can have many events. I have created these relations using collection and model properties of my models attributes. My problem is why is it so hard to find events of specific user? I wish I could do this:

User.
  find({id: id}).
  populate('projects').
  populate('reports').
  populate('events').
  then(function (eventsOfMyUser) {
  });

but since only projects is an attribute of my User model only the first populate works. Shouldn't be an easier way to find a deep model rather than writing nasty and confusing async loops in my controller or model code?

Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33
  • This is one of those times it makes sense to write your own method function on the model for more complex behaviour. Also might be the time to use `.query()` with plain SQL – coagmano Dec 10 '15 at 03:30
  • @FrederickStark don't you think it's a very simple behavior that waterline fails to handle? I'm starting to think sailsJS really let's you down in these simple situations and I've made a mistake to start using it. I wish I'm wrong or I can fix my issue by making changes in my project. Don't you think it must be a part of framework? – Iman Mohamadi Dec 10 '15 at 08:53
  • Yes, it is behaviour that sails / waterline should be able to handle eventually. That's the trouble working with pre-v1.0 frameworks. Would be a good idea to raise an issue on the waterline repo. Or even better, a pull request if you've got the time to make a solution – coagmano Dec 10 '15 at 11:27

2 Answers2

3

At the moment sails doesn't have any kind of nested population. The issue to reference: https://github.com/balderdashy/waterline/issues/308

Worth saying there's a pull request to add nested population: https://github.com/balderdashy/waterline/pull/1052

Pull request isn't merged at the moment but you can use it installing one directly with

npm i Atlantis-Software/waterline#deepPopulate

With it you can do something like .populate('projects.reports ...)'.

Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
0

Right now there is a hook that allows you to do that. It is changing waterline with Offshore. Offshore is extended fork of Waterline with deep populate, cache mechanism.

sails-hook-orm-offshore

In the road map right now there are:

  • Associations Criteria (add default criteria to association)
  • Transactions
Bonanza
  • 1,601
  • 14
  • 23