Best practices with many to many relationships in JsonAPI

Hi guys,

how do you let the server know that there is a relationship many-to-many between two models when storing them? I know that in ember-data a relationship of this kind can be easily implemented by using hasMany in both models but when you want to save them, how does the server know that there’s a relationship between them? because in the models there’s no info about their relationship (apart from the hasMany property). Do you customize the serializer to send both models together? do you add info in the meta to let the server know that it has to do something else with that record?


This is a question I really would like to have answered too. I am also quite uncertain what json:api input I have to give to work with embers many-to-many implementation. How does the json need to look like ? There is very little documentation in the jsonapi spec so I am unsure how this works.

@Henry, @teejay Any update on how to solve this?

Do you have any more specific questions?

Broad strokes:

  • relationships in ember-data have automatically inferred inverses, but I prefer to specify them explicitly, anyway because of the inverses you should only have to set the relationship one way and it will link them
  • both records should be saved, meaning a request is made (POST/PUT/PATCH depending on your scenario and config) for each of the records
  • usually (and this should be done automatically by the JSON API serializer) the relationship is serialized with the type and ID of the related records. .

If you’re running into issues or have specific questions feel free to ask!

Is it possible to add transaction to make those action save? For example, if one of the record failed to save, do we have any way to abort previously saved record?

One alternative is to extend ember store to save model domains, so you send the main model and its relationships together in a single request.

@Henry do you have any example code for this?

Here you have an example of the store service being extended to support batch operations.