2

On different projects, I've been stucking on a very basic idea.

Everytime, that's the same concern. I want to add a new record to an association, but grabbing the parent without it's primary key.

For example, let's take a api/models/car.js model :

module.exports = {

  attributes: {
    licensePlate: {
      type: 'integer',
      required: true,
      unique: true
    },
    locations: {
      collection: 'location',
      via: 'car'
    }
  }
};

And an api/models/location.js model :

module.exports = {

  attributes: {
    coordinates: {
      type: 'array',
      required: true
    },
    car: {
      model: 'car'
    }
  }
};

A car can have multiple locations, a location have a single car.

I'm able to add a location to car using the native addTo blueprint action

POST /car/1/locations
{"coordinates":[2.13654,50.323654]}

Now what if, for some reason, I've no access to the car identifier, and feel like using another field, like a unique licensePlate ?

Basically, I would make a custom route inside config/routes, like

POST /car/byplate/:licensePlate/locations': {
  controller: 'Car',
  action: 'addLocationByPlate'
}

In order to be able to call

POST /car/byplate/AW45RE65/locations
{"coordinates":[2.13654,50.323654]}

And here is the problem... opening my fresh new action controller, I realize that, despite selecting my car by plate, the following logic (validation, location creation, location creation publish, location add to car's locations collection, location addition publish, error handling) is already implemented in sails.js core.

So here is the question:

How to properly call a native blueprint action with a custom route ?

Cyril CHAPON
  • 3,556
  • 4
  • 22
  • 40

1 Answers1

0

In your controller you can write something like this:

YourModelName.Query(data, function(err, items){
    if(err) return err;

    res.json(items);
})

So for example if you want to create a new object in Car model you can do something like this:

Car.create({"carID": req.param("carID")}, function(err, items){
   if(err) return err;

   res.json(items);
})

this will createa new object with the ID you sent as a param. Same goes for the other queries like add to, update, destroy etc.

Matan Gubkin
  • 3,019
  • 6
  • 25
  • 44
  • Hello. Thanks, but I stated that *"And here is the problem... opening my fresh new action controller, I realize that, despite selecting my car by plate, the following logic (validation, location creation, location creation publish, location add to car's locations collection, location addition publish, error handling) is already implemented in sails.js core."* – Cyril CHAPON Apr 24 '15 at 10:16
  • 1) You're returning a 200 where sails.js would return a 201. 2) You're not publishing anything to the socket rooms, neither the `created` neither the `addedTo`. 3) You're not making any validation 4) You're not handling any errors. – Cyril CHAPON Apr 24 '15 at 10:18
  • And I'm not blaming you. Because that's **complicated**. My real problem is I don't want to think about it all, because it's **already implemented** in sails.js => that's why I'm using a framework – Cyril CHAPON Apr 24 '15 at 10:19