Following the typical sails.js association example, assume you have a model User and a model Pets. A user has multiple pets, a pet has one owner - the user. Here are the model definitions for this example for reference: https://github.com/balderdashy/sails-docs/blob/master/concepts/ORM/Associations/OnetoMany.md
With Sails.js/Blueprint default RESTful routes, I can hit the endpoints:
GET /user/:id/pets
- to return an array of a user's pets.
POST /user/:id/pets/:id
- to create an association between a pet and a user.
Is there any way that I could POST the ids of multiple pets in order to create multiple associations?
For example: Say there are three existing pets with ids p1
, p2
, p3
, and a user with id u1
. I want to hit the endpoint:
POST /user/u1/pets/p1,p2,p3
to create three associations (u1<->p1), (u1<->p2), (u1<->p3).
I've thought about implementing this as a middleware that would look for POST requests with comma-separated strings, but I'm not sure how I can generically split one POST request into multiple equivalent POST requests. By "generically" I mean that I don't want to write middleware for every combination of model associations in my app.
This is a related question trying to do almost the exact opposite, but I wasn't able to draw any specific conclusions from it: Sails.js how to modify routes to interprate comma separated list of ids