epilogue
epilogue copied to clipboard
Create object and associations in one request
Hey, Is it possible to create an object and attach it to associations (or even create the associations and attach them) in one request?
For example: Create/Update user instance and assign existing tasks or new task to the user POST: api/users username: asaf password: hashed tasks: [1,4,6]
OR POST: api/users username: asaf password: hashed tasks: [{'name':'Check for emails'},{'name':'Turn on your skype'}]
Thanks Asaf
Not sure if epilogue provides by default. The way you customise epilogue is by writing middleware and hook it before or after the action. In your case it should be after create as you could assign association only after creating the parent object.
module.exports = {
create: {
write: {
after: function(req, res, context) {
var task_ids = req.body.tasks;
for (var i = 0; i < task_ids.length; i++) {
models.Task.findById(task_ids[i]).then(function(task) {
context.instance.setTasks(task);
});
}
// manipulate the fetch call
return context.continue;
}
}
},
update: {
fetch: function(req, res, context) {
return context.continue;
}
}
}
Hope that helps!
I'm currently working on something similar : https://github.com/dchester/epilogue/pull/193