'How to customize default find, findOne, create and update actions in SailsJS?

I'm using SailJS to build a REST API. I've a lot of nested models, such as: A building has many Floors, which have many Apartments, which have an Owner.

The Sails populate implementation doesn't work as expected, and I can populate at max a single level nested model.

In order to make things work I would like to rewrite custom controllers like this: controllers/BuildingController.js

module.exports = {
  find: function (req, res) {
     // Custom code with population
  }
  findOne: function (req, res) {
     // Custom code with population
  }
  create: function (req, res) {
     // Custom code with nested object creation and connection to parent object
  }
  ...
}

If I call an endpoint through the blueprint my custom controller for that model is called, but if I call it from another point in the code (e.g. Apartment.find()) Sails use its default action, not mine.

Do you have some kind of hint or solution to this problem? Is this possible to implement?

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source