Save multiple model at once, in bulk

Hi everyone,

I know this subject has already been discussed around this forum and stackoverflow, but I cannot find the right way to do it.

I have a model called “post” which contain post information (user, description, etc…). The user receive a few post, reads them and make actions on them. The posts are coming from my api when I request a GET /api/posts/

I also have a model called “post-state” where I save the action of people reading the post: when they like, share, vote, or favourite the post.

export default DS.Model.extend({
    post: belongsTo('post'),
    user: belongsTo('user'), //user who reads the post, probably not the creator of the post
    liked: attr('boolean', { defaultValue: false }),
    favourited: attr('boolean', { defaultValue: false }),
    voted: attr('boolean', { defaultValue: false }),
    shared: attr('boolean', { defaultValue: false }),
});

I’m looking for a way for my ember application to save my post-state models in a bulk, just the way I received the post in a bulk as well. The post states would be saved with an api call POST /api/post-states/

I’ve read in a discussion that the best way would be to create a custom adapter, but I’m not sure what to put in it to be able to do that… maybe to create a function saveAllRecords()?

What would be the best solution for me?

Thank you for you help!

3 Likes

I think the simplest way to achieve that is, assuming post states belong to a post, save the post itself and use the EmbeddedRecordsMixin to embed the posts states in that post. Supported out of the box and no custom adapter writing.

post-states do not belongs to post, and do not belongs to user either. I haven’t created this link because what the server returns is different based on the user who reads the post. Only the current user can see if it already liked, favourited or shared the post. The other users cannot see that. This model is there to create a relationship between the model user and the model post.

post model

export default DS.Model.extend({
	user: belongsTo('user'), //user who created the post
	description: attr('string'),
	comments: hasMany('comment', {async: true})
});

Is this the right approach?

I’m not sure, in your first code snippet in your first post the model explicitly belongs to both post and user, what’s up with that?

You can see it a bit like facebook, but hidden from the others, you won’t see others likes.

  • 1 user can have many posts, a post belongs to 1 user.
  • 1 user can see many posts
  • 1 post can have many post-states, 1 for each user.

It is not the same relationship.

Data example:

posts

[{
   id: 1,
   user: 1, //user who created the post
   description: 'Content post 1'
}, {
   id: 2,
   user: 2,  //user who created the post
   description: 'Content post 2'
}]

post-states

[{
   post: 1
   user: 3,  //user who liked the post
   liked: true
}, {
   post: 2,
   user: 3,  //user who didn't liked the post
   liked: false
}]

You could do something like this in your post model:

save():{
  this._super(...arguments); //invoke the regular save
  this.get('postStates').invoke('save'); // this will trigger the save on all the children objects
}

Hi @eibrahim, Thanks for you answer, but I don’t want to have individual requests to the server for each item, I want only one request for all items.

1 Like