2

I have a data model that has a many to many relship. Let's say that I have a Producer model, and a Customer module. A customer can buy from any number of producers, and a produces can serve any number of customers.

In the backend, I have the standard relational configuration for this with three tables:

  • customer
  • producer
  • customer2producer

My Ember models currently look like this:

// models/producer.js
import DS from 'ember-data';

export default DS.Model.extend({
  customers: DS.hasMany('module'),
});

// models/customer.js
import DS from 'ember-data';

export default DS.Model.extend({
  producers: DS.hasMany('module'),
});

Right now I am working with fixtures, and what I do is just define the customers and producers arrays with the ids of the "targets". The question is, that sooner or later I will switch to a real REST backend, and that means I have to deal with the model mismatch. I think I have to options:

I guess this is a very common scenario, and I would like to know what is the cleanest way to approach the problem. Thanks in advance.

Community
  • 1
  • 1
bgusach
  • 14,527
  • 14
  • 51
  • 68

1 Answers1

1

Your database doesn't need to map onto your REST API. Particularly you don't have the limitation of flat data (nor do you in some databases these days).

If you're in control of your backend API, I would just return responses like:

GET /customers

{
  customers: [
    {id: 1, producers: [1, 2]},
    {id: 2, producers: []},
  ]
}

GET /producers

{
  producers: [
    {id: 1, customers: [1]},
    {id: 2, customers: [1]}
  ]
}

Your REST backend should be the one concerned about serializing and deserializing to the database models, not your frontend app.

If you ever have something consuming the API that you can't instantly modify (mobile app, third-parties etc.) your REST API will naturally grow disjoint from your backend models over time anyways, giving you data transformation work. So I really want to compel you to just do what makes sense for REST.

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
  • This is a perfectly good option, but I can't see why I should clearly choose this over managing relationships on the frontend... (apart from my lack of experience in Ember... writing code on the backend would actually be easier for me) – bgusach Aug 10 '15 at 19:13
  • @ikaros45 You still have to manage relationships. You'd end up with 1 more endpoint `/customer2producers` another model which doesn't do anything besides holding relations in both directions. Or if you mean making an additional request to a relationship endpoint, without a corresponding ember model then you'd end up doing a weird override in the adapter where you're sideloading relations information and tracking it on before pushing the data to the store. It's just slow, verbose and awkward all around. – Kit Sunde Aug 10 '15 at 19:19
  • Yes, by managing relships I meant having an ember model `customer2producer`... the point is that I already have it on my backend (it's by default in the framework I'm working with...). The first link on my question seems to offer a solution to navigate the many2many relationships, but looks a little bit clunky... – bgusach Aug 10 '15 at 19:30
  • @ikaros45 I get you. It's clunky and slow because it's an extra endpoint to load data from that will be loaded after you get your initial data (unless you use embeded records, but then you're already doing basically doing transforms so you might as well include the relationship ids), if it takes someone halfway around the world 100ms, your user will end up waiting 200ms when it has to traverse the internet sequentially. The only reason why I would use an intermediary model like that is if it has data on it, then use the computed property for easy access. – Kit Sunde Aug 10 '15 at 19:39
  • Well speed is not an issue here, this is for some kind of intranet. But I think I will go for your approach to save some javascript headaches... – bgusach Aug 10 '15 at 19:46